在php中获取facebook Json格式内容

时间:2013-01-24 07:23:36

标签: php json

我刚开始学习Facebook应用程序开发,我需要按名称获取facebook group id,如果它是一个字符串,如果它的数字是(整数)。现在,我到目前为止所做的是:

        if(isset($_POST['scrape'])):
            $group_id = $_POST['gid'];
            if(!(int)$group_id)
            {
                $fb_group_array = file_get_contents("http://graph.facebook.com/".$group_id);                    
            }
        endif;

其中$ fb_group_array输出如下内容:

            {
               "is_published": true,
               "personal_info": "This group is where u Share Adult Jokes and Funny pictures.\nOnly \" BOYS \" with real identity are welcome to the group.\n( RULE: NO FIGHTS BETWEEN OUR BROTHERS, RESPECT; BEHAVE AND SALUTE to our BROTHERHOOD )\n(NOTE: ADULT CONTENTS, 18+ ONLY, GIRLS AND GAYS PROHIBITED)\nThank you. :)",
               "talking_about_count": 0,
               "username": "MENSROOMrELOADED",
               "were_here_count": 0,
               "category": "Public figure",
               "id": "429748430401621",
               "name": "M E N S R O O M (r E L O a D E D )",
               "link": "https://www.facebook.com/MENSROOMrELOADED",
               "likes": 137
            }

现在我想要从这个输出中提取数据作为id,用户名等。我试过爆炸它们并且它的工作正常但是我确定有更好的方法来做它,比如转换为PHP数组或JSON。请引导我走向正确的方向。 谢谢。

1 个答案:

答案 0 :(得分:3)

使用json_decode,如下所示:

$fb_group_obj = json_decode($fb_group_array, true);
$group_fbid = $fb_group_obj['id'];