Запрос на смену города через CURL

easyrocket

Созидатель (II)
Сообщения
12
Симпатии
1
Баллы
54
Сам уже замучился, пишу парсер c re-store.ru, все отлично работает, но не могу добиться смены города через curl запрос! Может кто помочь? (за небольшой вознаграждение разумеется)

На re-store фронтенд релизация смены города:
Код:
function changeCity(city, cityID){
    $.ajax({
        type: "POST",
        url: "/local/components/multisite/city.selector/ajax.php",
        data: {city: city, url:location.href, city_id: cityID, "action": "changeCity"},
        success: function (result) {
            var obj = jQuery.parseJSON(result);

            if (typeof obj.ddlWebsite !=='undefined'){
                $(document).trigger('change-city',obj.ddlWebsite);
            }

            if(obj.status){
                window.location.reload();
            }
        }
    });
}

При curl запросе мне просто возвращает менюшку со списком городов

А вот собственно и сам CURL
Код:
<?

        $config['cookie_file'] = $dir . '/cookies/' . md5($_SERVER['REMOTE_ADDR']) . '.txt';
           
        $data1 = '{"city": "Пермь", "action": "changeCityUser"}';
        $data1 = json_decode($data1, 1);
        $data1 = http_build_query($data1);

            $url = "https://re-store.ru/local/components/multisite/city.selector/ajax.php";
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $url );

            // get http header for cookies
            curl_setopt($curl, CURLOPT_VERBOSE, 1);
            curl_setopt($curl, CURLOPT_HEADER, 1);
            curl_setopt($curl, CURLINFO_HEADER_OUT, true);

            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);

            curl_setopt($curl, CURLOPT_COOKIEFILE, $config['cookie_file']);
            curl_setopt($curl, CURLOPT_COOKIEJAR, $config['cookie_file']);

            $headers = array(
                 'Connection: keep-alive',
                 //'Access-Control-Allow-Origin: *',
                'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.34',
                'Content-Type: text/html; charset=UTF-8',
                   'Accept: */*'
               );

            curl_setopt($curl, CURLOPT_REFERER, 'https://re-store.ru/discount/');
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data1);

   
            $out = curl_exec($curl);

            curl_close($curl);
   
?>
В моем случае возвращает всегда данный html, хотя Снимок экрана 2022-02-13 в 14.25.47.png
 
Последнее редактирование модератором:
Код:
https://re-store.ru/local/components/multisite/city.selector/ajax.php
Этот запрос устанавливает куку:
Код:
user_city=Краснодар; expires=Tue, 15-Mar-2022 16:24:47 GMT; Max-Age=2592000; path=/
По "Краснодару" запрос через курл должен быть:
PHP:
curl_setopt($ch, CURLOPT_COOKIE, 'user_city=Краснодар');
Либо второй запрос с:
PHP:
curl_setopt($ch, CURLOPT_COOKIEFILE, $config['cookie_file']);
 
Код:
https://re-store.ru/local/components/multisite/city.selector/ajax.php
Этот запрос устанавливает куку:
Код:
user_city=Краснодар; expires=Tue, 15-Mar-2022 16:24:47 GMT; Max-Age=2592000; path=/
По "Краснодару" запрос через курл должен быть:
PHP:
curl_setopt($ch, CURLOPT_COOKIE, 'user_city=Краснодар');
Либо второй запрос с:
PHP:
curl_setopt($ch, CURLOPT_COOKIEFILE, $config['cookie_file']);

Я изначально тоже так думал, но у меня ничего не вышло. и сейчас еще раз перепроверил, результат тот же 😭

Сможете помочь
Код:
https://re-store.ru/local/components/multisite/city.selector/ajax.php
Этот запрос устанавливает куку:
Код:
user_city=Краснодар; expires=Tue, 15-Mar-2022 16:24:47 GMT; Max-Age=2592000; path=/
По "Краснодару" запрос через курл должен быть:
PHP:
curl_setopt($ch, CURLOPT_COOKIE, 'user_city=Краснодар');
Либо второй запрос с:
PHP:
curl_setopt($ch, CURLOPT_COOKIEFILE, $config['cookie_file']);

Сможете помочь написать готовый вариант? Я готов оплатить)))

и этот линк (https://re-store.ru/local/components/multisite/city.selector/ajax.php) не савит куку user_city, вот что он ставитСнимок экрана 2022-02-13 в 22.31.20.png
 
PHP:
            $headers = array(
                 'Connection: keep-alive',
                 //'Access-Control-Allow-Origin: *',
                'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.34',
                'Content-Type: text/html; charset=UTF-8',
                   'Accept: */*'
               );
Зачем тут Content-Type: text/html если отправляешь не html код, а данные application/x-www-form-urlencoded. В данном случае нет необходимости менять этот заголовок т.к. curl самостоятельно установит нужное значение.
Добавить нужно заголовок X-Requested-With: XMLHttpRequest и в $data1 добавить city_id.
PHP:
        $config['cookie_file'] = $dir . '/cookies/' . md5($_SERVER['REMOTE_ADDR']) . '.txt';
           
        $data1 = '{"city": "Пермь", "city_id": "", "action": "changeCityUser"}';
        $data1 = json_decode($data1, 1);
        $data1 = http_build_query($data1);

            $url = "https://re-store.ru/local/components/multisite/city.selector/ajax.php";
           
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $url );

            // get http header for cookies
            curl_setopt($curl, CURLOPT_VERBOSE, 1);
            curl_setopt($curl, CURLOPT_HEADER, 1);
            curl_setopt($curl, CURLINFO_HEADER_OUT, true);

            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);

            curl_setopt($curl, CURLOPT_COOKIEFILE, $config['cookie_file']);
            curl_setopt($curl, CURLOPT_COOKIEJAR, $config['cookie_file']);

            $headers = array(
                 'Connection: keep-alive',
                 //'Access-Control-Allow-Origin: *',
                'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.34',
                   'Accept: */*',
                   'X-Requested-With: XMLHttpRequest'
               );

            curl_setopt($curl, CURLOPT_REFERER, 'https://re-store.ru/discount/');
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data1);

   
            $out = curl_exec($curl);

            curl_close($curl);
 
PHP:
    function init()
    {
        $curl = curl_init("https://re-store.ru/");
        curl_setopt($curl, CURLOPT_VERBOSE, 1);
        curl_setopt($curl, CURLOPT_HEADER, 1);
        curl_setopt($curl, CURLINFO_HEADER_OUT, true);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYSTATUS, false);
        curl_setopt($curl, CURLOPT_COOKIEJAR, './cookie');
        curl_setopt($curl, CURLOPT_REFERER, 'https://re-store.ru/');
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            'Content-Type: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
            'Connection: keep-alive',
            'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:97.0) Gecko/20100101 Firefox/97.0',
            'Accept-Language: en-US,en;q=0.9',
            'Pragma: no-cache',
            'Cache-Control: no-cache',
            'sec-ch-ua: "Opera";v="83", "Chromium";v="97", ";Not A Brand";v="99"',
            'sec-ch-ua-mobile: ?0',
            'sec-ch-ua-platform: "Windows"',
            'Sec-Fetch-Dest: document',
            'Sec-Fetch-Mode: navigate',
            'Sec-Fetch-Site: none',
            'DNT: 1',
        ));
        $result = curl_exec($curl);
        preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $result, $match_found);

        $cookies = array();
        foreach($match_found[1] as $item) {
            parse_str($item,  $cookie);
            $cookies = array_merge($cookies,  $cookie);
        }

        curl_close($curl);
        return $cookies['BITRIX_SM_SALE_UID'];
    }

    function change_city($city)
    {
        $sale_uid = init();
        sleep(1);
        $curl = curl_init("https://re-store.ru/local/components/multisite/city.selector/ajax.php");
        curl_setopt($curl, CURLOPT_VERBOSE, 1);
        curl_setopt($curl, CURLOPT_HEADER, 1);
        curl_setopt($curl, CURLINFO_HEADER_OUT, true);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYSTATUS, false);
        curl_setopt($curl, CURLOPT_COOKIEFILE, './cookie');
        curl_setopt($curl, CURLOPT_COOKIEJAR, './cookie');
        curl_setopt($curl, CURLOPT_REFERER, 'https://re-store.ru/discount/');
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
            'Connection: keep-alive',
            'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:97.0) Gecko/20100101 Firefox/97.0',
            'Accept-Language: en-US,en;q=0.9',
            'Pragma: no-cache',
            'Cache-Control: no-cache',
            'sec-ch-ua: "Opera";v="83", "Chromium";v="97", ";Not A Brand";v="99"',
            'sec-ch-ua-mobile: ?0',
            'sec-ch-ua-platform: "Windows"',
            'Sec-Fetch-Dest: document',
            'Sec-Fetch-Mode: navigate',
            'Sec-Fetch-Site: none',
            'DNT: 1',
            'X-Requested-With: XMLHttpRequest',
        ));
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, 'fuserId='.$sale_uid.'&city='.urlencode($city).'&url=https%3A%2F%2Fre-store.ru%2Fdiscount%2F&action=changeCity');
        $out = curl_exec($curl);
        curl_close($curl);
        return $out;
    }

    function get_discount($city)
    {
        change_city($city);
        sleep(1);
        $curl = curl_init("https://re-store.ru/discount/");
        curl_setopt($curl, CURLOPT_VERBOSE, 1);
        curl_setopt($curl, CURLOPT_HEADER, 1);
        curl_setopt($curl, CURLINFO_HEADER_OUT, true);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYSTATUS, false);
        curl_setopt($curl, CURLOPT_COOKIEFILE, './cookie');
        curl_setopt($curl, CURLOPT_COOKIE, 'user_city='.urlencode($city));
        curl_setopt($curl, CURLOPT_REFERER, 'https://re-store.ru/');
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            'Content-Type: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
            'Connection: keep-alive',
            'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:97.0) Gecko/20100101 Firefox/97.0',
            'Accept-Language: en-US,en;q=0.9',
            'Pragma: no-cache',
            'Cache-Control: no-cache',
            'sec-ch-ua: "Opera";v="83", "Chromium";v="97", ";Not A Brand";v="99"',
            'sec-ch-ua-mobile: ?0',
            'sec-ch-ua-platform: "Windows"',
            'Sec-Fetch-Dest: document',
            'Sec-Fetch-Mode: navigate',
            'Sec-Fetch-Site: none',
            'DNT: 1',
        ));
        $out = curl_exec($curl);
        $headerSent = curl_getinfo($curl, CURLINFO_HEADER_OUT);
        curl_close($curl);
        return $out;
    }

    $o = get_discount('Краснодар');

    file_put_contents('./out', $o);

Вот так работает :)
 
PHP:
    function init()
    {
        $curl = curl_init("https://re-store.ru/");
        curl_setopt($curl, CURLOPT_VERBOSE, 1);
        curl_setopt($curl, CURLOPT_HEADER, 1);
        curl_setopt($curl, CURLINFO_HEADER_OUT, true);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYSTATUS, false);
        curl_setopt($curl, CURLOPT_COOKIEJAR, './cookie');
        curl_setopt($curl, CURLOPT_REFERER, 'https://re-store.ru/');
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            'Content-Type: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
            'Connection: keep-alive',
            'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:97.0) Gecko/20100101 Firefox/97.0',
            'Accept-Language: en-US,en;q=0.9',
            'Pragma: no-cache',
            'Cache-Control: no-cache',
            'sec-ch-ua: "Opera";v="83", "Chromium";v="97", ";Not A Brand";v="99"',
            'sec-ch-ua-mobile: ?0',
            'sec-ch-ua-platform: "Windows"',
            'Sec-Fetch-Dest: document',
            'Sec-Fetch-Mode: navigate',
            'Sec-Fetch-Site: none',
            'DNT: 1',
        ));
        $result = curl_exec($curl);
        preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $result, $match_found);

        $cookies = array();
        foreach($match_found[1] as $item) {
            parse_str($item,  $cookie);
            $cookies = array_merge($cookies,  $cookie);
        }

        curl_close($curl);
        return $cookies['BITRIX_SM_SALE_UID'];
    }

    function change_city($city)
    {
        $sale_uid = init();
        sleep(1);
        $curl = curl_init("https://re-store.ru/local/components/multisite/city.selector/ajax.php");
        curl_setopt($curl, CURLOPT_VERBOSE, 1);
        curl_setopt($curl, CURLOPT_HEADER, 1);
        curl_setopt($curl, CURLINFO_HEADER_OUT, true);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYSTATUS, false);
        curl_setopt($curl, CURLOPT_COOKIEFILE, './cookie');
        curl_setopt($curl, CURLOPT_COOKIEJAR, './cookie');
        curl_setopt($curl, CURLOPT_REFERER, 'https://re-store.ru/discount/');
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
            'Connection: keep-alive',
            'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:97.0) Gecko/20100101 Firefox/97.0',
            'Accept-Language: en-US,en;q=0.9',
            'Pragma: no-cache',
            'Cache-Control: no-cache',
            'sec-ch-ua: "Opera";v="83", "Chromium";v="97", ";Not A Brand";v="99"',
            'sec-ch-ua-mobile: ?0',
            'sec-ch-ua-platform: "Windows"',
            'Sec-Fetch-Dest: document',
            'Sec-Fetch-Mode: navigate',
            'Sec-Fetch-Site: none',
            'DNT: 1',
            'X-Requested-With: XMLHttpRequest',
        ));
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, 'fuserId='.$sale_uid.'&city='.urlencode($city).'&url=https%3A%2F%2Fre-store.ru%2Fdiscount%2F&action=changeCity');
        $out = curl_exec($curl);
        curl_close($curl);
        return $out;
    }

    function get_discount($city)
    {
        change_city($city);
        sleep(1);
        $curl = curl_init("https://re-store.ru/discount/");
        curl_setopt($curl, CURLOPT_VERBOSE, 1);
        curl_setopt($curl, CURLOPT_HEADER, 1);
        curl_setopt($curl, CURLINFO_HEADER_OUT, true);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYSTATUS, false);
        curl_setopt($curl, CURLOPT_COOKIEFILE, './cookie');
        curl_setopt($curl, CURLOPT_COOKIE, 'user_city='.urlencode($city));
        curl_setopt($curl, CURLOPT_REFERER, 'https://re-store.ru/');
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            'Content-Type: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
            'Connection: keep-alive',
            'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:97.0) Gecko/20100101 Firefox/97.0',
            'Accept-Language: en-US,en;q=0.9',
            'Pragma: no-cache',
            'Cache-Control: no-cache',
            'sec-ch-ua: "Opera";v="83", "Chromium";v="97", ";Not A Brand";v="99"',
            'sec-ch-ua-mobile: ?0',
            'sec-ch-ua-platform: "Windows"',
            'Sec-Fetch-Dest: document',
            'Sec-Fetch-Mode: navigate',
            'Sec-Fetch-Site: none',
            'DNT: 1',
        ));
        $out = curl_exec($curl);
        $headerSent = curl_getinfo($curl, CURLINFO_HEADER_OUT);
        curl_close($curl);
        return $out;
    }

    $o = get_discount('Краснодар');

    file_put_contents('./out', $o);

Вот так работает :)
Спасибо! Для меня оказался ключевым параметр - 'X-Requested-With: XMLHttpRequest' ☺️
 
Назад
Верх