1

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

3
  • you're not showing us your whole code. Notice: Undefined variable: objJson in /var/www/vhosts/motherpipe.co.uk/httpdocs/staging/index2.php on line 27 Commented Sep 23, 2013 at 21:40
  • Yes sorry I was trying to do a loop after the actual printout of the response from twitter. Live at that page after the actual content of the Twitter response is $data = array(); foreach($objJson["statuses"] as $status) { $data[] = array( "created_at" => $status["created_at"], "text" => $status["text"] ); } echo "<pre>" . print_r($data,1) . "</pre>"; ?> Commented Sep 23, 2013 at 21:48
  • you can find a working api at the footer of this page.Twitter ApiV1.1 If you want something like this or further customisation let me know Commented Sep 24, 2013 at 1:08

4 Answers 4

2

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 .

Sign up to request clarification or add additional context in comments.

Comments

1

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";
}

1 Comment

Thanks Jonathan. I tried your suggestion but am getting 2 errors. "Warning: json_decode() expects parameter 1 to be string" , and "Invalid argument supplied for foreach()".
1

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;
}

Comments

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";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.