Skip to content

Instantly share code, notes, and snippets.

@mach3 mach3/getfeedurl.php Secret
Created Jan 30, 2011

Embed
What would you like to do?
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
You can’t perform that action at this time.