Get image url from twitter api entity parameter php

落花浮王杯 提交于 2019-12-07 04:58:58

问题


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!


回答1:


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

$media_url = $result->entities->media[0]->media_url;



回答2:


$results = json_decode($results);

I think that's much easier to see how to select items when you decode json objects :)

Try that next time :)




回答3:


I could very well be wrong but I believe you need [] before and after numbers in if statements.



来源:https://stackoverflow.com/questions/9477297/get-image-url-from-twitter-api-entity-parameter-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!