Get image url from twit api entity php parameter

I am trying to get and display images sent in tweet using php through an entity parameter. I have include_entities in my url and you can see the objects in the returned json.

In my foreach loop, I do the following:

foreach($results as $result){ $media_url = $result->entities->media->media_url; $media_size_h = $result->entities->media->sizes->small->h; $media_size_w = $result->entities->media->sizes->small->w; ... } 

However, this does not return anything.

For my conclusion, I have ...

 if(strlen($media_url) > 0) { //format table for tweets with images } else { //format table for regular tweets } 

And this is json ...

 array(20) { [0]=> object(stdClass)#5 (21) { ... ["entities"]=> object(stdClass)#7 (4) { ["media"]=> array(1) { [0]=> object(stdClass)#8 (10) { ["type"]=> string(5) "photo" ["media_url"]=> string(38) "http://photourl.jpg" ... ["sizes"]=> object(stdClass)#9 (4) { ["thumb"]=> object(stdClass)#10 (3) { ["resize"]=> string(4) "crop" ["h"]=> int(150) ["w"]=> int(150) } ... } 

Any ideas why this is not working? Any help is appreciated!

+6
source share
3 answers

$result->entities->media-> This is an array, as you can see from the structure ... try using

 $media_url = $result->entities->media[0]->media_url; 
+11
source
 $results = json_decode($results); 

It seems to me that it is much easier to see how to select elements when decoding json objects :)

Try again next time :)

0
source

I could have been mistaken, but I think that you need [] before and after the numbers in the if statements.

-1
source

Source: https://habr.com/ru/post/909518/


All Articles