2012-12-10 47 views
0

我試圖從Facebook頁面流的數組中獲取圖像src。 我查詢使用Facebook PHP SDK中的表和以下FQL multiquery:facebook php sdk show img src from array

$query=array(

"query1"=>"SELECT actor_id, attachment.media, app_data, description, message, permalink, likes.count, created_time, comments.count, action_links, attachment, type FROM stream WHERE source_id = 294546637313646 AND type > 0 limit 50", 
"query2"=>"SELECT name, page_id FROM page WHERE page_id IN (SELECT actor_id FROM #query1)"); 

$response=$facebook->api(array(
'method' => 'fql.multiquery', 
'queries' => $query 
)); 

$posts = $response[0]['fql_result_set']; 
$author = $response[1]['fql_result_set']; 

這讓我有這一個數組:

[4] => Array 
        (
         [actor_id] => 294546637313646 
         [attachment] => Array 
          (
           [media] => Array 
            (
             [0] => Array 
              (
               [href] => http://www.facebook.com/photo.php?fbid=306133272821649&set=a.306133049488338.53827.294546637313646&type=1&relevant_count=1 
               [alt] => Test Amp Picture 
               [type] => photo 
               [src] => http://photos-a.ak.fbcdn.net/hphotos-ak-prn1/69480_306133272821649_877073836_s.jpg 
               [photo] => Array 
                (
                 [aid] => 294546637313646_53827 
                 [pid] => 294546637313646_561094 
                 [fbid] => 306133272821649 
                 [owner] => 294546637313646 
                 [index] => 1 

我想呼應圖片src ,但無法解決如何去做。

我試過有: $pics = $posts[0]['fql_result_set']; 然後 echo "<img src={$pics['src']} />"; 但不起作用。

請任何幫助,將感激地收到。

感謝

+0

我看你的陣列,或者至少你已經證明這一點,與指數4,我猜你將需要訪問的開始那索引:'$ posts [4] ['attachment'] ['media'] [0] ['src']' – Dale

+0

嗨戴爾,陣列從0開始,但直到4,沒有圖像。我正在循環使用'for each',所以期待什麼都沒有,直到4. –

+1

我想你需要有一些異常處理,爲此! –

回答

0

未經測試的PHP代碼:

$query = "SELECT actor_id, attachment.media, app_data, description, message, permalink, likes.count, created_time, comments.count, action_links, attachment, type FROM stream WHERE source_id = 294546637313646 AND type > 0 limit 50"; 
$response = $facebook->api('/fql?q='.$query); 
$posts = $response["data"]; 

foreach($posts as $post){ 
    //do stuff 

    //check to see if theres an image 
    if(isset($post["attachment"]["media"]) and is_array($post["attachment"]["media"])){ 
      //lets get the first image only! 
     $first_pic = $post["attachment"]["media"][0]; 
     //here's the src of the first picture! 
     $src = $first_pic["src"]; 
     echo "<img src='$src' />"; 
    }else{ 
     echo "There is no attached picture!"; 
    } 
} 
+0

謝謝,幾乎在那裏,它顯示所有帖子上的圖像。舉例來說,2個Facebook的帖子有圖片,3個沒有圖片。顯示時,所有帖子都有2張圖片......雖然沿着正確的線條... –

+0

你是什麼意思 –

+0

解決了它,我用下面的 'if(isset($ value [「attachment」] [ 「media」] [0] [「src」])){ echo「」; }' 並顯示圖像沒有任何額外的每個循環... 謝謝你的幫助,雖然湯米指出我在正確的方向。 –