0

我有这段代码:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $page_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

curl_setopt($ch, CURLOPT_USERAGENT, 
  'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);

curl_setopt($ch, CURLOPT_FAILONERROR, true);

$header[] = "Accept: text/html, text/*";
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

$html = curl_exec($ch) or die('111');
curl_close($ch);

 echo $html;
 exit('1');

我安装了最新版本的 Xampp 并启用了 curl 的 Windows。

当我运行代码时,它返回111没有任何错误。我期待它返回页面内容和1

4

2 回答 2

2

尝试类似手册中的示例:

if(($html = curl_exec($ch)) === false)
{
    echo 'Curl error: ' . curl_error($ch);
    die('111');
}
curl_close($ch);
echo $html;
exit('1');

http://php.net/manual/en/function.curl-error.php

于 2013-10-11T17:32:34.673 回答
1

不要因为固定的错误信息而死。尝试

$result = curl_exec($ch);
if ($result === FALSE) {
   die(curl_error($ch));
}

反而。让 curl 告诉你为什么它失败了,而不是仅仅吐出一些毫无意义的111文本。

于 2013-10-11T17:32:29.980 回答