V
VadikV
Есть адрес страницы продукта (прописан в скрипте).
Если включить CURLOPT_FOLLOWLOCATION, то код страницы нормально получается.
Если без CURLOPT_FOLLOWLOCATION делать редирект вручную - редирект проходит нормально до конца, но в конце выдается пустая страница. Замучался уже.
Зачем это нужно? Хост не дает использовать CURLOPT_FOLLOWLOCATION и исправить это нельзя.
Вот исходный код скрипта:
Если раскомментировать строку curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); то $s будет содержать код страницы.
Если оставить, как есть - $s пустая в конце.
Подскажите.
Если включить CURLOPT_FOLLOWLOCATION, то код страницы нормально получается.
Если без CURLOPT_FOLLOWLOCATION делать редирект вручную - редирект проходит нормально до конца, но в конце выдается пустая страница. Замучался уже.
Зачем это нужно? Хост не дает использовать CURLOPT_FOLLOWLOCATION и исправить это нельзя.
Вот исходный код скрипта:
PHP:
function redirect_exec($ch)
{
curl_setopt($ch, CURLOPT_HEADER, 1);
$data = curl_exec($ch);
$info = curl_getinfo($ch);
$http_code = $info['http_code'];
if ($http_code == 301 || $http_code == 302 || $http_code == 303)
{
list($header) = explode("\r\n\r\n", $data, 2);
$matches = array();
preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches);
$url = trim(array_pop($matches));
$url_parsed = parse_url($url);
if (isset($url_parsed['host']))
{
curl_setopt($ch, CURLOPT_REFERER, $info['url']);
curl_setopt($ch, CURLOPT_URL, $url);
return redirect_exec($ch);
}
}
elseif($http_code == 200)
{
$matches = array();
preg_match('/(<meta http-equiv=)(.*?)(refresh)(.*?)(url=)(.*?)[\'|"]\s*>/', strtolower($data), $matches);
$url = trim(array_pop($matches));
$url_parsed = parse_url($url);
if (isset($url_parsed['host'])) {
curl_setopt($ch, CURLOPT_REFERER, $info['url']);
curl_setopt($ch, CURLOPT_URL, $url);
return redirect_exec($ch);
}
}
return $info['url'];
}
$url = 'http://www.urbanoutfitters.com/urban/catalog/productdetail.jsp?id=17550583&navAction=jump&isProduct=true&parentid=MORE%20IDEAS&isProduct=true&cross-sell=true&guide-bn=true';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
// curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIESESSION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__) . '/cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__) . '/cookie.txt');
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; MRA 4.5 (build 01399))');
curl_setopt($ch, CURLOPT_URL, $url);
$s = @curl_exec($ch);
$info = curl_getinfo($ch);
$http_code = $info['http_code'];
if ($http_code == 301 || $http_code == 302 || $http_code == 303)
{
$url = redirect_exec($ch);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
$s = @curl_exec($ch);
}
curl_close($ch);
Если раскомментировать строку curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); то $s будет содержать код страницы.
Если оставить, как есть - $s пустая в конце.
Подскажите.