1

I have the following PHP code :

<?php
require_once 'library/facebook.php';
include '../constants.php';

define('TOKEN_FILE', 'fb_page_accestoken.txt'); // dont need to change this
$fb = new Facebook(array(
  'appId' => FB_APP_ID,
    'secret' => FB_APP_SECRET,
    'cookie' => true,
));

$access_token = file_get_contents('fb_app_token.txt');

$response  = "https://graph.facebook.com/me/accounts?access_token=".$access_token;
echo $response;

?>

The above creates a URL to access my Facebook Graph API. When I copy it and paste it in the browser url I get the following:

{
   "data": [
      {
         "category": "Media/news/publishing",
         "name": "My Sandbox",
         "access_token": "abcdefghijklmnopqrstuvwxyz123456",
         "perms": [
            "ADMINISTER",
            "EDIT_PROFILE",
            "CREATE_CONTENT",
            "MODERATE_CONTENT",
            "CREATE_ADS",
            "BASIC_ADMIN"
         ],
         "id": "123456789"
      },

How can I extract particular information such as the access_token value in my php file?

Thank you

12
  • Hi, I was aware of that. When I tried var_dump(json_decode($response)); var_dump(json_decode($response, true)); Nothing worked Commented Jan 13, 2014 at 10:45
  • what your $response shows ? Commented Jan 13, 2014 at 10:56
  • It displays nothing - I get the following error PHP Notice: Trying to get property of non-object in ****** on line 26 Commented Jan 13, 2014 at 11:00
  • Use this $response = json_decode(file_get_contents("your url end point",true); and then print_r($response); see what you get Commented Jan 13, 2014 at 11:03
  • Also you need to have manage_pages permission for the user for which you are trying to access the data Commented Jan 13, 2014 at 11:21

2 Answers 2

1

Be aware, that you are appending the URL to $response.. not the content! Do:

$response = file_get_contents("http..");

& use:

$response = json_decode($response);

to get the response as a php object. You can then access your values normally:

$access_token = $response->access_token;
Sign up to request clarification or add additional context in comments.

4 Comments

Hi, when I echo $response->access_token; nothing is being displayed after following your modificaitons.
do var_dump($response); die; to see the content of the variable.. what does it output?
Now its giving me null
Make sure you replaced the "http://.." with your complete url + request token. var_dump this before using json_decode.. it should contain the json object.
1

Ok Your code needs to be this way, you are still not doing json_decode($jsondata,true); Here is the correct code.

define('TOKEN_FILE', 'fb_page_accestoken.txt'); // dont need to change this
$fb = new Facebook(array(
  'appId' => FB_APP_ID,
    'secret' => FB_APP_SECRET,
    'cookie' => true,
));

$access_token = file_get_contents('fb_app_token.txt');
$responsez = file_get_contents("https://graph.facebook.com/me/accounts?access_token=".$access_token);
$response  =  json_decode($responsez,true);

The above will return an array.

From that array you can display the access token as

$data =   $response['data'];
echo $data['access_token'];

or directly as

echo $response['data']['access_token'];

If you do not want json_decode() as array then just do as

define('TOKEN_FILE', 'fb_page_accestoken.txt'); // dont need to change this
    $fb = new Facebook(array(
      'appId' => FB_APP_ID,
        'secret' => FB_APP_SECRET,
        'cookie' => true,
    ));

    $access_token = file_get_contents('fb_app_token.txt');
    $responsez = file_get_contents("https://graph.facebook.com/me/accounts?access_token=".$access_token);
    $response  =  json_decode($responsez);

echo $response->data->access_token ;

You can also use the Graph api calls as

define('TOKEN_FILE', 'fb_page_accestoken.txt'); // dont need to change this
$fb = new Facebook(array(
  'appId' => FB_APP_ID,
    'secret' => FB_APP_SECRET,
    'cookie' => true,
));

$access_token = file_get_contents('fb_app_token.txt');
$params = array(
    'access_token' => $access_token
   );

$response = $fb->api('me/accounts','GET',$params);

Your response will have the data

NOTE : Make sure that this api end point requires manage_pages permission

https://developers.facebook.com/docs/graph-api/reference/user/accounts/

6 Comments

Hi, it gives me PHP Notice: Array to string conversion in and the output as Array
This line gives you what Please post it $responsez = file_get_contents("graph.facebook.com/me/…);
I have created a test page how json encode and decode works codepad.org/Mjb9A8as
I have updated the answer again you can do as $responsez = file_get_contents("graph.facebook.com/me/…); $response = json_decode($responsez); echo $response->data->access_token ;
Hi, did you update it as your code is like mine exactly. Thanks
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.