从Facebook解析JSON提要

时间:2016-01-20 13:54:48

标签: php json facebook facebook-graph-api

我正在创建一个Facebook应用程序,我从以下代码的用户帖子中获得了大约50个提要

try {
        $response = $fb->get('/me/feed?fields=likes{name},message,story,comments{from},from&limit=50');

    } catch(Facebook\Exceptions\FacebookResponseException $e) {

    } catch(Facebook\Exceptions\FacebookSDKException $e) {

    }
    $total_posts = array();
    $posts_response = $response->getGraphEdge();
    $response_array = $posts_response->asJson();

我在$response_array中得到以下结果:

[{"message":"testing...","from":{"name":"Ahsan Omer","id":"1060152144046414"},"id":"1060152144046414_1060590920669203","likes":[{"name":"Ahsan Omer","id":"1060152144046414"}],"comments":[{"from":{"name":"Ahsan Omer","id":"1060152144046414"},"id":"1060590920669203_1060622407332721"}]},{"message":"testing...","from":{"name":"Ahsan Omer","id":"1060152144046414"},"id":"1060152144046414_1060530170675278","comments":[{"from":{"name":"Ahsan Omer","id":"1060152144046414"},"id":"1060530170675278_1060622427332719"}]}]

我想列出谁已发布,谁喜欢以及谁在时间轴供稿中发表了评论,并且需要解析上述回复,我是否正确地采取了这种方式?

1 个答案:

答案 0 :(得分:0)

你可以在网上很容易地查找这个问题(谷歌是你的朋友),但无论如何你在这里使用未经测试的,可能是语法无效的解决方案,但它会帮助你!

json_decode 将解码对数组的响应(由于$ post_response-> asJson()返回的字符串不是数组,因此这里的命名具有误导性。使用 $解码[0] [消息] 您将访问第一个数组行和列消息

$decoded = json_decode($response_array, true),
echo $decoded[0][message]