dsmgcse8876 2014-05-02 11:56
浏览 55
已采纳

获取未定义的偏移量:检索twitter URL实体时出错0

This is my code:

$latesttweets = $twitterconn>get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$twitter_id."&count=".$limit);


foreach ( $latesttweets as $tweet ){
   ...


   $text_url = $tweet->entities->urls[0]->url; // this line gives an error

   // test
   echo $text_url; // this line doesn't give an error



   ... code for display tweet ...
}

I'm trying to retrieve the url from the object:

"urls": [{
  "url": "https:\/\/t.co\/XdXRudPXH5",
  "expanded_url": "https:\/\/blog.twitter.com\/2013\/rich-photo-experience-now-in-embedded-tweets-3",
  "display_url": "blog.twitter.com\/2013\/rich-phot\u2026",
  "indices": [80, 103]
}],

as shown here.

The funny thing is, I've tried echoing and it works, except that the error Notice: Undefined offset: 0 and Notice: Trying to get property of non-object which follows on the next line keeps appearing.

(Note: The same goes for all the other arrays in the urls array. I'm kind of sure it's something to do with the ..->urls[0]->.., but I see no other way.)

Am I doing something wrong?

  • 写回答

1条回答 默认 最新

  • doubi4435 2014-05-02 12:37
    关注

    Ok the issue is urls[0]-> not a valid object in PHP and you should use

    $text_url = $tweet->entities->urls{'0'}->url;
    

    instead.

    However in PHP 5+ the urls[0]-> should work

    Here is the example.

    $str = '{
        "text": "Four more years. http://t.co/bAJE6Vom",
        "entities": {
            "hashtags": [],
            "symbols": [],
            "urls": [
                {
                    "url": "https://t.co/XdXRudPXH5",
                    "expanded_url": "https://blog.twitter.com/2013/rich-photo-experience-now-in-embedded-tweets-3",
                    "display_url": "blog.twitter.com/2013/rich-phot…",
                    "indices": [
                        80,
                        103
                    ]
                }
            ],
            "user_mentions": [],
            "media": [
                {
                    "id": 266031293949698050,
                    "id_str": "266031293949698048",
                    "indices": [
                        17,
                        37
                    ],
                    "media_url": "http://pbs.twimg.com/media/A7EiDWcCYAAZT1D.jpg",
                    "media_url_https": "https://pbs.twimg.com/media/A7EiDWcCYAAZT1D.jpg",
                    "url": "http://t.co/bAJE6Vom",
                    "display_url": "pic.twitter.com/bAJE6Vom",
                    "expanded_url": "http://twitter.com/BarackObama/status/266031293945503744/photo/1",
                    "type": "photo",
                    "sizes": {
                        "medium": {
                            "w": 600,
                            "h": 399,
                            "resize": "fit"
                        },
                        "thumb": {
                            "w": 150,
                            "h": 150,
                            "resize": "crop"
                        },
                        "small": {
                            "w": 340,
                            "h": 226,
                            "resize": "fit"
                        },
                        "large": {
                            "w": 800,
                            "h": 532,
                            "resize": "fit"
                        }
                    }
                }
            ]
        }
    }
    
    ';
    
    $tweet = json_decode($str);
    $text_url = $tweet->entities->urls{0}->url;
    echo $text_url ;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python