我正在开发我的第一个用于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 subscibers和facebook喜欢
但仍然得到奇怪的回应。 twitter responseis 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;
有趣的是,当我评论它有效的瞬态时。
答案 0 :(得分: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)
函数,我对它进行了评论,以便我可以使用该函数。
您还需要检查wp_remote_get
实际上是否正常工作fetch_twitter_count
将始终返回至少0
编辑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
?>