【问题标题】:cURL returning blankcURL 返回空白
【发布时间】:2012-09-06 05:10:18
【问题描述】:

我正在尝试以下代码,因为由于重定向,我无法使用 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;

【问题讨论】:

  • 你试过 curl_getinfo() 和 curl_error() 看看他们报告了什么?

标签: php curl file-get-contents


【解决方案1】:

快速查看以下输出:

curl --max-redirs 3 -L -v $url 显示 302 重定向重定向到相同的 url .. 同时设置 cookie。

设置 cookie jar 似乎可以“修复”它:

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

所以 .. 启用 cookie (jar)

编辑:

您可以在现有代码中添加一行以使其工作:

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

【讨论】:

  • 非常感谢,添加了以下内容,现在一切正常(给 cookie.txt 777 权限)$cookie_file = "/home/***/cookiejar_keep/cookie.txt"; curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
【解决方案2】:

您可以使用CURLOPT_VERBOSE 选项来调试您的请求。

将输出写入 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);

【讨论】:

  • 看来你打错了,应该是 curl_setopt($ch, CURLOPT_STDERR, $fh);对吗?
【解决方案3】:

如果你跑了

echo curl_error($ch);

在 curl_exec 之后,你会看到:

最多 (20) 次重定向

找出重定向的原因和位置

【讨论】:

  • 我跑了同样的事情。我没有错误。虽然运行 print_r(curl_getinfo($ch)); 会显示单个重定向到相同的 URL。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-25
  • 2018-12-23
  • 2017-11-20
  • 1970-01-01
  • 2011-05-27
  • 2018-11-27
  • 1970-01-01
相关资源
最近更新 更多