如何从facebook图形api数组中提取特定数据?

时间:2015-06-25 23:50:11

标签: php arrays facebook facebook-graph-api

我已经将我的对象数据转换为数组,现在我试图从多维数组中提取某些部分但是我遇到了一些问题。感谢您的帮助,谢谢。

 /* 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>';

以下是输出:

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
                                )

                        )

如何打印所有['消息']?我试过了:

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

但是我收到了一个错误。谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

你的数组有一些键,其元素实际上是StdClass。您无法像$key['message']那样对其进行审核,但是:$key->message

另外,请勿忘记包含错误消息。错误非常通用,如果没有任何错误迹象,我们无法帮助您。

我没有注意到你的foreach。由于您只在$graphObject第一级进行迭代,因此您将获得$key个数据和另一个整数数组,每个键都有一个StdClass。在foreach上,将其设为foreach ($graphObject['data'] as $key => $value),然后将其用作echo $value->message