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

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

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型