Skip to content

Instantly share code, notes, and snippets.

@mach3
Created January 30, 2011 13:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mach3/77725d74919ace6f7475 to your computer and use it in GitHub Desktop.
Save mach3/77725d74919ace6f7475 to your computer and use it in GitHub Desktop.
WebページからフィードのURLを抽出する。(DOMDocument版)
<?php
function getFeedUrl( $url ){
$doc = DOMDocument::loadHTMLFile( $url );
if( empty( $doc ) ){ return false; }
$types = array(
"application/x.atom+xml",
"application/atom+xml",
"application/xml",
"text/xml",
"application/rss+xml",
"application/rdf+xml"
);
$feedUrl = null;
$links = $doc->getElementsByTagName("link");
for( $i = 0; $i < $links->length; $i ++ ){
$link = $links->item( $i );
if( !stristr( $link->getAttribute("rel"), "alternate" ) ||
!in_array( $link->getAttribute("type"), $types ) ){ continue; }
$feedUrl = $link->getAttribute("href");
if( !empty($feedUrl) ){ break; }
}
return $feedUrl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment