如何使用php提取url的标题和描述


How to extract Title and description of a url using php

我试图提取url的标题,描述和关键字。下面的代码对于单行描述可以正常工作,但对于多行描述则不行。有人能帮帮忙吗?如果我能知道如何提取图像也像社会书签网站。

<?php
  $url = "http://www.apnatimepass.com";
  $fp = fopen($url, 'r');
  $content = "";
  while(!feof($fp)) {
        $buffer = trim(fgets($fp, 4096));
        $content .= $buffer;

    }

  $start = '<title>';
  $end = '</title>';
  preg_match('!<title>(.*?)</title>!i', $content, $match);
  $title = $match[1];
  $metatagarray = get_meta_tags($url);
  $keywords = $metatagarray["keywords"];
  $description = $metatagarray["description"];
  echo " <div><strong>URL: </strong >$url</div> 'n";
  echo  "<div><strong>Title:</strong>$title</div>'n";
  echo " <div><strong>Description: </strong >$description</div>'n";
  echo " <div><strong>Keywords: </strong >$keywords</div>'n";
  ?>

你可以这样做

$doc = new DOMDocument;
$doc->loadHTMLFile('http://urlhere.com');
$title = $doc->getElementsByTagName('title');
$title = $title[0];
$xpath = new DOMXPath($doc);
$description = $xpath->query('/html/head/meta[name@="description"]/@content');
$keywords = $xpath->query('/html/head/meta[name@="keywords"]/@content');