-
-
Save mach3/77725d74919ace6f7475 to your computer and use it in GitHub Desktop.
WebページからフィードのURLを抽出する。(DOMDocument版)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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