0

我正在使用 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 对象的提示吗?

谢谢

4

2 回答 2

0

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

https://graph.facebook.com/fql?q=SELECT%20aid%2C%20owner%2C%20name%2C%20object_id%20FROM%20album%20WHERE%20aid%3D%2220531316728_324257%22
于 2013-02-21T05:27:48.610 回答
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);
    }

希望这对处于相同情况的其他人有所帮助。

于 2013-02-21T15:39:59.367 回答