最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

transient - is_wp_error() and handling errors

programmeradmin0浏览0评论

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( '.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()?

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()?

Share Improve this question asked May 20, 2011 at 8:14 VicePrezVicePrez 6785 silver badges15 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 6

WordPress can be inconsistent as to when is returns a WP_Error object and when it just returns false or string(0) when actually there was an error. I am not sure exactly what feedburner is returning that is not triggering a WP_Error from wp_remote_get() - but if you know wp_remote_get() will return an WP_Error, I would just set $result = new WP_Error( 'my-error' ); This is the same object that wp_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 with

function get_feedburner_stats() {
  $fbrefreshtime = 43200;  //Refresh Feedburner twice in a day

  $fb = wp_cache_get('fbstats_key');

  if ($fb == false) {
    $yourfeeduri = 'YOUR_FEEDBURNER_USERID'; 
    $feed = 'https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri='.$yourfeeduri;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $feed);
    $feed = curl_exec($ch); curl_close($ch);
    $xml = new SimpleXMLElement($feed);
    $fb = $xml->feed->entry['circulation']; 

    //Set the Value in Cache
    wp_cache_set('fbstats_key', strval($fb), '', $fbrefreshtime);
  }
  return $fb;
} 

To return the value, I use this

<?php echo get_feedburner_stats(); ?>

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.

发布评论

评论列表(0)

  1. 暂无评论