2017-02-10 142 views
0

我只是打印Facebook API resonse如何遍历这个对象PHP

print_r($response) 

输出

Facebook\GraphNodes\GraphEdge Object 
(
[request:protected] => Facebook\FacebookRequest Object 
    (
     [app:protected] => Facebook\FacebookApp 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] => Facebook\GraphNodes\GraphNode Object 
      (
       [items:protected] => Array 
        (
         [name] => SC Corinthians Paulista 
         [id] => 132769576762243 
        ) 

      ) 

     [1] => Facebook\GraphNodes\GraphNode Object 
      (
       [items:protected] => Array 
        (
         [name] => Miranda Cosgrove 
         [id] => 9934379006 
        ) 

我想访问[items:protected]阵列。但我不能找到如何让自己的

我试过

$ items_array = $ response-> items:protected;

但是,这是行不通的,请帮助我。我想获得该数组并重复它

+1

无法访问受保护的数据类型 –

+0

真的,没办法:

的线沿线的东西吗? @MASIDDIQUI – beginner

+0

我想你可以使用简单的'foreach'迭代项目,如下所示:https://developers.facebook.com/docs/php/GraphEdge/5.0.0 –

回答

1

如果它受到保护,这意味着您无法从外部访问。我建议你看一下Facebook \ FacebookRequest的类,如果有一个公共方法提供给你的项目,否则你不能访问这个。

1

Facebook的PHP API建议简单foreach得到节点:

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

GraphNode对象所描述here如何工作的。

0

你可以找到这个对象在Facebook的GitHub库的代码源:Facebook/GraphNodes/GraphEdge

GraphEdge延伸Collection它实现了ArrayAccess和IteratorAggregate

迭代器是由在$项目属性...

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

然后是的,使用简单的foreach应该可以工作

1

您可以使用以下几种不同的技术来遍历此对象:

(推荐技巧:)您可以使用GraphNode对象的方法通过循环访问GraphEdge对象来一次输出项目,以获取GraphNode并调用'asJson ()”或‘asArray()’就可以了:

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

    //Print as array 
    print_r($node->asArray()); 
} 

还有其他的方法来访问GraphNode对象的属性,如‘如果你想只得到数据的特定项目getfield命令’和‘getFieldNames’和不想退还整件事(你可以在这里阅读:https://developers.facebook.com/docs/php/GraphNode/5.0.0

(也工作:)或者你可以使用嵌套的foreach循环(按照文档在这里:https://developers.facebook.com/docs/php/GraphEdge/5.0.0)。

foreach ($response as $node) { 
    //each item returned by response 
    foreach ($node as $itemKey => $itemValue) { 
     //each item within the node 
     print $itemKey.' is '.$itemValue; 
    } 
}