Post to twitter using PHP Oauth API
Now Oauth library is familiar to everyone , Post to twitter using PHP Oauth API 1.1 is pretty simple and easy to manage all the operation like read/write/get status etc.
This article will help you to create a post to your twitter account , this is using Oauth method so you can tweets anyone’s account with proper application, the code section only few lines and you should have an account with twitter, should create an Apps on your twitter account.
For creating twitter Oauth API Apps check this link .
After creating your Apps on twitter account just create a PHP file or your custom function for post to twitter using Oauth API, this is using version 1.1.
<?PHP
require_once 'twitteroauth.php';
define("CONSUMER_KEY", "****consumer key*******");
define("CONSUMER_SECRET", "*******Consumer secret********");
define("OAUTH_TOKEN", "**********Oauth token*******");
define("OAUTH_SECRET", "*****Oauth Secret********");
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_SECRET);
$content = $connection->get('account/verify_credentials');
$connection->post('statuses/update', array('status' => 'This tweet is from PHP Oauth API'));
?>
Make sure your twitter apps have the proper access to read/write data from/to your twitter account.
This simple code will post a tweets to my own twitter account like below.
the other Options available with twitter Oauth API are follows.
private static const URL_PUBLIC_TIMELINE:String = "/statuses/public_timeline.json";
private static const URL_HOME_TIMELINE:String = "/statuses/home_timeline.json";
private static const URL_FRIENDS_TIMELINE:String = "/statuses/friends_timeline.json";
private static const URL_USER_TIMELINE:String = "/statuses/user_timeline";
private static const URL_RETWEETS_BY_ME:String = "/statuses/retweeted_by_me.json";
private static const URL_RETWEETS_TO_ME:String = "/statuses/retweeted_to_me.json";
private static const URL_RETWEETS_OF_ME:String = "/statuses/retweets_of_me.json";
private static const URL_SINGLE_TWEET:String = "/statuses/show/";
private static const URL_SEND_UPDATE:String = "/statuses/update.json";
private static const URL_DESTROY_TWEET:String = "/statuses/destroy/";
private static const URL_RETWEET:String = "/statuses/retweet/";
private static const URL_RETWEETS:String = "/statuses/retweets/";
private static const URL_MENTIONS:String = "/statuses/mentions.json";
private static const URL_FRIENDS:String = "/statuses/friends";
private static const URL_FOLLOWERS:String = "/statuses/followers";
private static const URL_USER_DETAILS:String = "/users/show.json?/"; // was just /show/ ref line 665
private static const URL_USER_SEARCH:String = "/users/search.json";
private static const URL_SINGLE_DIRECT_MESSAGE:String = "/direct_messages/show/";
private static const URL_RECEIVED_DIRECT_MESSAGES:String = "/direct_messages.json";
private static const URL_SENT_DIRECT_MESSAGES:String = "/direct_messages/sent.json";
private static const URL_SEND_NEW_DIRECT_MESSAGE:String = "/direct_messages/new.json";
private static const URL_DESTROY_DIRECT_MESSAGE:String = "/direct_messages/destroy";
private static const URL_CREATE_FRIENDSHIP:String = "/friendships/create/";
private static const URL_DESTROY_FRIENDSHIP:String = "/friendships/destroy/";
private static const URL_FRIENDSHIP_EXISTS:String = "/friendships/exists.xml";
private static const URL_FRIENDSHIP_SHOW:String = "/friendships/show.xml";
private static const URL_VERIFY_CREDENTIALS:String = "/account/verify_credentials.json";
private static const URL_RATELIMIT_STATUS:String = "/account/rate_limit_status.json";
private static const URL_UPDATE_PROFILE:String = "/account/update_profile.json";
private static const URL_UPDATE_PROFILE_COLORS:String = "/account/update_profile_colors.json";
private static const URL_UPDATE_PROFILE_IMAGE:String = "/account/update_profile_image.xml";
private static const URL_UPDATE_PROFILE_BG_IMAGE:String = "/account/update_profile_background_image.json";
private static const URL_RETRIEVE_FAVORITES:String = "/favorites";
private static const URL_CREATE_FAVORITE:String = "/favorites/create/";
private static const URL_DESTROY_FAVORITE:String = "/favorites/destroy/";
private static const URL_FOLLOW_USER:String = "/notifications/follow/";
private static const URL_UNFOLLOW_USER:String = "/notifications/leave/";
private static const URL_BLOCK_USER:String = "/blocks/create/";
private static const URL_UNBLOCK_USER:String = "/blocks/destroy/";
private static const URL_BLOCK_EXISTS:String = "/blocks/exists/";
private static const URL_BLOCKS:String = "/blocks/blocking.json";
private static const URL_BLOCK_IDS:String = "/blocks/blocking/ids.json";
private static const URL_END_SESSION:String = "/account/end_session.json";
private static const URL_SOCIAL_GRAPH_FRIEND_IDS:String = "/friends/ids";
private static const URL_SOCIAL_GRAPH_FOLLOWER_IDS:String = "/followers/ids";
private static const URL_REPORT_SPAM:String = "/report_spam.xml";
private static const URL_SAVED_SEARCHES:String = "/saved_searches.json";
private static const URL_RETRIEVE_SAVED_SEARCH:String = "/saved_searches/show/";
private static const URL_CREATE_SAVED_SEARCH:String = "/saved_searches/create.json";
private static const URL_DESTROY_SAVED_SEARCH:String = "/saved_searches/destroy/";
private static const URL_TWITTER_SEARCH:String = "http://search.twitter.com/search.atom";
private static const URL_TWITTER_TRENDS:String = "http://search.twitter.com/trends.json";
private static const URL_TWITTER_TRENDS_CURRENT:String = "http://search.twitter.com/trends/current.json";
private static const URL_TWITTER_TRENDS_DAILY:String = "http://search.twitter.com/trends/daily.json";
private static const URL_TWITTER_TRENDS_WEEKLY:String = "http://search.twitter.com/trends/weekly.json";
private static const DATA_FORMAT:String = "json";
You can download the working example from below link.
31 thoughts on “Post to twitter using PHP Oauth API”
is it still working ?
Where i get the twitteroauth.php code….
download the zip package it has all the required files.
Please can you tell me how to get trends???
Thank you, the example work like a charm.
Hi,
this code works only for my twitter account but not for all users. It sucks !!!!!!!!!!
No this works only for your account the main purpose is you can create some auto posting content once you setup the key, In which situation other users need to post to your twitter account ?
The normal twitter tweet button do that Job. In more specific, There is a way to post their own twitter account (means logged users) that you have to manage it with Oauth 2.0 token exchange pattern and dynamic permission mode. something like FB APP or Google APP pattern.
that is not covered in this article.
hello Friends,
i use the example from below link, put my tokens and Consumer keys and nothing is willing to have no error notifications. Example post does not save on my twitter 🙁
Please help to solve the problem and run this script.
Please help me run this script, I really want to use it on my website. I want to send messages directly from my website to Twitter.
Best regards
Tomek
It should work while you creating APP on twitter make sure you have proper read/write permission.
thank you! work!
Thank you for this tutorial!
How can I get line breaks into the tweet?
Thanks in advance!
Dave
Did you tried with new line char.
something like.
$connection->post('statuses/update', array('status' => 'This is a test message. \n is used for new line character.'));
Yes.
I tried:
$connection->post('statuses/update', array('status' => 'Another \n NewLine \n Test'));
This was the result: https://twitter.com/berlindave/status/537670834090172416
(And I tried many other new line characters, like \r\n, \u000a, or %0D%0A …
… none of them worked.
Any further ideas?
Officially twitter do not provide more details about their line break features.
I read it some where this available through Twitter web client and Twitter’s official apps only.
I’m not sure this feature available in Oauth API.
Ok, thank you for your reply! I’ll so further investigations … 😉
Admiring the persistence you put into your site and in depth information you provide.
It’s great to come across a blog every once in a while that isn’t the same unwanted rehashed information. Wonderful read!
I’ve bookmarked your site and I’m including your RSS feeds to my Google account.
Thank you a bunch for sharing this with all people you actually understand what you are talking about!
Bookmarked.
get('search/tweets', $query);
}
//Change *SEARCHWORDS* to what you want to search for
$query = array( "q" => "-RT *SEARCHWORDS*",);
$results = search($query);
foreach ($results->statuses as $result) {
$retweet = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
$retweet->host = "https://api.twitter.com/1.1/";
$status = $result->text;
$tid= $result->id;
//Make Sure its under 140 or trim it.
if(strlen($status) > 140) $status = substr($status, 0, 139);
// Post the retweets to Twitter
$retweet->post('statuses/retweet/'.$tid.'');
// Output the retweets
echo $result->user->screen_name . ": " . $result->text . "\n";
}
?>
Amazing Job, everything works so good but is there any way to get that status id in return to get tweet url
Try this,
$status = $connection->post(..);
print_r($status);
i think this script is no more working :/
The Code is works fine,
Twitter recently changed the APP Permission based on Mobile number verification.
So your twitter account does not have mobile number the APP you created previously many change to read only permission . so using that keys you can’t post to twitter.
check the status and print the return array you will get error details. Also check my twitter account you can saw latest tweets using this api.
Make sure your key copy pasted correctly without white spaces or special char
i have asked twitter for read and write prmsn and i have done everything correctly …
Check this link https://apps.twitter.com/ and make sure your app have proper permission. Also make sure the oauth keys are updated while changing the permission. Its should work.
did you print the array any errors it shows ?
Hello,
Nice post. But I am struggling with ” post tweet along with image from my website” using twitter api.
Will you tell how can I do this. I search Google for solution, but not found any working code.
It will be very helpful. Thank you.
You can check below link it have some example codes.
https://dev.twitter.com/docs/api/1.1/post/statuses/update_with_media
I know its very delayed but may help you.
Post to twitter with media file using Oauth API
http://www.walkswithme.net/tweets-with-media-file-using-oauth-api-in-php
nice but i want only send tweet to my twitter application,
please sir iam finding this mathed from back 2 years.
please sir solve my this problam.
advance thanks.
waiting for your reply…
hi, This code is really help me , bt can you tell me how i post in twitter with image. This is very helpfull for me.
Sorry for the delayed reply ,
you can check below link it have some example codes.
https://dev.twitter.com/docs/api/1.1/post/statuses/update_with_media
I know its very delayed but may help you.
Post to twitter with media file using Oauth API
http://www.walkswithme.net/tweets-with-media-file-using-oauth-api-in-php