I am using the Twitter Search API 1.1 to search for tweets on a particular keyword and display the results as a search results page at motherpipe.co.uk.
I have managed to get the Oauth working with the help of James Mallison at http://github.com/j7mbo/twitter-api-php .
I am stuck at the stage of taking the actual response I get from Twitter and display it normally in divs in HTML (using PHP). This is the reply I get: https://motherpipe.co.uk/staging/index2.php .
I just can't seem to write a simple loop to display only the 'screen_name' and 'text'.
So far I have tried different versions of this:
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$requestMethod = 'GET';
$getfield = '?q=sweden&result_type=recent';
$twitter = new TwitterAPIExchange($settings);
echo $twitter ->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
$response = json_decode($twitter);
foreach($response as $tweet)
{
echo "{$tweet->screen_name} {$tweet->text}\n";
}
Any feedback or ideas on how to loop through this correctly would be much appreciated.
Cheers
If you would do a var_dump
of $response
, you would probably get a better idea of the structure. Looks like you need to loop over $response->statuses
. The user information is under $tweet->user->screen_name
and the tweet text is under $tweet->text
. So:
$response = json_decode($twitter);
foreach($response->statuses as $tweet)
{
echo "{$tweet->user->screen_name} {$tweet->text}\n";
}
The user information is under $tweet->user->screen_name
and the tweet text is under $tweet->text
$response = json_decode($twitter);
foreach($response->statuses as $tweet)
{
echo "{$tweet->user->screen_name} {$tweet->text}<br />";
}
OR also you can use this too
$response = json_decode($twitter, true);
$counter = 0;
foreach($response['statuses'] as $tweet)
{
echo "{$tweet[$counter]['user']['screen_name'] {$tweet[$counter]['text']}<br />";
$counter +=1;
}
So the answer (thanks to Prisoner, Jonathan Kuhn and Mubin Khalid) is, in order to display the response from the Twitter Search API, assign a variable to the Twitter JSON response. This will enable you to loop through and display whatever you want from the response.
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$requestMethod = 'GET';
$getfield = '?q=sweden&result_type=recent';
$twitter = new TwitterAPIExchange($settings);
$api_response = $twitter ->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
$response = json_decode($api_response);
foreach($response->statuses as $tweet)
{
echo "{$tweet->user->screen_name} {$tweet->text}\n";
}
I dont use james code but I get it working other way round
<ul>
<?php $get_tweets = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$twitteruser."&count=".$notweets);
foreach($get_tweets as $tweet) {
$tweet_desc = $tweet->text;
?>
<li><?php echo $tweet_desc ?></li>
<?php }?>
</ul>
You can see a working Twitter Api V1.1 at the footer of this page. If you want something like this or further customisation let me know .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.