我想用PHP做一個簡單的網絡爬蟲,我有問題得到給定的URL的HTML源代碼。我目前使用cURL來獲取源代碼。cURL沒有得到URL的HTML源代碼
我的代碼:
$url = "http://www.nytimes.com/";
function url_get_contents($Url) {
if (!function_exists('curl_init')) {
die('CURL is not installed!');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
if ($output === false) { die(curl_error($ch)); }
curl_close($ch);
return $output;
}
echo url_get_contents($url);
?>
眼下沒有得到迴應,而且沒有任何錯誤,所以它是一個有點神祕。任何建議或修正可以理解
編輯:我加入
如果($輸出===假){模具(curl_error($ CH)); }
給函數的中間,它結束了給我一個錯誤(最後!):
無法解析主機:www.nytimes.com
我還是真的不知道是什麼問題是。有任何想法嗎?
由於
你從來沒有打擾檢查curl是否成功。 'if($ output === false){die(curl_error($ ch)); }' –
http://stackoverflow.com/questions/6516902/how-to-get-response-using-curl-in-php應該有所幫助。 – Scalable
'$ Url!= $ url' also - 變量區分大小寫 –