如果您通过浏览器访问此链接(不需要):
http://podbor-akkumulyatora.ru/site/submit/?filter_brand=1&filter_model=1&filter_generation=1&filter_engine=12532
您将被重定向
http://podbor-akkumulyatora.ru/audi/100/4--c4-1990-1994/s4-4.2--280-hp/
但是,如果您首先通过PHP cURL 链接,您将被重定向到其他链接:
http://podbor-akkumulyatora.ru/audi/100/4--c4-1990-1994/
(最后没有s4-4.2--280-hp/
!)。
为什么在使用cURL时,外部服务器返回不同 位置(在响应标题中)比通过浏览器连接?
我试着解决这整天(真的)。所有请求标头都是正确的,我将它们与发送浏览器的标头进行了比较。
我的代码:
<?php
echo 'Final URL is: <br>' . curl_query('http://podbor-akkumulyatora.ru/site/submit/?filter_brand=1&filter_model=1&filter_generation=1&filter_engine=12532')[2];
function curl_query($url, $ajax = false, $POST = null) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
if ($POST) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $POST);
}
curl_setopt($ch, CURLOPT_HEADER, false);
$cookies = dirname(__FILE__) . '/akb/cookie.txt';
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookies);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookies);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 YaBrowser/17.11.0.2191 Yowser/2.5 Safari/537.36');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
$headers = array( // Additional headers
'Accept: '.($ajax ? 'application/json, text/javascript, */*; q=0.01' : 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8'),
'Accept-Encoding: gzip, deflate',
'Accept-Language: ru,en;q=0.9',
'Connection: keep-alive',
'DNT: 1',
'Host: podbor-akkumulyatora.ru',
'Upgrade-Insecure-Requests: 1',
);
if ($ajax) {
$headers[] = 'X-Requested-With: XMLHttpRequest';
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_REFERER, 'http://podbor-akkumulyatora.ru/');
$debug = false;
if ($debug) {
ob_start();
$out = fopen('akb/debug.txt', 'w');
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_STDERR, $out);
}
$data = curl_exec($ch);
$http_code = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
$final_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
if ($debug) {
fclose($out);
$debug = ob_get_clean();
}
curl_close($ch);
return array($data, $http_code, $final_url);
}