2012-04-21 9 views
0

私はフォロワーの現在のカウントのtwitterのapiに問い合わせるwordpressの私の最初のPHPクラスに取り組んでいます。私はこれを他のソーシャルメディアサイトにも拡大し、すべてのバグを解消する予定です。Twitterのフォロワーを数えるシンプルなPHPクラスですか?

問題は、私が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']); 
    } 
} 

OKので、私はクラスに若干の変更を加えただけでなく、それはRSS subscibersを含むように拡張し、Facebookは

が好きそれでも変な応答を取得して受け取ったいくつかの回答とヘルプに基づきます。ツイッターレスポンスは0、その他は機能が見つかりません。私はそれらのエラーチェックをコメントアウトするとき、私はまだ同じ応答を取得します。

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; 

興味深いことに、私はそれが動作過渡現象をコメントアウトときのようにそれを呼び出しています。

+2

何応答? – halfer

+1

コードをデバッグしようとしましたか? –

+0

そして、*意図された*応答は何ですか?たとえば、このクラスをどのようにインスタンス化していますか、どのように使用していますか? – Sampson

答えて

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

EDIT表示HERESに簡単な例:あなたは、以下の出力からわかるように がNO [ボディ]キーをされています

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)関数を使用しないでください、私はそれをコメントアウトしたので、私はその関数で作業することができました。あなたはまた、wp_remote_getが実際にfetch_twitter_countとして動作していることを確認する必要があり

は常に、少なくとも0

編集2返します。 をまた、私はあなたがちょうどあなたの代わりに戻って、同じようにそれを呼び出す必要がコンストラクタから、あなたの結果をエコー参照してください。

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

次に、あなたがhtmlの間でそれを必要とする場合は、問題が発生する場合があります、それをエコーし​​ようとしている他、あなたがそれをしたい、これまで$twitter_folowers->countをエコーすることができます。

編集4 - それをより多くのOOPを作るためのクラスにいくつかの変更だけではなく、いくつかのカプセル化された機能:あなたが受け_are_

<?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 
?> 
+0

@ローレンスCherone フェッチfunstionはスタンドアロン機能として正常に動作します。だから、私は問題が二枚目になってしまったのです。私はクラス内のエラーチェックから0を受け取ります。体の背後にある共鳴は、jsonレスポンス全体を必要としないInとして扱うデータの量を制限することです。実際には、フォロワー数をどのように引き出すことができるのかは、ボディのない配列のトップレベル属性としてカウントされます。$ count ['body'] ['followers_count']そうではありません。 – midaym

+0

また、エラーチェックは不可欠です。なぜなら、私はAPIからの正しいレスポンスを受け取れないので、失敗し、私のユーザーに私が嫌うエロルを提供し、エラーなしでテーマをオフラインで編集できるからです。 – midaym

+0

私の回答が更新されました –