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

Friday, February 11, 2022

[FIXED] Facebook PHP 4.x return array parse

 February 11, 2022     arrays, facebook, facebook-graph-api, facebook-php-sdk, php     No comments   

Issue

Hey all I am just now getting around to the newest version of the PHP sdk for Facebook. It seems to use a new array of returned data that I can not seem to be able to parse out what I need and not all other gibberish it has contained inside the array.

The array returned looks like this:

Array (
    [data] => Array (
        [0] => stdClass Object (
            [id] => 439676913452345546787863523525
            [name] => John Doe
            [picture] => stdClass Object (
                [data] => stdClass Object (
                    [is_e] =>
                    [url] => https://blah.com....
                )
            )
        )
        [1] => stdClass Object (
            [id] => 56594790468026754634524674
            [name] => Bob Barker
            [picture] => stdClass Object (
                [data] => stdClass Object (
                    [is_e] =>
                    [url] => https://blah.com....
                )
            )
        )
        [2]...etc etc...
        [paging] => stdClass Object ( 
            [cursors] => stdClass Object ( 
                [before] => QWFMmhllXN5JH....Rk52QWc= 
                [after] => WGHtdnNIwaDlRz....05RNB0E= 
            ) 
        )
    )
)

In the older version of the Facebook SDK I just needed to do something along these lines (below code example is getting the id within the returned array and checking also to make sure it has a value or not):

foreach($theReturnedArray['data'] as $rData) {
  $id = (isset($rData['id']) ? $rData['id'] : null);
  $name = (isset($rData['name']) ? $rData['name'] : null);

  echo $name . ' ' . $id . '<br />';
}

in order to get the value that I needed. Now the return has an added stdClass object within the array itself and I am unsure on how to go about getting id, name & picture url from it.

Any help would be awesome!

ANSWER

It was caused of an error in my code that I was getting 0's back (the +):

 echo $name . ' ' . $id + '<br />';

And I needed to change it to:

 echo $name . ' ' . $id . '<br />';

and the full code together:

 foreach($graphObject['data'] as $rData) {
    $id = (isset($rData->id) ? $rData->id : null);
    $name = (isset($rData->name) ? $rData->name : null);
    $picUrl = (isset($rData->picture->data->url) ? $rData->picture->data->url : null);

    echo $id . '<br/>';
    echo $name . '<br/>';
    echo $picUrl . '<br/>';
    echo '======================================<br/>';
 }

Solution

You get values from objects with ->

echo $rData->id;     // This format is for objects

And not

echo $rData['id'];   // This format is for arrays

So your code would now look like

$id = isset($rData->id) ? $rData->id : NULL;
$name = isset($rData->name) ? $rData->name : NULL;


Answered By - Hanky Panky
  • 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