剥离FQL JSON

时间:2013-02-20 18:14:10

标签: php facebook json facebook-graph-api

我正在使用FQL来运行此查询

"SELECT post_id, actor_id, description, created_time, message, type, attachment FROM stream WHERE source_id = $page_id AND type > 0 LIMIT 0,9"

返回包含大量未使用信息的10个项目,并希望获得一些帮助和指导,以帮助将其删除为类似

  {
    "image" : '...',
    "text" : '...',
    "username" : '...',
    "userurl" : '...',
    "userpic" : '...'
  }

有人可以给我一些关于重新格式化JSON对象的提示吗?

由于

2 个答案:

答案 0 :(得分:0)

它以这样的方式对我有用:

https://graph.facebook.com/fql?q=SELECT%20aid%2C%20owner%2C%20name%2C%20object_id%20FROM%20album%20WHERE%20aid%3D%2220531316728_324257%22

答案 1 :(得分:0)

为自己想出来,创建了一个简单的PHP类来保存所需的变量,然后将其添加到数组中。

对于任何感兴趣的人来说,这是代码的主要部分。

类别:

class Item{
    public $image;
    public $link;
    public $text;
    public $username;
    public $userurl;
    public $userpic;
}

正在使用:

$feed = json_decode($feed);
        $data = array();
        foreach ($feed->data as $post){
            $item = new Item;
            if ($post->attachment->media){
                if (isset($post->attachment->media[0]->src)){
                    $item->image = $post->attachment->media[0]->src;
                }else if (isset($post->attachment->media[0]->photo->images[1]->src)){
                    $item->image = $post->attachment->media[0]->photo->images[1]->src;
                }else if (isset($post->attachment->media[0]->src)){
                    $item->image = $post->attachment->media[0]->src;
                }
                $item->link = $post->attachment->media[0]->href;
            }

            $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
            $text = $post->message;
            if(preg_match($reg_exUrl, $text, $url)){
                $text = preg_replace($reg_exUrl, "<a href=\"".$url[0]."\" target=\"_blank\">".$url[0]."</a> ", $text);
            }
            $item->text = $text;
            $puser = number_format($post->actor_id,0,'','');
            $url = "https://graph.facebook.com/$puser?fields=picture,name,link&access_token=$at";
            $puser = file_get_contents($url);
            $puser = json_decode($puser);

            $item->userpic = $puser->picture->data->url;
            $item->username = $puser->name;
            $item->userurl = $puser->link;
            $item->platform = "facebook";
            $data[] = $item;
        }
        $this->response($data, 200);
    }

希望这可以帮助处于同样情况的其他人。