为什么我无法使用CURL获取网站内容 [英] Why I can't get website content using CURL
本文介绍了为什么我无法使用CURL获取网站内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我真的很困惑。我的问题是我不能得到网站内容使用curl
我试图显示结果,但它总是相同的结果,它总是返回一个空字符串..
I'm really confused. My problem is I can't get website content using curl I've tried to display the result but it always the same result, it always return an empty string..
这是我的函数:
function get_html_content($url, $timeout=10) {
// fake user agent
$userAgent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2) Gecko/20070219 Firefox/2.0.0.2';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$string = curl_exec($ch);
curl_close($ch);
return $string;
}
上面的代码有什么问题,因为每当我尝试上面的代码,它总是返回一个空字符串。
Is there anything wrong with my code above, because whenever I try the above code, it always return an empty string.
推荐答案
如注释中所述,添加
CURLOPT_PROXY
问题。
这篇关于为什么我无法使用CURL获取网站内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文