Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Use cURL and SimpleXML to retrieve and parse Wordpress RSS feed
<?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>
@daniellabirch

This comment has been minimized.

Copy link

commented Aug 28, 2015

This saved my day! Thanks for posting!

@pumamammal

This comment has been minimized.

Copy link

commented Jun 22, 2016

I love you, You just made my day, so customization. Let me contribute

@WilliamIsted

This comment has been minimized.

Copy link

commented Aug 2, 2016

Nice to find a simple working solution. Thanks

@fcava10

This comment has been minimized.

Copy link

commented Oct 1, 2016

Please,
how to display only "two posts"?
display the image Article
and
description with fewer characters
put the article link in the description

@fcavanha10

This comment has been minimized.

Copy link

commented Oct 1, 2016

Please,
how to display only "two posts"?
display the image Article
and
description with fewer characters
put the article link in the description

@xrobachok

This comment has been minimized.

Copy link

commented Oct 12, 2016

@fcavanha10,

how to display only "two posts"?

Go to http://your-blog-site-url-here/wp-admin/options-reading.php and select 2 items for news.

how to display the image Article and description with fewer characters put the article link in the description

Add Featured Image to RSS
Go to your theme folder and add next code to your functions.php file:

/**
 * Add Featured Image as a separate RSS item.
 *
 */
add_action('rss2_item', 'add_featured_image_node');

function add_featured_image_node() {
    global $post;
    if(has_post_thumbnail($post->ID)):
        $thumbnail = get_the_post_thumbnail( $post->ID );
        echo("  <featuredImage>{$thumbnail}</featuredImage>\n");
    endif;
}

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:

foreach ($xml->channel->item as $item) {
    echo '<h2><a href="' . $item->link . '">' . $item->title . '</a></h2>';
    echo '<img src="' . $item->featuredImage->img['src'] . '" width="100" height="100" alt="' . $item->featuredImage->img['src'] . '" class="' . $item->featuredImage->img['class'] . '">';
    echo '<p>' . $item->description . '<a href="' . $item->link . '">Read more: ' . $item->title . '</a></p>';
}

You can modify an output HTML code for your purposes and style it with CSS.

@leokovaski

This comment has been minimized.

Copy link

commented Apr 7, 2017

I'm wrong: Warning: Invalid argument supplied for foreach() in

@pumamammal

This comment has been minimized.

Copy link

commented Jul 2, 2017

I left coding a while because the project I was on broke. Now I'm back. seeing a code I fork amazes me. Thanks

@PascaleBeier

This comment has been minimized.

Copy link

commented Jul 4, 2017

CURLOPT_ENCODING does not actually mean what you think it means:

http://php.net/manual/de/function.curl-setopt.php

@ghost

This comment has been minimized.

Copy link

commented Oct 6, 2017

Amazing !!!
Very helpful script saving lots of work and server request.

@alyaspk

This comment has been minimized.

Copy link

commented May 17, 2018

Thank you so much, this has saved by day as well :)

@heyitsnovi

This comment has been minimized.

Copy link

commented Sep 26, 2018

Nice.... 100% working :) thank you 👍

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.