本文介绍了foreach中的参数无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下方式从FB中检索评论:

I am using the following to retrieve comments from 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循环进行格式化:

when I print_r($requests); I get the following but cannot work out how to format it with a foreach loop:

{
    "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 />';
}

但没有运气

推荐答案

我想你在找:

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

数据不是直接在JSON文档中的属性的名称;它位于 http://*****.com/pages/view?id = 2153& item = Mens-Collection-Shoes 评论之下

data isn't the name of a property directly inside the JSON document; it's under http://*****.com/pages/view?id=2153&item=Mens-Collection-Shoes and comments.

这篇关于foreach中的参数无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 11:31