我想打开网页的主页,并从它的HTML标记中提取标题和描述使用curl与PHP,我成功地做到了这一点,但许多网站在那里我无法打开。我的代码在这里:curl无法下载网页
function curl_download($Url){
if (!function_exists('curl_init')){
die('Sorry cURL is not installed!');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
// $url is any url
$source=curl_download($url);
$d=new DOMDocument();
$d->loadHTML($source);
$title=$d->getElementsByTagName("title")->item(0)->textContent)
$domx = new DOMXPath($d);
$desc=$domx->query("//meta[@name='description']")->item(0);
$description=$desc->getAttribute('content');
?>
此代码适用于大多数网站,但有很多人甚至无法打开。可能是什么原因?
当我尝试使用get_headers
函数获取那些网站的标题时,它的工作正常,但这些不是使用curl打开的。其中两个网站是blogger.com
和live.com
。
会发生什么情况?任何错误? – tiwo 2012-07-21 20:27:24
只是猜测......他们是否可能阻止卷曲(因为curl尊重'robots.txt',或者通过在curl的请求头上皱眉)服务器端? – tiwo 2012-07-21 20:27:38
没有错误,只是不返回任何内容,我试着直接使用file_get_contents函数打开,结果相同,但不能。 – Sourabh 2012-07-21 20:30:16