douyao4632 2013-04-06 20:55
浏览 43
已采纳

如何与twitter的API的JSON返回正确交互?

I'm using the Twitter API (version 1.1) and am trying to get a specific variable from the JSON return via PHP. $mentions is the returned json_decode() value of a GET statuses/mentions_timeline request.

I have the following PHP:

foreach($mentions as $mention) {
    echo '<pre>' . print_r($mention) . "</pre><br>";
}

This works as expected, however when I add ['created_at'] to $mention I get:

Fatal error: Cannot use object of type stdClass as array

Expected return as per the twitter docs:

[ // mentions
  { // mention
    "created_at": "Mon Sep 03 13:24:14 +0000 2012"
  }
]

This is an example of what's returned with print_r($mention) (inside the loop):

stdClass Object (
    [created_at] => Sat Apr 06 14:56:36 +0000 2013
)

Just what is this stdClass and how do I interact with it, if possible?

  • 写回答

1条回答 默认 最新

  • doulu7258 2013-04-06 20:56
    关注

    Use

    $mentions = json_decode($data, true)
    

    If you look in the docs, you will notice that there is assoc parameter if you want to get associative array instead of an object.

    Otherwise you can access object properties with $mention->created_at

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100