dsuxcxqep31023992 2012-05-26 15:32
浏览 23
已采纳

使用curl加载头部内容

I want to load the content of head using curl only ,currently iam using

<?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 "&nbsp;&nbsp;&nbsp;&nbsp;$description". '<br/>';
echo "&nbsp;&nbsp;&nbsp;&nbsp;$keywords";
?>

This code is returning the complete code of url but i want only of head.Don't relate it with my previous question as here it is not necessary to use curlopt_writefunction()

  • 写回答

2条回答 默认 最新

  • duansha8764 2012-05-27 16:16
    关注

    Despite the similarity of the names, HEADER doesn't correspond with the html <head>, nor does BODY correspond with the html <body>. CURLOPT_HEADER means include the http headers in the return value. CURLOPT_NOBODY means do not include the http payload in the return value(the payload for an http response with content-type:text/html would be the entire html document).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件
  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan