PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, February 26, 2022

[FIXED] How to get profile info from Facebook using Facebook-PHP-SDK

 February 26, 2022     facebook-php-sdk, php     No comments   

Issue

I am new to php and just downloaded Facebook php sdk and I am trying to write a very simple code to go to my personal Facebook retrieve the profile name and return it. But I keep getting an error. Here is what I got so far. I have already set up an account on Facebook Developers so I have an app id, app secret, and access token.

php file

<?php
require_once(__DIR__ . '/vendor/autoload.php');
$fb = new Facebook\Facebook([
  'app_id' => '{}',
  'app_secret' => '{}',
  'persistent_data_handler' => 'memory',
  'default_graph_version' => 'v3.3',
  ]);

try {
  // Returns a `Facebook\FacebookResponse` object
  $response = $fb->get('/me?fields=id,name', '{access-token}');
} catch(Facebook\Exceptions\FacebookResponseException $e) {
  echo 'Graph returned an error: ' . $e->getMessage();
  exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
  echo 'Facebook SDK returned an error: ' . $e->getMessage();
  exit;
}

$user = $response->getGraphUser();

echo 'Name: ' . $user['name'];
?>

Error Message

Graph returned an error: Invalid OAuth access token

Solution

That code works for me:

        $app_id = "your_app_id";
        $secret = "your_secret";
        $token = "your_token";
        $fb = new \Facebook\Facebook([
            'app_id' => $app_id,
            'app_secret' => $secret,
            'default_graph_version' => 'v3.2',
            'default_access_token' => $token, // optional
        ]);


Answered By - Sayakiss
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing