无法获得facebook分享数

时间:2016-01-12 17:41:44

标签: php facebook

我正在尝试获取Facebook的分享网址。也许问题是数据是在不同的时间聚合而不是瞬时聚合但是我的搜索结果为0。

这是我的代码:

<?php
class Sharecount
{
    private $url;
    private $timeout;

    public function __construct($url,$timeout=10)
    {
        $this->url = rawurlencode($url);
        $this->timeout = $timeout;
    }

    public function getFacebookCount()
    {
        $json_string = $this->curlRequest('http://api.facebook.com/restserver.php?method=links.getStats&format=json&urls='.$this->url);
        $json = json_decode($json_string, true);
        return isset($json[0]['total_count'])?intval($json[0]['total_count']):0;
    }

    private function curlRequest($url)
    {
        $ch=curl_init();
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_FAILONERROR,1);
        curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch,CURLOPT_TIMEOUT,$this->timeout);
        $cont=curl_exec($ch);
        if(curl_error($ch)){
            die(curl_error($ch));
        }
        return $cont;
    }
}

这是我的结果:

string(264) "[{"url":"http:\/\/url","normalized_url":"http:\/\/url","share_count":0,"like_count":0,"comment_count":0,"total_count":0,"click_count":0,"comments_fbid":null,"commentsbox_count":0}]" 

我不想提供网址,所以可能与网址有关

0 个答案:

没有答案