簡體   English   中英

如何與twitter的API的JSON返回正確交互?

[英]How do I properly interact with the JSON return of twitter's API?

我正在使用Twitter API(版本1.1),並試圖通過PHP從JSON返回獲取特定變量。 $ mentionsGET statuses/mentions_timeline請求的返回json_decode()值。

我有以下PHP:

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

這可以按預期工作,但是當我將['created_at']添加到$提到時,我得到:

Fatal error: Cannot use object of type stdClass as array

根據Twitter文檔的預期回報:

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

這是使用print_r($提及)返回的內容的示例(在循環內):

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

這個stdClass是什么,如果可能的話我該如何與它進行交互?

采用

$mentions = json_decode($data, true)

如果查看文檔 ,如果要獲取關聯數組而不是對象,則會注意到存在assoc參數。

否則,您可以使用$mention->created_at訪問對象屬性

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM