简体   繁体   English

cURL返回空白

[英]cURL returning blank

I'm trying the following code as I can't use php file_get_contents due to redirecting, but this is returning nothing: 我正在尝试以下代码,因为重定向不能使用php file_get_contents,但这没有返回任何内容:

function file_get_contents_curl($url) {
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

    $data = curl_exec($ch);

    curl_close($ch);

    return $data;
}


$url = 'http://www1.macys.com/shop/product/michael-michael-kors-wallet-jet-set-monogram-ziparound-continental?ID=597522&CategoryID=26846&RVI=Splash_5';
$html = file_get_contents_curl($url);
echo "html is ".$html;

Quickly looking at the output of: 快速查看输出:

curl --max-redirs 3 -L -v $url shows that 302 redirection redirects to same url .. while setting the cookies. curl --max-redirs 3 -L -v $url显示302重定向重定向到相同的URL ..同时设置cookie。

setting cookie jar seems to 'fix' it: 设置cookie jar似乎'修复'它:

curl -c /tmp/cookie-jar.txt --max-redirs 3 -L -i $url

So .. enable cookie (jar) 所以..启用cookie(jar)

Edit: 编辑:

You can add a line to your existing code to make it work: 您可以在现有代码中添加一行以使其正常工作:

curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__).DIRECTORY_SEPERATOR.'c00kie.txt');

You can use CURLOPT_VERBOSE option to debug your requests. 您可以使用CURLOPT_VERBOSE选项来调试您的请求。

Writes output to STDERR, or the file specified using CURLOPT_STDERR. 将输出写入STDERR,或使用CURLOPT_STDERR指定的文件。

$curl_debug_file = __DIR__.DIRECTORY_SEPARATOR.'curl.log';
$fh = fopen($curl_debug_file, "w+");
curl_setopt($handle, CURLOPT_STDERR, $fh);
curl_setopt($ch, CURLOPT_VERBOSE, true);

If you run 如果你跑

echo curl_error($ch);

right after curl_exec, you'll see this: 在curl_exec之后,你会看到:

Maximum (20) redirects followed 最多(20)次重定向

Find out why it's redirecting and where 找出它重定向的原因和位置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM