2015-05-03 93 views
0

我有以下代碼,我在HTML頁面上獲取JSON格式的Tweets,我希望它能夠整齊地顯示在HTML頁面上。我已經包含了foreach循環,但是我收到以下錯誤:「json_decode()期望參數1是字符串,數組給定」。json_decode()期望參數1是字符串,給出的數組

function getConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret) 
{ 
$connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret); 
return $connection; 
} 

$connection = getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret); 

$tweets = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$twitteruser."&count=".$notweets); 

$resArr = json_decode($tweets, 1); // decodes the json string to an array 

if (is_array($resArr)) 
{ 
    foreach ($resArr as $tweet) 
    { 
     echo $tweet['text']."<br />"; 
    } 
} 

我曾嘗試使用下面的代碼,閱讀等建議後,但錯誤也試圖「用$這個時候不是在對象上下文」:

$resArr = json_decode($this->item->extra_fields, true); 

會有人請我提供一些指導?

回答

1

你的錯誤「json_decode()期望參數1是字符串,數組給出」暗示$ tweets已經是一個數組(不是字符串)。請嘗試以下操作:

$tweets = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$twitteruser."&count=".$notweets); 

foreach ($tweets as $tweet) 
{ 
    echo $tweet->text."<br />"; 
} 
+0

我已經試過了,但是我得到了以下錯誤:「無法使用類型stdClass的對象作爲排序」 – user2675041

+0

編輯:用途 - >代替[] – datchung

相關問題