我想用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 - 变量区分大小写 –