【问题标题】:Get image url from twitter api entity parameter php从twitter api实体参数php获取图片url
【发布时间】:2012-11-15 20:44:27
【问题描述】:

我正在尝试通过entities 参数获取并显示在推文中发布的图像。我的 url 中有 include_entities,可以看到返回的 json 中的实体。

在我的 foreach 循环中,我正在执行以下操作:

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;
...
}

但是,这并没有返回任何内容。

对于我的输出,我有...

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

} else {
    //format table for regular tweets 
} 

这是 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)
        }
...
}

任何想法为什么这不起作用?任何帮助将非常感激!

【问题讨论】:

    标签: php image twitter entities


    【解决方案1】:

    $result->entities->media-> 这是结构中看到的数组...尝试使用

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

    【讨论】:

    • 嗨,我没有得到我在这里写的 media_url 字段值 - stackoverflow.com/questions/24471417/…
    • API 不久前被更改。现在,如果您想要媒体网址,您需要这样做:$result->media[0]->media_url;
    【解决方案2】:
    $results = json_decode($results);
    

    我认为在解码 json 对象时更容易了解如何选择项目 :)

    下次试试:)

    【讨论】:

    • 大声笑,小伙子。一切都是正确的。下次请注意阅读。
    • 现在是正确的,我的编辑之后..检查差异,伙计
    【解决方案3】:

    我很可能是错的,但我相信 if 语句中的数字前后都需要 []。

    【讨论】:

      猜你喜欢
      • 2016-10-04
      • 2018-07-19
      • 2014-05-18
      • 1970-01-01
      • 2016-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-10
      相关资源
      最近更新 更多