PHP cUrl 0 内容长度,没有错误

标签 php curl

我的 cUrl 请求有时有效,然后停止工作。我正在回显 header 和任何错误,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;

有什么想法可能会导致这种情况吗?我发誓今天早上我醒来时一切正常,下类回家后什么也没有。直接测试该网站,工作正常。

最佳答案

这是代码的最后一部分 - 您调用了 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;

此外,请考虑避免使用@

关于PHP cUrl 0 内容长度,没有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12169979/

相关文章:

PHP - 如何将 Curl 与 Kerberos 一起使用

php - Curl 返回空字符串

php - 短网址的 file_get_contents()

php - 可扩展的网络应用程序

javascript - AngularJS $http.post 上的错误 access-control-allow-origin

javascript - 如何使用 Php 中的选择选项获取 GridView 中的数据

javascript - 从 javascript 调用多次时,PHP cURL 返回旧数据

php - MySql:选择查询

php - 干 : how to use this code in several entities accross Symfony2 project? 特征?

php - utf8 到 ISO-8859-1 无法通过 Curl 正确转换某些字符