Rich Ezeikiel
Rich Ezeikiel

Reputation: 41

how can i extract specific data from facebook graph api array?

I have convert my object data to an array and now i am trying to extract certain parts from the multi dimensional array however i am having some problems. Assistance is appreciated, thank you.

 /* PHP SDK v4.0.0 */
/* make the API call */
$request = new FacebookRequest(
  $session,
  'GET',
  '/89647580016/feed'
);
$response = $request->execute();
$graphObject = $response->getGraphObject()->AsArray();
/* handle the result */

  // print data

 echo '<pre>' . print_r( $graphObject, 1 ) . '</pre>';

The following is the output:

Array
(
    [data] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 89647580016_10153019927930017
                    [from] => stdClass Object
                        (
                            [name] => Central Casting Los Angeles
                            [category] => Local Business
                            [category_list] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [id] => 176831012360626
                                            [name] => Professional Services
                                        )

                                )

                            [id] => 89647580016
                        )

                    [message] => ***NON Union Submissions***
Must be registered with Central Casting!

Jessica is currently booking a TV show working tomorrow Friday June 26th with a possible recall Monday June 29th in LA. These will be Night Calls, so you must be okay working late into the night.
She is looking for Caucasian looking men, who appear to be in their 30's-50's, who appear to be very upscale, who have business suits.

If this is you please call submission line 818-260-3952. Thank you!
                    [actions] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [name] => Comment
                                    [link] => https://www.facebook.com/89647580016/posts/10153019927930017
                                )

                            [1] => stdClass Object
                                (
                                    [name] => Like
                                    [link] => https://www.facebook.com/89647580016/posts/10153019927930017
                                )

                            [2] => stdClass Object
                                (
                                    [name] => Share
                                    [link] => https://www.facebook.com/89647580016/posts/10153019927930017
                                )

                        )

How can i print all the ['message'] ? I tried:

foreach ($graphObject as $key => $value) {
 echo '<br>'.$key['message'];
}

But i got a error. Thank you for your help.

Upvotes: 2

Views: 867

Answers (1)

Fabiano Araujo
Fabiano Araujo

Reputation: 932

Your array has some keys which elements are actually StdClass. You can't reffer to it as $key['message'] but as: $key->message.

Also, don't forget to include your error message. An error is extremely generic and we can't/won't help you if there is no indication of what is wrong.

I didn't notice your foreach. As you are iterating only on $graphObject first level, you will get as $key data and as value another whole array which each key has a StdClass. On your foreach, run it as foreach ($graphObject['data'] as $key => $value) , then use it as echo $value->message

Upvotes: 1

Related Questions