merhaba,
migros.com.tr üzerinden sadece 1 ürün çekiyorum yanlız bu ürün yerel aramalar haricinde çıkmıyor. teslimat adresini girmek gerekiyor. curl ile ilgili çok bir tecrübem yok. curl postfields filan denedim ama inspectordan bir türlü izleyeceğim yolu çözemedim.
https://www.migros.com.tr/adresime-gelsin
sayfasından
adana
çukurova
beyazevler mah.
seçip
örnek olarak
https://www.migros.com.tr/yesil-koza...700-g-p-9a21d1
bu yerel markanın fiyatını çekmem gerekiyor.
nasıl yapacağım konusunda yönlendirebilecek biri var mı ?
php curl yardım
4
●89
- 23-11-2020, 17:41:56
- 23-11-2020, 18:16:52Kimlik doğrulama veya yönetimden onay bekliyor.visitorId kismini o anki session id'niz ile guncellemeniz gerekiyor.
$visitorId = '28e412d49-cbc2-4t63-8dee-8e89108202bd'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'https://www.migros.com.tr/rest/regions'); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, '{"serviceAreaObjectId":1015010,"serviceAreaObjectType":"DISTRICT"}'); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Authority: www.migros.com.tr', 'Pragma: no-cache', 'Cache-Control: no-cache', 'Accept: application/json, text/plain, */*', 'Dnt: 1', 'Accept-Language: en-US,en;q=0.9,tr;q=0.8,ru;q=0.7', 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36', 'Content-Type: application/json', 'Origin: https://www.migros.com.tr', 'Sec-Fetch-Site: same-origin', 'Sec-Fetch-Mode: cors', 'Sec-Fetch-Dest: empty', 'Referer: https://www.migros.com.tr/adresime-gelsin', 'Cookie: VSTR_ID=' . $visitorId; ]); curl_exec($ch); curl_close($ch);Bu istegi gonderdikten sonra ayni session id ile istediginiz urunun fiyat bilgisini alabilirsiniz. Kolay gelsin. - 23-11-2020, 18:32:40Üyeliği durdurulduMerhaba.
Seçili adres bilgisi sessionda, session id değeri ise cookie'de saklandığından ötürü yapmanız gereken cookieyi tutmak ve bir sonraki isteklerde kullanmak.
Şimdi her şeyden önce, belirleyeceğimiz adresin id değerine ulaşmamız gerekli. Ben şunu fark ettim:
- İlk istekte https://www.migros.com.tr/rest/locat...tType=DISTRICT adresine gider, burada iller ve plaka kodları olur. Buradan ilimizin plaka kodunu (id değeri) seçilir. Adana için ID değeri 1 mesela.
- Şehrimizi seçtiğimizde başka bir istek gider, bu istekte ilçe listesi alınır. https://www.migros.com.tr/rest/locations/towns/{$plaka_kodu}/deliverable?serviceAreaObjectType=DISTRICT
Burada ilçe id değerini almamız gerekli, Çukurova için 1015 mesela.
- İlçe seçilince bir istek daha gider, https://www.migros.com.tr/rest/locations/districts/{$ilce_id}/deliverable, burada ilçenin mahalleleri gözükür. Bizim almamız gereken değer yine id değeri, Beyazevler için 1015010 müş.
- Son istek artık bir POST isteğidir, https://www.migros.com.tr/rest/regions adresine bir JSON post edilir. Json içeriği şöyledir: {"serviceAreaObjectId":{$mahalle_id},"serviceAreaO bjectType":"DISTRICT"}
Şimdi bunu koda dökelim:
<?php $mahalle_id = '1015010'; $data = [ 'serviceAreaObjectId' => $mahalle_id, 'serviceAreaObjectType' => 'DISTRICT' ]; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'https://www.migros.com.tr/rest/regions'); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); // bir sonraki istekte cookielerin kaybolmasını istemiyoruz. curl_setopt($ch, CURLOPT_COOKIESESSION, true); curl_setopt($ch, CURLOPT_COOKIEJAR, ''); curl_setopt($ch, CURLOPT_COOKIEFILE, ''); curl_exec($ch); // şimdi ilk isteğimizi attık, adresimizi tanımladık. yapmamız gereken fiyatı çekmek. curl_setopt($ch, CURLOPT_URL, 'https://www.migros.com.tr/yesil-kozan-taze-kasar-peyniri-700-g-p-9a21d1'); curl_setopt($ch, CURLOPT_POST, false); $result = curl_exec($ch); // şimdi regex ile fiyatı çekelim. preg_match("'<span class=\"value\" id=\"store-product-primary-price\">(.*?)</span>'si", $result, $match); print 'Fiyat: '.$match[1]; ?>Şimdi gördüğünüz gibi ben burada mahalle id değerini sabit belirledim, siz değişken olacak ben onu da otomatik çekmek istiyorum derseniz şu fonksiyonu kullanabilirsiniz:
function get_mahalle_id($sehir, $ilce, $mahalle) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'https://www.migros.com.tr/rest/locations/cities/deliverable?serviceAreaObjectType=DISTRICT'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($ch); $data = json_decode($result, 1)['data']; $data = array_filter($data, function($item) use ($sehir) {return mb_strtolower($item['name']) == mb_strtolower($sehir);}); if(count($data) > 0) { foreach($data as $item) {$sehir_id = $item['id'];break;} curl_setopt($ch, CURLOPT_URL, 'https://www.migros.com.tr/rest/locations/towns/'.$sehir_id.'/deliverable?serviceAreaObjectType=DISTRICT'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($ch); $data = json_decode($result, 1)['data']; $data = array_filter($data, function($item) use ($ilce) {return mb_strtolower($item['name']) == mb_strtolower($ilce);}); if(count($data) > 0) { foreach($data as $item) {$ilce_id = $item['id'];break;} curl_setopt($ch, CURLOPT_URL, 'https://www.migros.com.tr/rest/locations/districts/'.$ilce_id.'/deliverable'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($ch); $data = json_decode($result, 1)['data']; $data = array_filter($data, function($item) use ($mahalle) {return mb_strtolower($item['name']) == mb_strtolower($mahalle);}); if(count($data) > 0) { foreach($data as $item) {return $item['id'];} } else { return 'Geçersiz mahalle adı!'; } } else { return 'Geçersiz ilçe adı!'; } } else { return 'Geçersiz şehir adı!'; } }