0

I have the following PHP 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');

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

When I use the above code I decode the JSON string and get the following:

stdClass Object ( [data] => Array ( [0] => stdClass Object ( [category] => Media/news/publishing [name] => PAGE ABC [access_token] => abcdefghijklmnopqrstuvwxyz1234567890 [perms] => Array ( [0] => ADMINISTER [1] => EDIT_PROFILE [2] => CREATE_CONTENT [3] => MODERATE_CONTENT [4] => CREATE_ADS [5] => BASIC_ADMIN ) [id] => 35645645678735 ) [1] => stdClass Object ( [category] => Entertainment website [name] => Page 123 [access_token] => abcdefghijklmnopqrstuvwxyz1234567890 [perms] => Array ( [0] => ADMINISTER [1] => EDIT_PROFILE [2] => CREATE_CONTENT [3] => MODERATE_CONTENT [4] => CREATE_ADS [5] => BASIC_ADMIN ) [id] => 1364564569777454 )

How can I get the above into a PHP array? For example to be able to extract the access_token of page id 1364564569777454.

5
  • us2.php.net/json_decode. Note the second (optional) argument for json_decode. Commented Jan 13, 2014 at 18:29
  • $responsez->data[0]->id or responsez['data']['0']['id'] when $responsez = json_decode(file_get_contents($response), true) Commented Jan 13, 2014 at 18:31
  • @DaveChen excellent it worked. Instead of having data[0] would it be possible to have its absolute name rather than its relatgive e.g. data['XYZ']? Commented Jan 13, 2014 at 18:38
  • Do you mean this page name? [name] => PAGE ABC You'll want to do a loop, to return the array that has that specific attribute. Commented Jan 13, 2014 at 18:40
  • Instead of having responsez['data']['0']['id'] one would have responsez['data']['XYZ']['id'] i.e. 0 instead of XYZ - something static Commented Jan 13, 2014 at 18:43

3 Answers 3

2
responsez = json_decode(file_get_contents($response), true); 

The second param in json_decode allow convert returned object into associative arrays.

Sign up to request clarification or add additional context in comments.

Comments

0
 $arr = array();
 foreach($responsez->data as $obj){
      $arr[] = array("page_id"=>$obj->id,"access _token"=>$obj->access_token);         
 }
 print_r($arr); 

Comments

0

I was having troubles with the accounts JSON and this worked better for me:

        $request = (new FacebookRequest($session, 'GET', '/' . $this->userID . '/accounts'))->execute();
        $graphObject = json_decode($request->getRawResponse(), true);

        foreach ($graphObject['data'] as $page) { // array['data', 'paging']
            if ($page['id'] == $this->fbAppPageID) {
                $this->pageToken = $page['access_token'];
            }
        }
        return $this->pageToken;

Comments

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.