Riyaz saifi Riyaz saifi - 4 months ago 7
PHP Question

how to loop images using foreach loop

I want to loop images from Facebook

invitable_friends
API using
foreach
loop

$beranda = json_decode(get_html("https://graph.facebook.com/$user->id/invitable_friends?fields=id,picture,name&limit=7&access_token=$accessToken"))->data;

foreach($beranda as $friendsid){


echo '<img src="'.$friendsid->picture.'">';
}


facing this error

Catchable fatal error: Object of class stdClass could not be converted to string in


Thia is the structure of
$beranda


Array
(
[0] => stdClass Object
(
[id] => AVkUphJYSPksSJ47kLM8PGm8cOAjlNLG5NWUMp5IhdSCWkziShWsSVkRdVx1Z73jeu7B73bRzYnecUqD27V3xO36Uh27LD16mVZlRKGKgDLZIQ
[picture] => stdClass Object
(
[data] => stdClass Object
(
[is_silhouette] =>
[url] => https://scontent.xx.fbcdn.net/v/t1.0-1/p50x50/13731636_1671465926510554_2031112688215377192_n.jpg?oh=5a85a07de9ab4011e17378a38bac941c&oe=5831B7E6
)

)

[name] => Sam Saifi
)


var dump of
$friendsid->picture


object(stdClass)#38 (1) {
["data"]=>
object(stdClass)#39 (2) {
["is_silhouette"]=>
bool(false)
["url"]=>
string(145) "https://scontent.xx.fbcdn.net/v/t1.0-1/p50x50/10155363_1386729058279575_5046516924942639505_n.jpg?oh=f55ea91f39815a7fb9661e65899d0a02&oe=581B8AE2"


}
}

Answer

According to your print_r result you need to change your line...

echo  '<img src="'.$friendsid->picture.'">';

to

echo '<img src="' . $friendsid->picture->data->url . '">';

because you are calling a parameter [url], within an object [data], within another object [picture], within another parent object. I would recomment a bootcamp course on object oriented php, anyway Glad it works!

Comments