Skip to content

Instantly share code, notes, and snippets.

@dannyconnolly
Created September 19, 2016 23:06
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 dannyconnolly/f76cd992c5dff1d7f914c3cf01163531 to your computer and use it in GitHub Desktop.
Save dannyconnolly/f76cd992c5dff1d7f914c3cf01163531 to your computer and use it in GitHub Desktop.
parse wp feed
<?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