Social Icons

Pages

Saturday, March 22, 2014

How to post tweet to Twitter using Php

Here's my first tutorial for beginners which shows how to post or send tweet to Twitter using php code. It's very simple just follow this tutorial.

What you need?

  • A system installed php. If you don't have, get Xampp from here. Install once your download gets finished. 
  • Next, is Abraham's Php Library. You can get it right from here. Click download zip, extract it & save it in a separate folder.
  • Sublime Editor. (optional but it's good when you do coding).

Note : Click Pictures to view in big size.

Steps to send tweet to Twitter : 


1. Go to Twitter Developer page first to create an app. You need to first sign-in with your current twitter username and password to proceed.

2. Click Create new app.


3. In Create an application page, fill Application name, App Description and Website URL. If you don't have a website, just paste your Twitter profile link.



Once you filled tick Yes i agree & click Create application.


4. You can see the following screenshot once your application created without any problem. 




The next thing you need to do is to change the app access permission to write since we gonna post tweet to twitter. For to do, Goto - Permissions Tab - Change the access type to read and write - Click Update setting.

               

5. Visit API Keys Tab, scroll down and click Create my access token.


                          

6. Note down API Key, API Secret, Access Token & Access Token Secret and keep it in safe.


7. Go to Xampp folder which is in C Drive. And in that, open htdocs folder. Delete Index.php & Index.html if you find anything in htdocs folder. (This is for beginners)


8. Create a new folder i call it as FirstApp2014. Copy only twitteroauth folder from Abraham Twitter Php Library (i.e the one you downloaded from here) and paste it in FirstApp2014 folder.




9. Your FirstApp2014 folder now should only have twitteroauth folder. Let create test.php using any editor ( i prefer sublime) - save it in FirstApp2014 folder.

10. First add this code to include Abraham Twitter library,


<?php
require_once('twitteroauth/twitteroauth.php'); 

Next is,

$CONSUMER_KEY = " paste consumer key here which you got from Twitter apps page";
$CONSUMER_SECRET = "paste consumer secret here";
$ACCESS_TOKEN =  "paste access token here";
$ACCESS_SECRET = "paste access secret here";

To make a connection to twitter add the following lines,

$connection = new TwitterOAuth($CONSUMER_KEY, $CONSUMER_SECRET, $ACCESS_TOKEN , $ACCESS_SECRET);

The following line verify the credentials,

$content = $connection->get('account/verify_credentials');

The code to post tweet is,

$connection->post('statuses/update', array('status' => "Hai this is my first tweet"));

Replace your text above what you want to tweet through app.


Here's the full code for test.php :

<?php
require_once('twitteroauth/twitteroauth.php'); 

$CONSUMER_KEY = " paste consumer key which you got from Twitter apps page";
$CONSUMER_SECRET = "paste consumer secret here";
$ACCESS_TOKEN =  "paste access token here";
$ACCESS_SECRET = "paste access secret here";
$connection = new TwitterOAuth($CONSUMER_KEY, $CONSUMER_SECRET, $ACCESS_TOKEN , $ACCESS_SECRET);

$content = $connection->get('account/verify_credentials');

$connection->post('statuses/update', array('status' => "Hai this is my first tweet"));
echo "Tweet sent";
?> 

11. Once you're done open any browser and type localhost/FirstApp2014/test.php to execute this script in order to tweet. You can see your tweet in your user timeline. Enjoy! :)