0

I'm trying to get the image src from an array of a Facebook page stream. I query the table using the facebook php sdk and the following FQL multiquery:

$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'];

this gives me an array that has this in it:

[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

I'm trying to echo the image src, but can't work out how to do so.

I've tried having: $pics = $posts[0]['fql_result_set']; and then echo "<img src={$pics['src']} />"; but that doesn't work.

Any help please, would be gratefully received.

Thanks

3
  • I see that your array, or at least what you've shown of it, starts with index 4, I'm guessing you will need to access that index: $posts[4]['attachment']['media'][0]['src']
    – Dale
    Dec 10, 2012 at 14:18
  • Hi Dale, the array starts at 0, but up until 4, there were no images. I'm looping through using a for each so would expect nothing until 4.
    – Neil Frost
    Dec 10, 2012 at 15:32
  • 1
    I guess you need to have some exception handling, for that! Dec 10, 2012 at 18:08

1 Answer 1

0

untested php code:

$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!";
    }
} 
2
  • Thanks, nearly there, it displays the images on all posts. So for example, 2 facebook posts have images, and 3 do not. When displaying, all posts have the 2 images... It's along the right lines though...
    – Neil Frost
    Dec 11, 2012 at 9:57
  • worked it out, I used the following if (isset ($value["attachment"]["media"][0]["src"])){ echo "<img src='". $value["attachment"]["media"][0]["src"]. "'/>"; } and that displays the image without any additional for each loops... Thanks for you help though Tommy it pointed me in the right direction.
    – Neil Frost
    Dec 11, 2012 at 22:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.