Im trying to build a function which grabs the feedburner “readers” using wp_remote_get()
. I noticed that it frequently returned a value of 0
.
I assumed at first that it was a WordPress error (handled by is_wp_error()
) or a flaw with wp_remote_get()
. Wrong of-course..
Feedburner just kept crashing, so I used a second transient to store a result (never 0
) with an expiration of 7 days. The part which i cant get my head around is handling errors with is_wp_error()
. I need to force an error so i can handle it properly, before I put it up on production.
Heres an illustration:
$result = wp_remote_get( 'http://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=' . urlencode($username) );
if ( is_wp_error($result) )
return false;
Whats the best way to force an error? Should i use new WP_error()
?
WordPress can be inconsistent as to when is returns a
WP_Error
object and when it just returnsfalse
orstring(0)
when actually there was an error. I am not sure exactly what feedburner is returning that is not triggering aWP_Error
fromwp_remote_get()
– but if you knowwp_remote_get()
will return an WP_Error, I would just set$result = new WP_Error( 'my-error' );
This is the same object thatwp_remote_get()
will return on error.To grab Feedburner stats, I’ve always used
wp_cache_get
. Here’s a function I’ve had success withTo return the value, I use this
That being said, Feedburner is horribly unreliable. What I’d recommend doing is building in a conditional that if the number = 0, then it returns a static number.