Main menu
WalkswithMeAPI PHP Social NetworkingPost to twitter using PHP Oauth API

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 .

Post to twitter using PHP Oauth API

Post to twitter using PHP Oauth API

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.

Post to twitter using PHP Oauth API

Post to twitter using PHP Oauth API

This simple code will post a tweets to my own twitter account like below.

Post to twitter using PHP Oauth API

Post to twitter using PHP Oauth API

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.

Download2068 downloads

31 thoughts on “Post to twitter using PHP Oauth API

    1. 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.

  1. 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

    1. 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.'));

        1. 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.

  2. 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.


  3. 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";
    }
    ?>

  4. Amazing Job, everything works so good but is there any way to get that status id in return to get tweet url

    1. 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

  5. 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.

    1. 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…

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 

FacebookTwitterGoogle+RSS