私はフォロワーの現在のカウントの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;
興味深いことに、私はそれが動作過渡現象をコメントアウトときのようにそれを呼び出しています。
何応答? – halfer
コードをデバッグしようとしましたか? –
そして、*意図された*応答は何ですか?たとえば、このクラスをどのようにインスタンス化していますか、どのように使用していますか? – Sampson