我只想使用 curl 加载 head 的内容,目前我正在使用
<?php
$url="www.facebook.com";
$title='';$keywords='';$description='';
$ch = curl_init();
$timeout=5;
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, 'http://'.$url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_TIMEOUT,0);
$html = curl_exec($ch);
curl_close($ch);
echo htmlspecialchars($html);//gives the complete source.Why?
//parsing begins here:
$doc = new DOMDocument();
@$doc->loadHTML($html);
$nodes = $doc->getElementsByTagName('title');
$metas = $doc->getElementsByTagName('meta');
if($nodes->length>0)$title = $nodes->item(0)->nodeValue;
for ($i = 0; $i < $metas->length; $i++)
{
$meta = $metas->item($i);
if($meta->getAttribute('name') == 'description')
$description = $meta->getAttribute('content');
if($meta->getAttribute('name') == 'keywords')
$keywords = $meta->getAttribute('content');
}
echo $title. '<br/>';
echo " $description". '<br/>';
echo " $keywords";
?>
此代码返回 url 的完整代码,但我只想要 head。不要将它与我之前的问题联系起来,因为这里没有必要使用 curlopt_writefunction()