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

问题描述 投票:0回答:1

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

我已经创建了一个带有秘密和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>';
}
?>
php facebook facebook-graph-api
1个回答
0
投票

如果您只想要喜欢的页面,则只需要这个:

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

http://graph.facebook.com/facebook 替换为页面 ID 或用户名: 示例:

© www.soinside.com 2019 - 2024. All rights reserved.