我正在为 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;
有趣的是,当我注释掉它起作用的瞬态时。