我已经在dev.twitter.com上创建了一个应用程序,我的应用程序正在我的Twitter上发布JSON数据:http://projweb.hj.se/~hada1192/twitter/tweets_json.php
我对Twitter API,JSON和PHP很新,所以现在我的问题是:如何选择我的推文(我只想要文本,例如“我的第一条推文”,“我的第二条推文,推特很有趣“,”不能告诉你如何在PHP阵列中一起享受过山车的多少?
这是我的代码:
<?php
require 'tmhOAuth.php'; // Get it from: https://github.com/themattharris/tmhOAuth
// Use the data from http://dev.twitter.com/apps to fill out this info
// notice the slight name difference in the last two items)
$connection = new tmhOAuth(array(
'consumer_key' => 'xxx',
'consumer_secret' => 'zz',
'user_token' => '547733151-yyy', //access token
'user_secret' => 'ooo' //access token secret
));
// set up parameters to pass
$parameters = array();
if ($_GET['count']) {
$parameters['count'] = strip_tags($_GET['count']);
}
if ($_GET['screen_name']) {
$parameters['screen_name'] = strip_tags($_GET['screen_name']);
}
if ($_GET['twitter_path']) { $twitter_path = $_GET['twitter_path']; } else {
$twitter_path = '1.1/statuses/user_timeline.json';
}
$http_code = $connection->request('GET', $connection->url($twitter_path), $parameters );
if ($http_code === 200) // if everything's good
{
$response = strip_tags($connection->response['response']);
if ($_GET['callback']) // if we ask for a jsonp callback function
{
echo $_GET['callback'],'(', $response,');';
}
else
{
echo $response;
}
}
else
{
echo "Error ID: ",$http_code, "<br>\n";
echo "Error: ",$connection->response['error'], "<br>\n";
}
?>
答案 0 :(得分:0)
响应是JSON,因此您可以使用内置的php(&gt; =版本5.2)函数json_decode()将JSON解码为关联数组。从这里您可以访问以下内容的推文:
$tweets = json_decode($response, true);
foreach( $tweets as $key => $tweet ) {
echo $tweet["text"];
}
答案 1 :(得分:0)
解决方案:
if ($_GET['callback']) // if we ask for a jsonp callback function
{
echo $_GET['callback'],'(', $response,');';
}
else
{
//echo $response;
$tweets = json_decode($response, true);
foreach( $tweets as $key => $tweet ) {
echo $tweet["text"] . "<br>";
}
}