卷曲错误:最多(20)次重定向

时间:2017-01-27 07:05:09

标签: php curl web-scraping

在向myntra尝试CURL时,它给出了错误。我试图通过DOMDOCUMENT获取提取细节,但它给出了同样的错误:

  

最多(20)次重定向

这是我的代码:

<?php
        $url = 'http://www.myntra.com/sports-shoes/nike/nike-men-black-dart-12-msl-running-shoes/1547908/buy?src=search&uq=false&q=nike&p=1';
        $ch  = curl_init($url);
        //curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)");
        curl_setopt($ch, CURLOPT_AUTOREFERER, true);
        curl_setopt($ch, CURLOPT_HEADER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FAILONERROR, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Cookie: test=cookie"));

        $cl  = curl_exec($ch);
        if(curl_exec($ch) === false)
        {
                echo 'Curl error: ' . curl_error($ch);
                echo 'Curl error: ' . curl_errorno($ch);
        }else{
           $dom = new DOMDocument();
           $xpath = new DOMXpath($dom);
           print_r($xpath);            
        }
?>

3 个答案:

答案 0 :(得分:0)

添加一些cookie文件。

curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__) . '/cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__) . '/cookie.txt');

应该可以。

答案 1 :(得分:-1)

<?php

$url = 'http://www.myntra.com/sports-shoes/nike/nike-men-black-dart-12-msl-running-shoes/1547908/buy?src=search&uq=false&q=nike&p=1';
$ch  = curl_init($url);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:47.0) Gecko/20100101 Firefox/47.0");
$request_headers = [
                'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8;',
                'Accept-Encoding: gzip, deflate',
                "Connection: keep-alive",
                "Content-Type: text/html; charset=UTF-8",

            ];
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
curl_setopt($ch, CURLOPT_ENCODING, "");
$cl  = curl_exec($ch);
$h = curl_getinfo($ch);
$e = curl_error($ch);
curl_close($ch);
var_dump($cl);

有时您必须发送请求标头,然后按照编码进行解码。

答案 2 :(得分:-5)

对此

使用CURLOPT_MAXREDIRS选项
curl_setopt($ch, CURLOPT_MAXREDIRS , 1000);

我希望它有效,祝你好运!