foreach中的参数无效

时间:2012-11-20 14:57:45

标签: php facebook-graph-api foreach json

我正在使用以下内容从FB中检索评论:

$url = urlencode("http://*****.com/pages/view?id=2153&item=Mens-Collection-Shoes");
$request_url ="https://graph.facebook.com/comments/?ids=" . $url;
$requests = file_get_contents($request_url);

当我print_r($requests);时,我得到以下内容,但无法解决如何使用foreach循环格式化它:

{
    "http:\/\/*****.com\/pages\/view?id=2153&item=Mens-Collection-Shoes": {
        "comments": {
            "data": [{
                "id": "123456",
                    "from": {
                    "name": "Laurelle",
                        "id": "123456"
                },
                    "message": "I love these",
                    "can_remove": false,
                    "created_time": "2012-11-20T10:20:16+0000",
                    "like_count": 0,
                    "user_likes": false
            } {
                "id": "123456",
                    "from": {
                    "name": "Dan",
                        "id": "123456"
                },
                    "message": "I know, just stunning!",
                    "can_remove": false,
                    "created_time": "2012-11-20T14:24:15+0000",
                    "like_count": 0,
                    "user_likes": false
            }],
                "paging": {
                "next": "https:\/\/graph.facebook.com\/*****\/comments?limit=25&offset=25&__after_id=*****"
            }
        }
    }
}

我想要:

data->from->id
data->from->name
data->message
data->created_time

我试过了:

$fb_response = json_decode($requests);

foreach($fb_response->data as $item){
echo $item->from->id . '<br />';
echo $item->from->name . '<br />';
echo $item->message . '<br />';
echo $item->created_time . '<br /><br />';
}

但没有运气

1 个答案:

答案 0 :(得分:3)

我认为你在寻找:

$originalUrl = "http://*****.com/pages/view?id=2153&item=Mens-Collection-Shoes";
$url = urlencode($originalUrl);
...
foreach($fb_response->$originalUrl->comments->data as $item) {

data不是JSON文档中直接属性的名称;它在http://*****.com/pages/view?id=2153&item=Mens-Collection-Shoescomments下。