0

私はワードプレス用の最初のphpクラスに取り組んでおり、TwitterAPIに現在のフォロワー数を問い合わせています。すべてのバグを修正したら、これを他のソーシャルメディアサイトにも拡張する予定です。

問題は、私がOOPとワードプレスウィジェットの開発に不慣れであり、意図した応答を受け取っていないことです。

私はそれをキャッシュせずに動作させることができました。それはおそらくそれを通り抜けていると思います。

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のいいねを含むように拡張しました

しかし、それでも奇妙な応答があります。twitterresponseis0などはFunctionNotFoundです。これらのエラーチェックをコメントアウトしても、同じ応答が返されます。

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

なぜアレイにアクセスするのかわからない:$json['body']それを削除すれば、うまくいくと思います。

これが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 
?>

編集: 以下の出力からわかるように、[body]キーはありません:

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] => 
)

関数を使用しないと言っているのではなく、is_wp_error($json)関数を操作できるようにコメントアウトしました。

常に少なくとも0を返すため、wp_remote_get実際に機能していることも確認する必要がありますfetch_twitter_count

編集2:また、コンストラクターからの結果をエコーし​​ているのがわかります。代わりに、次のように呼び出す必要があります。

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

次に、必要な$twitter_folowers->count場所にエコーできます。エコーしようとすると、html間で必要な場合に問題が発生する可能性があります。

編集4-いくつかのカプセル化された関数だけでなく、より多くのoopにするためのクラスへのいくつかの変更:

<?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 に答える