让Facebook像计数一样

时间:2012-02-13 05:22:17

标签: php facebook facebook-like

我已将Facebook Like集成到我的网站中。现在我需要在我的网站的管理面板中获取/列出Facebook的数量。有没有办法完成这项工作?需要建议。谢谢。

4 个答案:

答案 0 :(得分:10)

$url = 'http://graph.facebook.com/PAGE_OR_APP_ID';
echo '['.$url.']: '.json_decode(file_get_contents($url))->{'likes'};

答案 1 :(得分:8)

为了使用Facebook API获取任何对象的喜欢数量,请使用您选择的Facebook API以及如下命令:

 https://graph.facebook.com/< your object id>/

您将收到一个JSON对象,您可以从中提取喜欢的数量:

 {
 "id": "567454",
 "link": "http://www.facebook.com/pages/PHP-Developer/103146756409401",
 "likes": 250,
 "type": "page"
 }

有关https://developers.facebook.com/docs/reference/api/

的更多信息

在facebook上关于此主题的更直接/更新的链接是

https://developers.facebook.com/docs/reference/api/page/

答案 2 :(得分:0)

        var options = {};
        options['foo'] = 123;
        options['bar'] = 666;
        this.transitionToRoute(someRoute, {
            "queryParams": options
        });

}

function fbLikeCount($appid,$appsecret){
//Construct a Facebook URL
$json_url ='https://graph.facebook.com/'.$appid.'?access_token='.$appsecret;
$json = file_get_contents($json_url);
$json_output = json_decode($json);

//Extract the likes count from the JSON object
if($json_output->likes){
    return $likes = $json_output->likes;
}else{
    return 0;
}

答案 3 :(得分:0)

您可以使用Graph API(v3)实现此目标

function checkFacebookReactions($url){
    $access_token = 'YOUR-FACEBOOK-TOKEN'; // Generate a Facebook Token first

    $api_url = 'https://graph.facebook.com/v3.0/?id=' . urlencode( $url ) . '&fields=engagement&access_token=' . $access_token;
    $fb_connect = curl_init(); // initializing
    curl_setopt( $fb_connect, CURLOPT_URL, $api_url );
    curl_setopt( $fb_connect, CURLOPT_RETURNTRANSFER, 1 ); // return the result, do not print
    curl_setopt( $fb_connect, CURLOPT_TIMEOUT, 20 );

    $json_return = curl_exec( $fb_connect ); // connect and get json data
    curl_close( $fb_connect ); // close connection
    $body = json_decode( $json_return );

    // Print each key values, if needed
    foreach($body->engagement as $k=>$v){
        echo $k .": " .$v ."<br>";
    }

    $count = $body->engagement->reaction_count; // Return only reactions (likes + other reactions)
    return $count; 
}

$url = "REPLACE-WITH-YOUR-URL"; // The url you want to fetch details from
checkFacebookReactions($url);

首先,您将需要生成一个Facebook令牌。您可以找到大量资源来解释如何获取令牌。这将达到目的:https://elfsight.com/blog/2017/10/how-to-get-facebook-access-token