curl_exec不起作用

时间:2013-10-11 17:28:55

标签: php windows curl

我有这段代码:

$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');

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

当我运行代码时,它会返回111而没有任何错误。 我希望它能够返回页面内容和1

2 个答案:

答案 0 :(得分:1)

不要因固定的错误消息而死亡。尝试

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

代替。卷毛告诉你为什么它失败了,而不只是吐出一些毫无意义的111文本。

答案 1 :(得分:1)

尝试类似手册中的示例:

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

相关问题