Created
April 17, 2013 16:26
-
-
Save betweenbrain/5405671 to your computer and use it in GitHub Desktop.
Use cURL and SimpleXML to retrieve and parse Wordpress RSS feed
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 | |
$curl = curl_init(); | |
curl_setopt_array($curl, Array( | |
CURLOPT_URL => 'http://blogs.guggenheim.org/map/feed/', | |
CURLOPT_USERAGENT => 'spider', | |
CURLOPT_TIMEOUT => 120, | |
CURLOPT_CONNECTTIMEOUT => 30, | |
CURLOPT_RETURNTRANSFER => TRUE, | |
CURLOPT_ENCODING => 'UTF-8' | |
)); | |
$data = curl_exec($curl); | |
curl_close($curl); | |
$xml = simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA); | |
//die('<pre>' . print_r($xml], TRUE) . '</pre>'); | |
?> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title></title> | |
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> | |
</head> | |
<body> | |
<?php foreach ($xml->channel->item as $item) { | |
$creator = $item->children('dc', TRUE); | |
echo '<h2>' . $item->title . '</h2>'; | |
echo '<p>Created: ' . $item->pubDate . '</p>'; | |
echo '<p>Author: ' . $creator . '</p>'; | |
echo '<p>' . $item->description . '</p>'; | |
echo '<p><a href="' . $item->link . '">Read more: ' . $item->title . '</a></p>'; | |
} | |
?> | |
</body> | |
</html> |
I'm wrong: Warning: Invalid argument supplied for foreach() in
I left coding a while because the project I was on broke. Now I'm back. seeing a code I fork amazes me. Thanks
CURLOPT_ENCODING does not actually mean what you think it means:
Amazing !!!
Very helpful script saving lots of work and server request.
Thank you so much, this has saved by day as well :)
Nice.... 100% working :) thank you 👍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@fcavanha10,
Go to http://your-blog-site-url-here/wp-admin/options-reading.php and select 2 items for news.
Add Featured Image to RSS
Go to your theme folder and add next code to your functions.php file:
Display Featured Image and other information you want
Copy the code from the first post at this page (see above gistfile1.php) and replace foreach cycle with next code:
You can modify an output HTML code for your purposes and style it with CSS.