Handling timeouts with the Wordpress RSS Feed Object and SimplePie
In later versions of WordPress they have implemented their own RSS feed object which is basically just a wrapper for the well known RSS reader plugin SimpliePie.
On some of my own WordPress websites I need to make use of some RSS feeds to show results from various places such as horse racing results.
I first try and obtain an RSS results feed from Betfair and if that fails I then resort to a backup from the BHA.
However sometimes the first feed loads rather slowly and I end up with the lower quality feed due to the WordPress object timing out after 10000 milliseconds.
Even though I cache the results I require the first load of the feed to take however long it needs to - up to 30 seconds if it needs the time but due to the 10 second timeout I often don't get the first feed loaded into my cache.
Even after looking at the WordPress RSS feed object there seemed to be no easy way to pass in a timeout parameter into an overloaded constructor therefore the feed was always timing out after 10 seconds.
For all I know there is a way to set the HTTP load timeout with the WordPress RSS object but as of yet I haven't found it plus I haven't had the time or inclination to scour the WordPress codebase looking for it.
The code I was using for my first attempt is below.
However as you can see, although the RSS call is wrapped in an error object and I can pass in the maximum number of items I want to get. However I cannot pass in a timeout parameter.
Therefore instead of waiting for ages for a fix from anyone from WordPress I decided to jump straight over the WordPress wrapper object and go straight to the underlying SimpliePie object to get my data.
Not only does this mean no calls to wrapper objects that only ultimately point to SimplePie anyway but I have much more control over the parameters that are used for my feed collection.
I still use the 2 feeds and resort to my backup feed if I cannot get any items from my Betfair call but I now use the following code for my first attempt.
As you can see the ELSE branch just outputs an error message and this is just an example of how you can access the error if you need to. I don't use it on my site but it is useful to know if you are not using the WordPress error object.
The HTTP load timeout is easily set with the following line of code:
Since I have changed my first attempt at loading the feed to skip the WordPress wrapper object with a call to SimplePie I haven't had any problems at all.
You might find this helpful if you are doing your own work with RSS feeds and WordPress.
In later versions of WordPress they have implemented their own RSS feed object which is basically just a wrapper for the well known RSS reader plugin SimpliePie.
On some of my own WordPress websites I need to make use of some RSS feeds to show results from various places such as horse racing results.
I first try and obtain an RSS results feed from Betfair and if that fails I then resort to a backup from the BHA.
However sometimes the first feed loads rather slowly and I end up with the lower quality feed due to the WordPress object timing out after 10000 milliseconds.
Even though I cache the results I require the first load of the feed to take however long it needs to - up to 30 seconds if it needs the time but due to the 10 second timeout I often don't get the first feed loaded into my cache.
Even after looking at the WordPress RSS feed object there seemed to be no easy way to pass in a timeout parameter into an overloaded constructor therefore the feed was always timing out after 10 seconds.
For all I know there is a way to set the HTTP load timeout with the WordPress RSS object but as of yet I haven't found it plus I haven't had the time or inclination to scour the WordPress codebase looking for it.
The code I was using for my first attempt is below.
$rss = fetch_feed('http://rss.betfair.com/RSS.aspx?format=rss&sportID=7');
// Checks that the object is created correctly
if (!is_wp_error( $rss ) ) :
// Figure out how many total items there are, but limit it to 10.
$maxitems = $rss->get_item_quantity(10);
// Build an array of all the items, starting with element 0 (first element).
$rss_items = $rss->get_items(0, $maxitems);
endif;
However as you can see, although the RSS call is wrapped in an error object and I can pass in the maximum number of items I want to get. However I cannot pass in a timeout parameter.
Therefore instead of waiting for ages for a fix from anyone from WordPress I decided to jump straight over the WordPress wrapper object and go straight to the underlying SimpliePie object to get my data.
Not only does this mean no calls to wrapper objects that only ultimately point to SimplePie anyway but I have much more control over the parameters that are used for my feed collection.
I still use the 2 feeds and resort to my backup feed if I cannot get any items from my Betfair call but I now use the following code for my first attempt.
$feed = new SimplePie();
$feed->set_feed_url('http://rss.betfair.com/RSS.aspx?format=rss&sportID=7');
// set timeout to 30000 milliseconds (30 seconds)
$feed->set_timeout(30);
// set number of items to return to 10
$feed->set_item_limit(10);
$feed->set_stupidly_fast(true);
// enable caching and set the duration to cache for in seconds (200 seconds)
$feed->enable_cache(true);
$feed->set_cache_duration(200);
$feed->init();
$feed->handle_content_type();
if (!$feed->error()):
// get up to 10 items
$rss_items = $feed->get_items(0, 10);
// if there are less than 10 items get the actual number in case I need to know
$maxitems = count($rss_items);
else:
// error output it
echo "Error is " . $feed->error();
endif;
As you can see the ELSE branch just outputs an error message and this is just an example of how you can access the error if you need to. I don't use it on my site but it is useful to know if you are not using the WordPress error object.
The HTTP load timeout is easily set with the following line of code:
$feed->set_timeout(30);
Since I have changed my first attempt at loading the feed to skip the WordPress wrapper object with a call to SimplePie I haven't had any problems at all.
You might find this helpful if you are doing your own work with RSS feeds and WordPress.