In this article you can learn how to post a status update to twitter from a PHP application.
First you have to create a app in twitter. For that go to https://dev.twitter.com/apps and sign in with your twitter account.
Go to “My applications” and use “Create a new application” option to create a new twitter application. Keep the application access level to “Read and write”. So that we can write to twitter by using this app.
[ Note: Twitter app do not allow you to use localhost as domain or call back URL. In this example i have used www.detailsinn.com. ]
There are several PHP libraries available for twitter integration. You can find here : https://dev.twitter.com/docs/twitter-libraries#php
In this article i have used Abraham Williams API Library. You can download it here : https://github.com/abraham/twitteroauth
Download the library and grab “twitteroauth” folder and place it in your project folder. Now create index.php and place following code. Replace CONSUMER_KEY, CONSUMER_SECRET, CALLBACK_URL with your twitter app details.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
<?php session_start(); include_once 'twitteroauth/twitteroauth.php'; $consumerkey = 'CONSUMER_KEY'; $consumersecret = 'CONSUMER_SECRET'; $callbackurl = 'CALLBACK_URL'; if(isset($_SESSION['accesstoken']) and $_SESSION['accesstoken']!=''){ if(isset($_POST['msg']) and $_POST['msg']!=''){ $tw = new TwitterOAuth($consumerkey, $consumersecret,$_SESSION['accesstoken']['oauth_token'],$_SESSION['accesstoken']['oauth_token_secret']); $tw->post('statuses/update',array('status'=>$_POST['msg'], 'in_reply_to_status_id' => $_SESSION['id'])); echo 'Posted to twitter.'; } ?> <form action="" method="post"> Message<br /><textarea name="msg"></textarea><br /> <input type="submit" value="Post to Twitter" /> </form> <?php }else{ if(isset($_REQUEST['oauth_token'])){ $tw = new TwitterOAuth($consumerkey, $consumersecret, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']); $accesstoken = $tw->getAccessToken($_REQUEST['oauth_verifier']); if($tw->http_code == 200){ $content = $tw->get('account/verify_credentials'); $id = $content->user->id; $_SESSION['accesstoken'] = $accesstoken; $_SESSION['id'] = $id; header('Location: index.php'); exit; }elseif($tw->http_code == 401){ header('Location: index.php'); exit; }else{ echo 'Oops! Twitter connection failed!!'; session_destroy(); exit; } }else{ $tw = new TwitterOAuth($consumerkey, $consumersecret); $requesttoken = $tw->getRequestToken($callbackurl); $_SESSION['oauth_token'] = $requesttoken['oauth_token']; $_SESSION['oauth_token_secret'] = $requesttoken['oauth_token_secret']; if($tw->http_code == 200){ $authorizationurl = $tw->getAuthorizeURL($_SESSION['oauth_token']); header('Location: '.$authorizationurl); }else{ echo 'Unable to connect with twitter.'; exit; } } } ?> |
Now execute the URL. You will be redirected to twitter to authenticate.
Once you done it you can see the form by which you can post to twitter.