Twitter bot in php with oAuth support

For last couple of days, I was looking into twitter API. One day I asked Lenin vai about a bot he made. He suggested me a Twitter bot. I was so excited at the very beginning. But when I had started I gone through some new features which I didn’t know and of course sometimes silly things give us so much pain 😛

About Tweet Bot:

The bot collects data from one or more locations and then tweets those one by one. Very simple php script. Usable with any website or blog and even any person at Twitter.

Tweet Bot Features:
  • oAuth supported
  • Parses feed with simple feed parser
  • Formatting links by shortening url
  • Format large title
Setting up Tweet Bot

Register application from here. For oAuth support I’ll be using Abraham William’s oAuth Library. Download the Twitter oAuth Library from github. The bot will be getting feeds from RSS. For that we need a parser. So, here we’ll be using Simplepie to parse RSS feed. Download the parser from here.

I always prefer to make a configuration file, just to keep the code clean and ready to use any time.



/* Change this url as needed */
define('FEED_URL', '');

Now let’s make our hand dirty with some coding 🙂


Connect to twitter via Abraham’s oAuth Library


Setup Simplepie to parse RSS feed.

$feed = new SimplePie();

We need to save the parsed link so that we can prevent the bot tweeting the same thing more than once. Twitter has word limitation of 140 words. So, the bot parses the title as well as the link. It always matches the latest link with the last tweeted one.

/* find the total number of available feed */
$max = $feed->get_item_quantity();

/* Get last tweeted data */
$last_saved_tweet = file_get_contents('last_tweet');

/* Get the first item on the feed */
$item = $feed->get_item(0);

/* permalink of the first item */
$itemlink = $item->get_permalink();

/* save the permalink into file */
file_put_contents('last_tweet', $itemlink);

/* title of the first item */
$itemtitle = $item->get_title();

/* length of the item title */
$titlelength = strlen($itemtitle);

if ($titlelength > 110) {
    $itemtitle = substr($itemtitle, 0, 107) . "...";

if ($itemlink != $last_saved_tweet) {

    /* shorten the url with tinyurl */
    $shortlink = file_get_contents("" . $itemlink);

    /* Update status with Abraham's oAuth Library */
    $connection->post('statuses/update', array ('status' => $itemtitle . " " . $shortlink));

So, now the Tweet Bot is ready to shoot! 🙂

Location API on Aloashbei Platform

To work with Aloahbei platform, first configure the .wsdl file for LBS which is given on the Aloashbei site. Or you can download it from here.


class LBSRequest {
 public $msisdn;
 function  __construct($num) {
 $this->msisdn = $num;
 public $registrationID = "******";
 public $password= "******";

class GPInit {
 public $LBSRequest;

if(isset ($_POST['number'])) {
 $GPInit = new GPInit();
 $GPInit->LBSRequest = new LBSRequest("88".$_POST['number']);
 $result = $soap->requestLocation($GPInit);

Download full code

Send SMS with GrameenPhone API and PHP

To work with Aloahbei platform, first edit the .wsdl file for SMS which is given on the Aloashbei site. Or you can download it from here.

First, make a config.php file.

$soap = new SoapClient("");

$var['registrationID'] = "******";
$var['password'] = "****";
$var['sourceMsisdn'] = '88017********';
$var['smsPort'] = 7424;
$var['msgType'] = 4;
$var['charge'] = 0.00;
$var['chargedParty'] = '88017********';
$var['contentArea'] = 'gpgp_psms';

Now, make the main index.php file put the number and message into the $var array and call sendSMS api.

$var['destinationMsisdn'] = '88'.$_POST['number'];
$var['msgContent'] = $_POST['msg'];

$result = $soap->sendSMS( array ("SendSMSRequest" => $var) );

Download full code