Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get image url from twitter api entity parameter php

I'm attempting to get and display images posted in a tweet with php via the entities parameter. I have include_entities in my url and can see the entities in the returned json.

In my foreach loop, I am doing 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 is not returning anything.

For my output, I have...

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

} else {
    //format table for regular tweets 
} 

And this is the 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 would be much appreciated!

like image 276
Michael Avatar asked Feb 28 '12 06:02

Michael


1 Answers

$result->entities->media-> this is an array as seen in structure... try using

$media_url = $result->entities->media[0]->media_url;
like image 50
Arfeen Avatar answered Nov 06 '22 19:11

Arfeen