【问题标题】:PHP cUrl 0 content length, no errorsPHP cUrl 0 内容长度,没有错误
【发布时间】:2012-08-29 02:05:56
【问题描述】:

我有这个 cUrl 请求有时有效,然后停止工作。我正在回显标题和任何错误,http 显示 200 ok 并且没有错误,但内容长度为 0。

$userAgent = 'Googlebot/2.1 (http://www.googlebot.com/bot.html)';
$cslbnum = "881126";
$target_url =      "https://www2.somewebsite.aspx?LicNum=" .   $cslbnum;
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_REFERER,  "https://www2.somesite.aspx");
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
if(curl_exec($ch) === false)
{
echo 'Curl error: ' . curl_error($ch);
}
else
{
echo 'Operation completed without any errors';
}
$html = curl_exec($ch);
$html = @mb_convert_encoding($html, 'HTML-ENTITIES', 'utf-8');
curl_close( $ch );

echo $html;

有什么想法会导致这种情况吗?我发誓今天早上我醒来它工作,下班回家,什么都没有。直接测试网站,运行良好。

【问题讨论】:

    标签: php curl


    【解决方案1】:

    这是您代码的最后一点 - 您调用了两次 curl_exec。

    // ...
    $html = curl_exec($ch);
    if($html === false)
    {
    echo 'Curl error: ' . curl_error($ch);
    }
    else
    {
    echo 'Operation completed without any errors';
    }
    $html = @mb_convert_encoding($html, 'HTML-ENTITIES', 'utf-8');
    curl_close( $ch );
    echo $html;
    

    另外,请考虑避免使用@。

    【讨论】:

    • 谢谢,我没听懂,我已经搞砸了好几个小时了。仍然显示内容长度为 0 并且不显示页面,非常令人沮丧
    • @savagenoob 你的网址应该是https://www2.somewebsite.com/default.aspx?LicNum=吗?
    • 不,但我不想在论坛上发帖...我可以发给你,有没有办法在这里发帖?也许是个人电子邮件?
    • @savagenoob 我有办法防止垃圾邮件,所以请在非公共服务器上执行这个脚本,它会给你我的电子邮件地址。 代码: <?php $string = 'c2hhbm9xdWluKGRvdCkyKGF0KWxpc3BhbXZlKGRvdCljb20='; $email = base64_decode($string); $email = str_replace('(at)', '@', $email); $email = str_replace('(dot)', '.', $email); $email = str_replace('no', '', $email); $email = str_replace('spam', '', $email); echo $email; ?>
    猜你喜欢
    • 2016-03-17
    • 1970-01-01
    • 2019-07-10
    • 2017-11-23
    • 1970-01-01
    • 2014-04-20
    • 1970-01-01
    • 2011-12-08
    • 2016-04-23
    相关资源
    最近更新 更多