2013-02-20 90 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