Dachan
Dachan

Reputation: 156

Parsing Twitter search results in JSON with PHP

I'm able to pull out information from the user_timeline with:

$recentTweets = $connection->get('https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=username&count=1');
for ($i=0; $i<count($recentTweets); $i++){
    echo $recentTweets[$i]->text;
};

But it doesn't seem to work with Twitter's search results. I assume it has to do with the fact it is formatted differently? I'm seeing 'stdClass Object' when I use print_r, which I don't see with the results from the user_timeline.

$cowTweets = $connection->get('https://api.twitter.com/1.1/statuses/search.json?q=cow&count=1');
for ($i=0; $i<count($cowTweets); $i++){
    echo $cowTweets[$i]->text;
};

I'm using Abraham's TwitterOauth library if that helps.

Upvotes: 2

Views: 4348

Answers (3)

Dey
Dey

Reputation: 11

$cowTweets = $connection->get('https://api.twitter.com/1.1/statuses/search.json?q=cow&count=1');

Try this because it is a JSON response:
case your count=1 use:

echo $cowTweets->statuses[0]->text;

case count > 1 use

for ( $i = 0; $i < count( $cowTweets->statuses ); $i++ ) {
    echo $cowTweets->statuses[ $i ]->text;
};

Upvotes: 1

4x3LDev
4x3LDev

Reputation: 99

you can find you're answer here : PHP Twitter Hashtag Search showing no results in parse but data present in array

hope it's help !

Upvotes: 0

Schwierig
Schwierig

Reputation: 712

Just use something like this and iterate through the assoc array you get from it:

$array = json_decode(file_get_contents('https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=username&count=1'),true);

Upvotes: 1

Related Questions