2013-02-20 64 views
0

我使用FQL来运行此查询剥离下来FQL JSON

"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对象的提示吗?

感谢

回答

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); 
    } 

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

+0

您可以在从Facebook获取JSON数据后编辑它。但是,当你进行API调用时,你也可以使用过滤器,所以它不会给你太多的东西。 – Wenger 2013-02-21 15:46:22

0

它为我在这样的方式:

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

我想我更多地讨论将json提要映射到对象而不是控制返回的信息。 – 2013-02-21 08:57:19