【问题标题】:Give structure to tweets in JSON format以 JSON 格式为推文提供结构
【发布时间】:2015-07-12 19:37:16
【问题描述】:

我有以下代码,它将JSON 格式的推文显示到HTML 页面。我希望以更美观的方式显示这些推文。谁能给我提供任何选择?

session_start();
require_once("twitteroauth-master/twitteroauth/twitteroauth.php"); //Path to twitteroauth library

$twitteruser = "massa_jes";
$notweets = 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_encode($tweets); //decodes the json string to an array
var_dump($resArr);

【问题讨论】:

  • 您应该更具体地说明您想要实现的目标。您是否打算将推文嵌入 HTML 页面? (另外,请从您的代码示例中删除前导空格。)
  • 是的,我希望将推文嵌入到 HTML 页面中,并且我已经删除了问题中的前导空格
  • 好吧,如果你想要 HTML,那么我建议你抽出 HTML 而不是 var_dump($resArr);。 :)

标签: php json twitter tweets


【解决方案1】:

试试下面的

$tweetsArr = json_decode($tweets,true); //decodes the json string to an array
foreach($tweetsArr as $tweet) {
echo $tweet['text']."<br />";
...
...
}

【讨论】:

  • 这是我想要的,但是我收到以下错误:为 foreach() 提供的参数无效
  • 检查 $tweetsArr 的计数 if count($tweetsArr)=1 然后试试这个 foreach($tweetsArr[0] as $tweet) {
猜你喜欢
  • 1970-01-01
  • 2023-04-11
  • 2019-02-21
  • 2019-02-16
  • 2021-12-24
  • 1970-01-01
  • 2022-11-15
  • 1970-01-01
  • 2019-09-06
相关资源
最近更新 更多