2014-07-22 44 views
2

我有帶回這種反應​​如何解析Facebook的圖形API響應

Facebook\GraphObject Object 
(
    [backingData:protected] => Array 
     (
      [data] => Array 
        (
         [0] => stdClass Object 
          (
           [id] => 111 
           [from] => stdClass Object 
            (
             [id] => 111 
             [name] => fo bar 
            ) 

           [name] => etc 

          ) 

我試圖做$reponse->{'backingData:protected'}但它不工作一個Facebook圖形API請求。

另外,下一組結果是圖api的鏈接,但是結果是純json。

[paging] => stdClass Object 
     (
      [cursors] => stdClass Object 
       (
        [after] => MTI3NzMzMTQwMzYy 
        [before] => MTAxNTQzNjI5NTY1NDAzNjM= 
       ) 

      [next] => https://graph.facebook.com/v2.0/111/albums?access_token=xxxxv&limit=25&after=yyy 
     ) 

我的代碼

$user_profile = (new FacebookRequest(
     $session, 'GET', '/me/albums' 
    ))->execute()->getGraphObject(); 

    echo '<pre>'; print_r($user_profile); echo '</pre>'; 
+0

'$ response-> data [0] - > id'我認爲 – WizKid

+0

可以在這裏發佈php代碼嗎? – bhushya

+0

不,我不能使用類型的Facebook \ GraphObject的對象作爲數組' – user9418

回答

4

https://developers.facebook.com/docs/php/GraphObject/4.0.0

的getProperty

的getProperty (string $name, string $type = 'Facebook\GraphObject') 獲取此圖形對象命名鍵的值。如果該值是一個標量(字符串,數字等),它將被返回。如果它是一個關聯數組,則它將作爲GraphObject類型轉換爲適當的子類類型(如果提供)。在https://developers.facebook.com/docs/graph-api/reference/v2.0/album

+2

getProperty函數不捕獲所有用例。例如,/ me /權限會返回一個非常難看的數據(不會使用getProperty解析),或者至少在Internet上找不到使用它的解決方案。然而,Bhushya的答案是要走的路。 Facebook非常愚蠢,它向我返回了一個包含一個包含單個數據條目的graphobject的數組的graphobject。對於我來說,像數據[0]一樣硬編碼是程序員完全無法接受的。另外,Facebook改變了很多東西,並將其隱藏在隱藏在開發站點中的更新日誌中。 dtf – khuderm

17

這裏場的

$user_profile = (new FacebookRequest(
     $session, 'GET', '/me/albums' 
    ))->execute()->getGraphObject(); 

$id = $user_profile->getProperty('id'); 

完整列表是這樣的:

 $user_profile = (new FacebookRequest(
     $session, 'GET', '/me/albums' 
     ))->execute()->getGraphObject(); 
     $album = $user_profile->getProperty('data'); 

     $album_data = $album->asArray();//this will do all job for you.. 
     foreach($album_data as $row){ 
      var_dump($row); 
     } 
+0

謝謝,但是這不會在foreach中提供任何輸出,我在上面的帖子中添加了數組輸出。但是當你嘗試迭代它時,什麼都沒有生成。 – user9418

+0

你打印出的ehrn有什麼錯誤'$ user_profile-> getProperty('data')[0]' – bhushya

+0

'[2014-07-22 22:33:41] production.ERROR:exception'Symfony \ Component \ Debug \ Exception \ FatalErrorException',並在/ var/www/app/controllers/FacebookController中顯示消息'Can not use object of type Facebook \ GraphObject as array'。php:52 堆棧軌跡: #0 [內部函數]:Illuminate \ Exception \ Handler-> handleShutdown() #1 {main} [] []' 沒有[0]的輸出是https:// gist.github.com/anonymous/8e345f124f7efcd076b5 – user9418

5

對於Facebook而言讀新版圖形API V2.5讀取數據如下:

$fb = new \Facebook\Facebook([ 
     'app_id' => 'KEY HERE', 
     'app_secret' => 'SECRET HERE', 
     'default_graph_version' => 'v2.5', 
    ]); 
$asscee_t ="ACCESS TOKEN HERE"; 
    $response = $fb->get('/me/friends', $asscee_t); 
     $get_data = $response->getDecodedBody(); // for Array resonse 
     //$get_data = $response->getDecodedBody(); // For Json format result only 
     echo $get_data['summary']['total_count']; die; // Get total number of Friends 
+0

這一個爲我工作謝謝。 –