Twitter bot tutorial


by Php Ninza on February 21, 2010

Twitter is heavily becoming a source of traffic and with there extremely powerful api , it’s very easy to automate stuff via Twitter bots.

Today i am going to show you a tutorial for very simple but yet very powerful bot which if used effectively can drive great traffic on sites. These bots simply search twitter stream for predefined keywords and tweet target messages to the tweeters. These bots can be set via cron to read stream every few minutes and make responses in real time.

For newbies and starters all Twitter API documentation can be found here , From here we are going to use Search API methods and Status Update REST api method for sending predefined tweets to the users.

Search API can give output both in JSON and ATOM(XML) format , And can take several parameters for searching twitter stream. We are going to use just the query(q) parameter to provide keyword to the search ,A complete parameter list can be found here .

So , Let’s take the parameter query as PHP problem and send them our link of phpcollection.com in the tweet. For this we will first search the twitter stream for the keyword PHP problem

< ?php
$url = "http://search.twitter.com/search.atom?q=urlencode(PHP+problem)";

The response of this query is in a XML format so we will use simplexml PHP extension for converting this XML data in native PHP object. simplexml_load_file function loads and converts xml data directly from the url. It can be easily then traversed via a Php object.

$data = simplexml_load_file($url);

Now the data can be traversed via a foreach loop , $data->entry contains a array of all tweet entries. We will take them one by one in $value. Tweet username can be accessed via $value->author->uri to whom we will be tweeting the predefined message.

foreach ($data->entry as $value)
{
	$from_name = $value->author->uri;
	$from_name = str_replace ("http://twitter.com/", "", $from_name);
    	$message = "@$from_name Hey Buddy you can find solution to your problems at http://www.phpcollection.com #php";

Now we have the users who are looking for help in PHP language (via PHP problem keyword), Now using Twitter PHP wrapper we will tweet this message to the users. This can be easily achieved via CURL, However for the sake of the tutorial we will use mytwitter php class for tweeting back to users.

		require_once( 'mytwitter.php' );
		$twitter =  new MyTwitter('phpcollection', '**********');
 		$result = $twitter->updateStatus($message);
}
?>

Complete code can be downloaded here

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay

{ 9 comments… read them below or Shout @ me! }

phpcollection February 21, 2010 at 6:50 am

Twitter Bot tutorial in PHP , Send automatic tweets | PHP tutorials and Scripts Collection http://tinyurl.com/yhwe9wc

This comment was originally posted on Twitter

careersoft February 21, 2010 at 8:42 am

Twitter Bot tutorial in PHP , Send automatic tweets | PHP … http://bit.ly/dzSHSC

This comment was originally posted on Twitter

djbhai February 21, 2010 at 11:56 am

How to create an automated twitter bot http://bit.ly/9xZYBV #php #twitter

This comment was originally posted on Twitter

techprone February 21, 2010 at 2:56 pm

How to create an automated twitter bot http://bit.ly/9xZYBV #php #twitter

This comment was originally posted on Twitter

adry19x February 28, 2010 at 12:40 pm

Twitter bot tutorial http://www.phpcollection.com/twitter-bot.html

This comment was originally posted on Twitter

Tiggerpepper March 7, 2010 at 11:57 pm

Twitter Bot tutorial in PHP , Send automatic tweets | PHP tutorials and Scripts Collection http://tinyurl.com/yhwe9wc

This comment was originally posted on Twitter

Achmad Solichin March 18, 2010 at 5:59 am

wow… great tutorial. Is it can be detected as SPAM?

Php Ninza March 19, 2010 at 2:51 am

Well, it totally depends how you use it, Using it for spam purpose may make twitter your user id as spam, However , if you go slow as few updates per hour. You would be fine.

swaen May 31, 2010 at 3:01 pm

The link to download the code give me a 404. Where can I find it?

Leave a Comment

Previous post: