Merhaba.
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ı!';
}
}