计算推特关注者的简单php类?

计算推特关注者的简单php类?,php,oop,twitter,count,Php,Oop,Twitter,Count,我正在为wordpress开发我的第一个php类,在这个类中,我查询twitter api以获取当前的追随者数量。一旦我解决了所有的漏洞,我计划将其扩展到其他社交媒体网站 问题是我是OOP和wordpress小部件开发的新手,没有收到预期的响应 class razor_SocialCount { private $user; private $api; public $count; function __construct($user, $api) { $this->user =

我正在为wordpress开发我的第一个php类,在这个类中,我查询twitter api以获取当前的追随者数量。一旦我解决了所有的漏洞,我计划将其扩展到其他社交媒体网站

问题是我是OOP和wordpress小部件开发的新手,没有收到预期的响应

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']));
}

}
我已经让它在没有缓存的情况下工作了,它只是那个部分,我想它可能已经完成了

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和其他函数。当我注释掉这些错误检查时,仍然得到相同的响应

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;

有趣的是,当我注释掉瞬变时,它是有效的。

不确定为什么您要使用:
$json['body']
访问数组,请删除它,我认为您可以开始了

下面是一个显示followers\u计数的快速示例

<?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 
?>
我不是说不要使用
is\u wp\u error($json)
函数,我把它注释掉了,这样我就可以使用这个函数了

您还需要检查
wp\u remote\u get
是否实际工作,因为
fetch\u twitter\u count
将始终至少返回0

编辑2: 我还看到您只是从构造函数中回显您的结果,您应该返回并调用它,就像:

然后你可以在任何你想要的地方回显
$twitter\u folowers->count
,否则如果你在html之间需要回显,你可能会遇到麻烦

编辑4-对类进行一些更改,使其更面向对象,而不仅仅是一些封装的函数:

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


您收到了什么样的响应?您是否尝试过调试代码?预期的响应是什么。例如,你是如何实例化这个类的,以及你是如何使用它的?@matt调试它的php是什么意思?我没有收到任何错误或启用了eror all的警告,如果这是你的意思的话。@halfer我得到了返回的0,就好像它们是api的错误一样。如果(is_wp_error($json))返回0;@Lawrence Cherone fetch funstion作为一个独立函数运行良好。这就是为什么我对问题的所在感到困惑。我从类内的错误检查中收到0。正文背后的原因是限制我处理的数据量,因为我不需要整个json响应。事实上,我真的不知道你是如何将追随者计数作为数组的顶级属性的,因为如果没有body,它将是$count['body']['followers\u count']错误检查也很重要,因为如果我没有从api收到正确的响应,它将失败,并向我的用户提供我不想要的erorr,它还允许对主题进行离线编辑,不会出现错误。好吧,很高兴知道,我没有意识到json的phps解码并没有维护json提要的相同Heiarch。至于它本身的功能,我将不得不与wp_remote_玩一玩,看看它是否工作正常。还是不确定为什么它在私有方法中不起作用。这可能是问题所在,wp_remote_get在类中没有作用域,可能使用curl或file_get_内容,非常确定wp_remote_get没有FGC或curl不能做的特殊或独特的功能,它也将帮助您在将来将代码移植到其他CMS。好吧,最后有时间测试您的建议,原因是什么['body']调用是我得到了一个错误,没有说decode只接受数组。我相信我遇到的问题也源于暂时性的,好像我注释了那些行,它可以工作,但没有缓存。