如何获得喜欢的Facebook页面数

Tom*_*Tom 0 facebook facebook-graph-api

我的以下脚本不再显示fb-page-like的数量了...

$ch = curl_init("http://graph.facebook.com/FACEBOOKPAGE");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$raw = curl_exec($ch);
curl_close($ch);
$data = json_decode($raw);
$scm_fb = $data->likes;
echo $scm_fb;
Run Code Online (Sandbox Code Playgroud)

我也尝试了以下代码,但这也什么也没显示...

$page_id = "FACEBOOKPAGE"; 
$likes = 0; //Initialize the count

//Construct a Facebook URL
$json_url ='https://graph.facebook.com/'.$page_id.'';
$json = file_get_contents($json_url);
$json_output = json_decode($json);

//Extract the likes count from the JSON object
if($json_output->likes){
    $likes = $json_output->likes;
}
Run Code Online (Sandbox Code Playgroud)

(我也尝试使用Facebook-Page-ID代替页面名称,结果相同)

有什么建议么?

350*_*50D 5

您可以使用以下php片段解析Facebook Like Widget源代码,从而获得任何图形对象或url

<?php

    $id = "youtubetabapp";
    // customize: "https://facebook.com/[customize]"    
    $url = "https://facebook.com/$id"; // by name
    //$url = "https://facebook.com/282374908483303"; // or by id

    // customize: "locale=[customize]&" to your needs
    $ch = curl_init("https://www.facebook.com/v2.5/plugins/like.php?locale=$locale&href=".$url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Safari');
    $html = curl_exec($ch);

    //die( var_dump( $html ) ); // <- debug fetched data
    // have a look for the data you need and customize your regex pattern accordingly
    // "preg_match([customize], $html, $match);"
    preg_match("/([0-9,.]+[a-zA-Z,\S]) people like this./", $html, $match);
    $likes = $match[1];
    //$likes = str_replace(array(".", "K"), array("", "000"), $likes); // <- customize output | remove ".", replace "K" with "000"
    //$likes = (float)$likes; // <- customize output | change type to float, strips prepending non-digits (eg: "K") as well
    die( $likes );

?>
Run Code Online (Sandbox Code Playgroud)

与API方法相比,您不需要提供任何令牌,只需一个请求即可。根据我的测试-您只需要保留locale参数以确保您具有用于解析的恒定源代码。