How to use twitter for sending notifications with Codeigniter

Today i will show you how you can create an application in codeigniter so you can use it to share “notifications” in twitter.
First of all you have to create a new application in twitter “Twitter app”. A very helpfull tutorial i founded here Tutorial. If it asks you for your mobile phone, just download the twitter app to your smartphone and you will be ok.

After you have just created your app, lets go and create the Codeigniter app.

First of all you must download the external library for twitterOAuth from here TwitterOAuth
When you download this change the name of twitter.class.php to Twitter.php and save it to libraries folder of codeigniter app. Also do the same with oAuth change it to OAUTH and save it to libraries folder.

After that we must do a change in the Twitter.php file in the __construct fuction.
Here is what it has inside

public function __construct($consumerKey, $consumerSecret, $accessToken = NULL, $accessTokenSecret = NULL)
{
if (!extension_loaded('curl')) {
throw new TwitterException('PHP extension CURL is not loaded.');
}

$this->signatureMethod = new Twitter_OAuthSignatureMethod_HMAC_SHA1();
$this->consumer = new Twitter_OAuthConsumer($consumerKey, $consumerSecret);
$this->token = new Twitter_OAuthConsumer($accessToken, $accessTokenSecret);
}

And now you must change it like this

public function __construct($config)
{
$consumerKey = $config['consumerKey'];
$consumerSecret = $config['consumerSecret'];
$accessToken= $config['accessToken'];
$accessTokenSecret = $config['accessTokenSecret'];

if (!extension_loaded(‘curl’)) {
throw new TwitterException(‘PHP extension CURL is not loaded.’);
}

$this->signatureMethod = new Twitter_OAuthSignatureMethod_HMAC_SHA1();
$this->consumer = new Twitter_OAuthConsumer($consumerKey, $consumerSecret);
$this->token = new Twitter_OAuthConsumer($accessToken, $accessTokenSecret);
}

So we are ready from the libraries.
Now a easy way to have your app_id and tokens saved in your app is in the config folder and not in your model inside. So create a file twitter.php and save in iside the config folder and save the credentials you took from twitter application that you have before created.

$config['consumerKey'] = '';
$config['consumerSecret'] = '';
$config['accessToken'] = '';
$config['accessTokenSecret'] = '';

After you saved your credentials let’s create the model you will have the code inside.

$this->config->load('twitter', TRUE);
$consumerKey = $this->config->item('consumerKey', 'twitter');
$consumerSecret = $this->config->item('consumerSecret', 'twitter');
$accessToken = $this->config->item('accessToken', 'twitter');
$accessTokenSecret = $this->config->item('accessTokenSecret', 'twitter');

$config = array(‘consumerKey’=>$consumerKey,
‘consumerSecret’=>$consumerSecret,
‘accessToken’=>$accessToken,
‘accessTokenSecret’=>$accessTokenSecret);

$this->load->library(‘Twitter’,$config);

$message = $notification_text . ‘ ‘ . $user_twitter;//is the name of the user with @ also remember to have a gap before the name and after.
try {
$this->twitter->send($message);

} catch (Exception $e) {
// an error occurred
echo $e->getMessage();
}

Remember that the twitter keeps what you are sending to the users so you can’t sent the same notification more the once in a specific time period. So you need to send once per day the same or different tweets.

Now you are ready to roll and send whatever you want to your friends and consumers.

Thanks for reading my tutorial!
Remember
Knoledge are for everyone.

Cheers