0

我有一个使用这段代码的函数,我试图找出我做错了什么。我想找到一个网页的 RSS 提要(如果有的话)。截至目前,它没有返回任何 URL,它显示了类型,但仅此而已。并且 blog_url 键没有在数组中设置。这是代码:

  $results = array();
  $doc = new DOMDocument();
  @$doc->preserveWhiteSpace = FALSE;
  $html = file_get_contents($url);
  $doc->loadHTML("$html");

  $links = $doc->getElementsByTagName('link');
  foreach ($links as $tag) {
    $type = $tag->getAttribute('type');
    if (preg_match("/(rss+xml|atom+xml')/si", $type))
      $href_text = $tag->nodeValue;
      if(preg_match("/('feed|journal|blog')/si", $href_text))
        $results['blog_url'] = $tag->getAttribute('href');
  }
4

1 回答 1

0
<?php

$url = ''; // EDIT THIS

$doc = new DOMDocument;
@$doc->loadHTMLFile($url);

$xpath = new DOMXPath($doc);
$nodes = $xpath->query('head/link[@rel="alternate"][@type="application/atom+xml" or @type="application/rss+xml"][@href]');

$result = array();

foreach ($nodes as $node) {
    $href = $node->getAttribute('href');
    if (preg_match('/(feed|journal|blog)/si', $href)) {
        $result['blog_url'] = $href;
        break;
    }
}

print_r($result);
于 2013-10-15T08:30:14.813 回答