skip to Main Content

I just print facebook api resonse

print_r($response)

output is

FacebookGraphNodesGraphEdge Object
(
[request:protected] => FacebookFacebookRequest Object
    (
        [app:protected] => FacebookFacebookApp Object
            (
                [id:protected] => secret
                [secret:protected] => secret
            )

        [accessToken:protected] => secret
        [method:protected] => GET
        [endpoint:protected] => /search?q=co&type=page
        [headers:protected] => Array
            (
                [Content-Type] => application/x-www-form-urlencoded
            )

        [params:protected] => Array
            (
            )

        [files:protected] => Array
            (
            )

        [eTag:protected] => 
        [graphVersion:protected] => v2.5
    )

[metaData:protected] => Array
    (
        [paging] => Array
            (
                [cursors] => Array
                    (
                        [before] => MAZDZD
                        [after] => MjQZD
                    )

                [next] => https://graph.facebook.com/v2.8/search?access_token=EAAafvw8PPA4BACHY8V6GDpbzMbtRlZC7dZCRnOGtO26Yc4g4yWWvqZCsMBPOWO3b72n2JPjXP8KD91ZCMXMAcARGUsk5cNShhy5LxOmj0Gz4ZA2ESzPZAd4VzBCpdZATCZBvZCOkAIxBd1gXBzkMY0DheyjruSlMHEPbbuVuTY350wgZDZD&q=co&type=page&limit=25&after=MjQZD
            )

    )

[parentEdgeEndpoint:protected] => 
[subclassName:protected] => 
[items:protected] => Array
    (
        [0] => FacebookGraphNodesGraphNode Object
            (
                [items:protected] => Array
                    (
                        [name] => SC Corinthians Paulista
                        [id] => 132769576762243
                    )

            )

        [1] => FacebookGraphNodesGraphNode Object
            (
                [items:protected] => Array
                    (
                        [name] => Miranda Cosgrove
                        [id] => 9934379006
                    )

I want to access [items:protected] array .But i can’t find how to get their

I tried

$items_array = $response->items:protected;

But this is not working , please help me. I want to get that array and iterate over it

4

Answers


  1. If it is protected, it means you can’t access from the outside. I d suggest you to have a look in the class FacebookFacebookRequest if there is a public method that is providing you items, else you can’t access to this.

    Login or Signup to reply.
  2. Facebook php api suggests simple foreach to get nodes:

    foreach ($response as $graphNode) {
        print_r($graphNode);
    }
    

    How to work with GraphNode object described here.

    Login or Signup to reply.
  3. You can find the code source of this object in the facebook github repo : Facebook/GraphNodes/GraphEdge

    GraphEdge extends Collection which implements ArrayAccess and IteratorAggregate

    Iterator is made on $items properties…

    /**
     * Get an iterator for the items.
     *
     * @return ArrayIterator
     */
    public function getIterator()
    {
        return new ArrayIterator($this->items);
    }
    

    Then yes, using a simple foreach should work

    Login or Signup to reply.
  4. You can traverse this object using a couple of different techniques:

    (Recommended Technique:) You can use the GraphNode object’s methods to output the items all at once by looping through the GraphEdge object to get the GraphNode and calling ‘asJson()’ or ‘asArray()’ on it:

    foreach($response as $node){
        //Print as json
        print($node->asJson());
    
        //Print as array
        print_r($node->asArray());
    }
    

    There are other methods for accessing the GraphNode object properties such as ‘getField’ and ‘getFieldNames’ if you want to only get a specific item of data and don’t want to return the whole thing (you can read about them here: https://developers.facebook.com/docs/php/GraphNode/5.0.0)

    (Also works:) Or you can use nested foreach loops (as per docs here: https://developers.facebook.com/docs/php/GraphEdge/5.0.0).

    Something along the lines of:

    foreach ($response as $node) {
        //each item returned by response
        foreach ($node as $itemKey => $itemValue) {
            //each item within the node
            print $itemKey.' is '.$itemValue;
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search