2015-05-03 96 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

相关问题