문제

first of all I want to apologize if this has been answered or if there's already a useful link.

I have created a facebook app with secret and id, I've been through the authentification procedure as well. My aim is to get Insight Data of one of the user's pages. As you may see in the code, up to now I only made it to retrieve the user's name (with a little help of the facebook dev tutorial. So example question: How can I get the page likes of to be echoed by my code? Thank you very much in advance!

<?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>';
}
?>
도움이 되었습니까?

해결책

If you want only the page likes, you'll need only this:

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

Replace http://graph.facebook.com/facebook by the page id or username: example:

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top