16
Feb
2015

Post to Twitter Via API Using Codebird-PHP

posted Monday, February 16th 2015 at 8:57 AM by

Auto Tweet Using PHP

Do you have a website with frequent updates? Maybe you're driving traffic to those updates via Twitter? Push those updates directly to Twitter via the API and PHP!

Initial Setup

First, make sure your web server is running PHP 5.3 or newer. You'll also need the php CURL extension and the php OpenSSL extension. If you need to check your system, write a phpinfo(); to see what you're currently running.

Next, head over to Git Hub to download the Codebird-PHP Twitter library.

Finally, once you have the Codebird library, you'll want to upload codebird.php and cacert.pem to your server. Note that cacert.pem is the certificate needed. Putting them in a functions, includes, or other secure directory is recommended.

Configuring Your Twitter Account

Start by heading over to https://apps.twitter.com/ and click "Create New App" to get started. Then fill out the basic details about your app.

Create a Twitter Application

Next, click the "Permissions" tab and update the settings to Read and Write.

Twitter Application Settings

Last of all bounce over to the "Keys and Access Tokens" tab and note your keys. You'll need these for the next step in configuring Codebird.

Twitter Application Keys and Access Tokens

Start Auto Tweeting Via PHP

Now that everything is ready to go, you can start using Codebird to send Tweets. Here is some sample code to send a message:

function tweet($message) {

	// add the codebird library
	require_once('codebird.php');
	 
	// note: consumerKey, consumerSecret, accessToken, and accessTokenSecret all come from your twitter app at https://apps.twitter.com/
	\Codebird\Codebird::setConsumerKey("consumerKey", "consumerSecret");
	$cb = \Codebird\Codebird::getInstance();
	$cb->setToken("accessToken", "accessTokenSecret");
	 
	$params = array(
	  'status' => $message
	);
	$reply = $cb->statuses_update($params);

}

//example call

tweet("Autopost to #Twitter with a #PHP API, check it out here: http://bit.ly/163eHsL");

Note that Twitter's 140 character limit includes URLs when posting via the Twitter API. You can (sort of) get around this by using a URL shortener like bit.ly, but more on that later in my bit.ly PHP API post.

Final Thoughts

That's all there is to it! Now you can start tweeting automatically using PHP. Got a question or trouble with the code? Let me know in the comments. Happy tweeting!

Share This:

Tags:

Comments:

View (14) Comments Post a Comment
  • Replying to Adam Konieska on Post to Twitter Via API Using Codebird-PHP







  • rendy
    Rendy
    Monday, June 29th 2015 at 1:41 AM

    It's working with my localhost, but when i upload to my website, why it doesn't working. Thanks, please reply to my email.

    • Replying to Rendy







    • Adam Konieska
      AK
      Monday, June 29th 2015 at 10:13 AM

      Aww shoot. I'll send you an email and hopefully we can troubleshoot it!

      • Replying to AK







      • test
        Test
        Wednesday, November 4th 2015 at 6:56 AM

        Nice Post

        • Replying to Test







  • james
    James
    Sunday, July 12th 2015 at 9:29 AM

    "You'll need these for the next step in configuring Codebird." - What next step? I seem to be missing something, which may be causing the following problem:

    When I run the tweet() function above it displays codebird.php as a webpage.

    • Replying to James







    • james
      James
      Sunday, July 12th 2015 at 10:21 AM

      Never mind! :)

      • Replying to James







  • anders
    Anders
    Saturday, September 12th 2015 at 10:05 AM

    Cool! Works perfectly. How do you tweet like this with a image? :-) Thanks!

    • Replying to Anders







    • anders
      Anders
      Saturday, September 12th 2015 at 2:28 PM

      Nevermind, found your other post that describes tweeting with image :)

      • Replying to Anders







      • Adam Konieska
        AK
        Saturday, September 12th 2015 at 2:55 PM

        Awesome Anders, glad you found it. Here's the link if anyone else is also looking: http://www.adamkonieska.com/archives/tweet-images-via-api-using-codebird-php/

        • Replying to AK







  • gary
    Gary
    Wednesday, November 11th 2015 at 9:25 PM

    THANK YOU! I've been trying for a while to find a script that does this easily. Yours works like a charm!!

    • Replying to Gary







    • Adam Konieska
      AK
      Thursday, November 12th 2015 at 8:24 AM

      Thanks Gary! That's awesome to hear.

      • Replying to AK







  • david
    David
    Thursday, March 3rd 2016 at 1:02 PM

    I got codebird.php to work but it's sending posts automatically without me having to create a cron job. Where in the code can I change the time of the auto posting?

    Thanks.

    • Replying to David







    • Adam Konieska
      AK
      Thursday, March 3rd 2016 at 5:40 PM

      Hey David. The code (as shown here) wouldn't run on its own. The code would do a tweet every time the tweet() function is executed - so if someone is accessing a URL that causes the script to run, it would post. If you wanted to set something to post at a specific time you could do it using a cron job.

      Hope that helps!

      • Replying to AK







  • malik
    Malik
    Friday, August 5th 2016 at 7:37 AM

    I want to post a tweet on behalf of my website users. They have already give me auth_token and auth_token_secret but the obove code is not working for it. and the Error is HTTP_CODE is 401.

    • Replying to Malik







    • Adam Konieska
      AK
      Friday, August 5th 2016 at 1:58 PM

      Hey Malik, the 401 status code is a pretty good indicator that there is a problem with your auth credentials. More details on the 401 status code.

      • Replying to AK