phpfacebookfacebook-graph-apifacebook-php-sdkfacebook-graph-api-v2.2

how can i use extracted data from facebook graph api


i maked a project, in this part of project i want to save gotten data from graph.facebook.com, now i the sample output of my project that i get from facebook graph api. :

Facebook\GraphObject Object
(
    [backingData:protected] => Array
        (
            [data] => Array
                (
                    [0] => stdClass Object
                        (
                            [message] => Automated XML Site Map Generator
http://pastebin.com/xjJe38dp
                            [id] => 103114753133019_371405176303974
                            [updated_time] => 2013-04-26T05:36:35+0000
                        )

                    [1] => stdClass Object
                        (
                            [message] => Simple but powerful DB class
http://pastebin.com/1qgxUrwX
                            [id] => 103114753133019_371404696304022
                            [updated_time] => 2013-04-26T05:34:23+0000
                        )

                    [2] => stdClass Object
                        (
                            [message] => Convert Existing DB to Unicode
http://pastebin.com/pHu08cPs
                            [id] => 103114753133019_371404609637364
                            [updated_time] => 2013-04-26T05:33:50+0000
                        )

                )

            [paging] => stdClass Object
                (
                    [previous] => https://graph.facebook.com/v2.2/103114753133019/feed?fields=message&since=1366954595&access_token=425591634259397|AQumfoxQyU3wAyt3sM37sYM9sp8&limit=25&__paging_token=enc_AexpCrJr7NTOG02uEaXs6pqjd11UjEohZJLjXZrWeYLOsE9hPX7WQTLemXIGMpzdFXEDdDUQj3qdwOqEbmlAfX4TREbZ-3GAfkKiUZ44kHGYLw&__previous=1
                    [next] => https://graph.facebook.com/v2.2/103114753133019/feed?fields=message&access_token=425591634259397|AQumfoxQyU3wAyt3sM37sYM9sp8&limit=25&until=1366954430&__paging_token=enc_AeyPm9mOsK3T9J0JNkIyQQqxfS7hLDe5GCs-IRLQWPOOzma8v9Rzvw8awxxE0GMQhx-rfs99X7TpUGw5f7HNgPnTKh11WbGC5Yj7GyW7s2VqoA
                )

        )

)

as you can see i print theese codes by running :

$request = new FacebookRequest( $session, 'GET', '/a page id/feed'.$sfield );

$response = $request->execute();
// get response
$graphObject = $response->getGraphObject();
// print data
echo '<pre>' . print_r( $graphObject, 1 ) . '</pre>';

so now i want to save messages to a file or a database when i want to use $graphObject as an array it gives me an error :

Fatal error: Cannot use object of type Facebook\GraphObject as array in /home/micengco/public_html/parser/facebook.php on line 34

so how i can do this ??


Solution

  • Objects have a different way of navigation than array. I think you will need to do something like this:

    $graphObject = $response->getGraphObject();
    $backingData = $graphObject->backingData;
    $data = $backingData[data][0];
    

    In such cases you need to read and comprehend the structure carefully. There will be associative and numerically indexed arrays in such data structures. What I am doing is above is first fetching the 'backingData' property of the object. Now under this property we have an associative array with a key 'data'. Under that there is a numerically index array that in your example has only one element. If there are multiple elements, you will have to run a loop.