Created
September 19, 2016 23:06
-
-
Save dannyconnolly/f76cd992c5dff1d7f914c3cf01163531 to your computer and use it in GitHub Desktop.
parse wp feed
This file contains hidden or 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 | |
$article_string = file_get_contents( $url ); | |
$article_string = preg_replace_callback('/<!\[CDATA\[(.*)\]\]>/', 'filter_xml', $article_string); | |
$article_xml = simplexml_load_string($article_string); | |
$namespaces = $article_xml->getNamespaces(true); // get namespaces | |
function filter_xml($matches) { | |
return trim(htmlspecialchars($matches[1])); | |
} | |
// | |
// foreach ($article_xml->channel->item as $item) { | |
// echo '<pre>'; | |
// print_r($item); | |
// echo '</pre><hr />'; | |
// } | |
$feed = $article_xml; | |
// echo $feed->channel->title.'<br />'; | |
// echo $feed->channel->link.'<br />'; | |
// echo $feed->channel->description.'<br />'; | |
// echo $feed->channel->lastBuildDate.'<br />'; | |
$i = 0; | |
foreach ($feed->channel->item as $article) { | |
if($i == 2) | |
die(); | |
echo 'Link: ' . $article->link.'<br />'; | |
echo 'Title: ' . $article->title.'<br />'; | |
echo 'Category: ' . $article->category.'<br />'; | |
echo 'Pb Date: ' . $article->pubDate.'<br />'; | |
echo 'Desc: ' . $article->description.'<br />'; | |
echo $article->children('http://purl.org/rss/1.0/modules/content/')->encoded; | |
foreach ($article->enclosure->attributes() as $key => $value) { | |
echo $key . ' - ' . $value.'<br />'; | |
} | |
echo 'Pb Date: ' . date('F jS, Y', strtotime($article->pubDate)).'<br /><hr />'; | |
$dc = $article->children('http://purl.org/dc/elements/1.1/'); | |
echo '<pre>'; | |
var_dump($dc); | |
echo '</pre>'; | |
$i++; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment