0

I have a function using this bit of code and I am trying to figure out what I am doing wrong. I want to find a web page's rss feed if it has one. As of right now, it's not returning any URL, it shows the type, but that's it. And the blog_url key does not get set in the array. Here is the code:

  $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');
  }
Dan Lowe
  • 51,713
  • 20
  • 123
  • 112
Sean Webb
  • 1
  • 2

1 Answers1

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);
Alf Eaton
  • 5,226
  • 4
  • 45
  • 50