[英]Why I can't get website content using CURL
我真的很困惑 我的問題是我無法使用curl獲取網站內容,但我嘗試顯示結果,但結果始終相同,總是返回一個空字符串。
這是我的功能:
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;
}
我上面的代碼有什么問題嗎,因為每當我嘗試上面的代碼時,它總是返回一個空字符串。
如評論中所述,添加
CURLOPT_PROXY
使用適當的代理設置可以解決該問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.