php访问facebook洞察数据通过图表


php-access facebook insight data via graph

首先,如果有人回答了这个问题,或者已经有一个有用的链接,我想道歉。

我已经创建了一个带有secret和id的facebook应用程序,我也已经通过了认证程序。我的目标是获得一个用户页面的洞察数据。正如您在代码中看到的那样,到目前为止,我只让它检索用户的名称(在facebook开发教程的帮助下)。因此,示例问题:我如何使我的代码响应的页面喜欢?提前非常感谢!

<?php 
session_start();
require "vendor/autoload.php";
use Facebook'FacebookSession;
use Facebook'FacebookRedirectLoginHelper;
use Facebook'FacebookRequest;
use Facebook'FacebookResponse;
use Facebook'FacebookSDKException;
use Facebook'FacebookRequestException;
use Facebook'FacebookAuthorizationException;
use Facebook'GraphObject;
use Facebook'GraphSessionInfo;
use Facebook'GraphUser;
$appid = "numbers"; // my AppID
$secret = 'numbersandletters'; // my secret
//Session
FacebookSession::setDefaultApplication($appid ,$secret);
//Login
$helper = new FacebookRedirectLoginHelper(           'http://localhost:85/phplearning/facebook/facebook2.php' );
try {$session = $helper->getSessionFromRedirect();} 
catch( FacebookRequestException $ex ) {echo $ex;} 
catch( Exception $ex ) {echo $ex;}
// active session?
if( isset($_SESSION['token']))
{
$session = new FacebookSession($_SESSION['token']); 
try{$session->Validate($appid ,$secret);}
catch( FacebookAuthorizationException $ex)
{$session ='';}
}
if ( isset( $session ) ) 
{   
$_SESSION['token'] = $session->getToken();
 try {
     $user_profile = (new FacebookRequest(
     $session, 'GET', '/me'
     ))->execute()->getGraphObject(GraphUser::className());
     echo "Name: " . $user_profile->getName();
     } catch(FacebookRequestException $e) {
    echo "Exception occured, code: " . $e->getCode();
    echo " with message: " . $e->getMessage();
 } 
} 
else 
{
  echo '<a href="' . $helper->getLoginUrl() . '">Login</a>';
}
?>

如果你只想要页面点赞,你只需要这个:

$data = file_get_contents('http://graph.facebook.com/facebook');
$data = json_decode($data);
echo $data['likes'];

用页面id或用户名替换http://graph.facebook.com/facebook:例子:

  • http://graph.facebook.com/dferreira.pt
  • http://graph.facebook.com/4funsociety