Jump to content

Twitter api


themistral

Recommended Posts

Guys,

 

I have spent the last few days playing with the Twitter API's but I really don't know what I'm doing.

 

I want a list of approx 50 of the newest tweets that updates an avatar every time a new mention of a particular word is mentioned. It's a bit like the fixoutlook.org background.

 

I guess I need to use the streaming API, but I am finding the documention really difficult to understand - I've never used an API before.

Do I need to save the data to a database first?

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/194791-twitter-api/
Share on other sites

Guys,

 

Assuming I do have to store the data in a database, I am having a problem

 

<?php
include_once('config.php');
$opts = array(
	'http'=>array(
		'method'	=>	"POST",
		'content'	=>	'track='.WORDS_TO_TRACK,
	)
);
$context = stream_context_create($opts);
while (1){
	$instream = fopen('http://'.TWITTER_USERNAME.':'.TWITTER_PASSWORD.'@stream.twitter.com/1/statuses/filter.json','r' ,false, $context);
	while(! feof($instream)) {
		if(! ($line = stream_get_line($instream, 20000, "\n"))) {
			continue;
		}else{
			$tweet = json_decode($line);
			//Clean the inputs before storing
			$id = mysql_real_escape_string($tweet->{'id'});
			$text = mysql_real_escape_string($tweet->{'text'});
			$screen_name = mysql_real_escape_string($tweet->{'user'}->{'screen_name'});
			$followers_count = mysql_real_escape_string($tweet->{'user'}->{'followers_count'});
			$avatar = mysql_real_escape_string($tweet->{'user'}->{'profile_image_url'});
			//We store the new post in the database, to be able to read it later
                                // code to add to database here
			flush();
		}
	}
}
?>

 

config.php contains twitter account details and search term.

I am using the above code to generate the stream, but $id = mysql_real_escape_string($tweet->{'id'}); shows the same id for every post. Can anyone offer any advice?

 

Please - I am desperate to understand this!

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/194791-twitter-api/#findComment-1024314
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.