0

我正在为 wordpress 开发我的第一个 php 类,在其中我查询 twitter api 以获取当前的关注者数量。一旦我解决了所有错误,我计划将其扩展到其他社交媒体网站。

问题是我是 OOP 和 wordpress 小部件开发的新手,没有收到预期的响应。

我已经让它在没有缓存的情况下工作,我认为这可能是通过它的那一部分。

class razor_SocialCount {
    function __construct($user, $api) {
           echo $this->razor_social_count_api($user, $api);
    }

    private function razor_social_count_api($user, $api) {
        if (empty($user) || empty($api)) return false;

        if(false === ($count = get_transient($api.'_recent_count') ) ) {

            switch($api) {
                case ('twitter'):
                    $count = $this->fetch_twitter_count($user);
                break;

                default:
                    $count = 'Function not found.';
                break;
            }

            set_transient($api.'_recent_count', $count, (60 * 60 * 3));
        }

        return number_format(doubleval($count));
    }

    private function fetch_twitter_count($user) {
        $json = wp_remote_get("http://api.twitter.com/1/users/show.json?screen_name=$user");

        if(is_wp_error($json)) return 0;

        $count = json_decode($json['body'], true);

        return intval($count['followers_count']);
    }
}

好的,所以根据我收到的一些回复和帮助,我对课程进行了一些细微的改动,并将其扩展为包括 rss 订阅者和 facebook 喜欢

但仍然得到奇怪的反应。twitter 响应为 0,其他为 Function Not Found。当我注释掉这些错误检查时,我仍然得到相同的响应。

class razor_SocialCount {
private $user;
private $api;
public $count;

function __construct($user, $api) {
    $this->user = $user;
    $this->api = $api;
    return $this->razor_social_count_api();
}

private function razor_social_count_api() {
    if (empty($this->user) || empty($this->api)) return false;

    switch($this->api) {
        case ('facebook'):
            if(false === ($this->count = get_transient('facebook_recent_count'))) {
                $this->count = $this->fetch_facebook_count();
                set_transient('facebook_recent_count', $this->count, (60*60*3));
            }
        break;

        case ('twitter'):
            if(false === ($this->count = get_transient('twitter_recent_count'))) {
                $this->count = $this->fetch_twitter_count();
                set_transient('twitter_recent_count', $this->count, (60*60*3));
            }
        break;

        case ('rss'):
            if(false === ($this->count = get_transient('rss_recent_count'))) {
                $this->count = $this->fetch_rss_count();
                set_transient('rss_recent_count', $this->count, (60*60*3));
            }
        break;

        default:
            $this->count = 'Function not found.';
        break;
    }
}

private function fetch_facebook_count() {
    $json = wp_remote_get("http://graph.facebook.com/$this->user");
    if(is_wp_error($json)) return 0;

    $json = json_decode($json['body'], true);

    return number_format(intval($json['likes']));
}

private function fetch_twitter_count() {
    $json = wp_remote_get("http://api.twitter.com/1/users/show.json?screen_name=$this->user");
    //if(is_wp_error($json)) return 0;

    $json = json_decode($json['body'], true);

    return number_format(intval($json['followers_count']));
}

private function fetch_rss_count() {
    $xml = wp_remote_get("http://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=$this->user");
    //if(is_wp_error($xml)) return 0;

    $xml = new SimpleXMLElement($xml['body']);
    return number_format(intval($xml->feed->entry['circulation']));
}

}

我这样称呼它

$facebook = new razor_SocialCount('midaymcom','facebook');
$twitter = new razor_SocialCount('midaym','twitter');
$feed = new razor_SocialCount('midaym','rss');

echo $facebook->count;
echo $twitter->count;
echo $feed->count;

有趣的是,当我注释掉它起作用的瞬态时。

4

1 回答 1

1

Not sure why your accessing the array with: $json['body'] remove that and I think your good to go.

Heres a quick example that displays the followers_count

<?php
function fetch_twitter_count($user) {
    $json = file_get_contents("http://api.twitter.com/1/users/show.json?screen_name=$user");

    if(is_wp_error($json)) return 0;

    $count = json_decode($json, true);

    return intval($count['followers_count']);
}


echo  number_format(fetch_twitter_count('jhon')); //1,155 
?>

EDIT: As you can see from the output below there is NO [body] key:

Array
(
    [id] => 6405412
    [id_str] => 6405412
    [name] => Jhon van der Ceelen
    [screen_name] => Jhon
    [location] => Amsterdam - The Netherlands
    [description] => GTD-er, Digital marketeer for over 17 years. Working @MindshareNL - Business Planning Director Digital and Client Leader Digital Unilever and KPN
    [url] => http://www.mindshare.nl
    [protected] => 
    [followers_count] => 1156
    [friends_count] => 471
    [listed_count] => 13
    [created_at] => Tue May 29 07:00:11 +0000 2007
    [favourites_count] => 29
    [utc_offset] => 3600
    [time_zone] => Amsterdam
    [geo_enabled] => 
    [verified] => 
    [statuses_count] => 921
    [lang] => nl
    [status] => Array
        (
            [created_at] => Sat Apr 21 09:05:36 +0000 2012
            [id] => 1.9362652702652E+17
            [id_str] => 193626527026524160
            [text] => Mobile Marketing Nederland | Blog: Mobile Advertising groeit (nu echt) http://t.co/ugjwisnO
            [source] => LinkedIn
            [truncated] => 
            [in_reply_to_status_id] => 
            [in_reply_to_status_id_str] => 
            [in_reply_to_user_id] => 
            [in_reply_to_user_id_str] => 
            [in_reply_to_screen_name] => 
            [geo] => 
            [coordinates] => 
            [place] => 
            [contributors] => 
            [retweet_count] => 0
            [favorited] => 
            [retweeted] => 
            [possibly_sensitive] => 
        )

    [contributors_enabled] => 
    [is_translator] => 
    [profile_background_color] => 9AE4E8
    [profile_background_image_url] => http://a0.twimg.com/profile_background_images/19025441/Binary_TV_Photo_psd.jpg
    [profile_background_image_url_https] => https://si0.twimg.com/profile_background_images/19025441/Binary_TV_Photo_psd.jpg
    [profile_background_tile] => 1
    [profile_image_url] => http://a0.twimg.com/profile_images/24825272/Jhon_normal.jpg
    [profile_image_url_https] => https://si0.twimg.com/profile_images/24825272/Jhon_normal.jpg
    [profile_link_color] => 0000FF
    [profile_sidebar_border_color] => 87BC44
    [profile_sidebar_fill_color] => E0FF92
    [profile_text_color] => 000000
    [profile_use_background_image] => 1
    [show_all_inline_media] => 1
    [default_profile] => 
    [default_profile_image] => 
    [following] => 
    [follow_request_sent] => 
    [notifications] => 
)

Im not saying dont use the is_wp_error($json) function, I commented it out so I could work with the function.

You need to also check that wp_remote_get is actually working as fetch_twitter_count will always return at least 0

Edit 2: Also I see your just echoing your result from the constructor you should return instead and call it like:

<?php $twitter_folowers = new razor_SocialCount('account','twitter'); ?>

Then you can echo $twitter_folowers->count where ever you want it, else trying to echo it you may have trouble if you need it between html.

Edit 4 - Some changes to the class to make it more oop and not just some encapsulated functions:

<?php 
class razor_SocialCount {
    private $user;
    private $api;
    public $count;

    function __construct($user, $api) {
        $this->user = $user;
        $this->api = $api;
        return $this->razor_social_count_api();
    }

    private function razor_social_count_api() {
        if (empty($this->user) || empty($this->api)) return false;

        if(false === ($this->count = get_transient($this->api.'_recent_count'))) {

            switch($this->api) {
                case ('twitter'):
                    $this->count = $this->fetch_twitter_count();
                    break;

                default:
                    $this->count = 'Function not found.';
                    break;
            }

            set_transient($this->api.'_recent_count', $this->count, (60 * 60 * 3));
        }
    }

    private function fetch_twitter_count() {
        $json = file_get_contents("http://api.twitter.com/1/users/show.json?screen_name=$this->user");
        if(is_wp_error($json)) return 0;

        $json = json_decode($json, true);
        return number_format(intval($json['followers_count']));
    }
}

$twitter_count = new razor_SocialCount('jhon','twitter');

echo $twitter_count->count; //1,156
?>
于 2012-04-21T15:49:04.010 回答