PHP - 使用 DOMDocument - 尝试查找指向提要的链接


PHP - Using DOMDocument - trying to find link to feed

我有一个使用这段代码的函数,我试图弄清楚我做错了什么。我想找到一个网页的 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');
  }
<?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);