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
	// 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:



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

  • 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
      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
        Wednesday, November 4th 2015 at 6:56 AM

        Nice Post

        • Replying to Test

  • 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
      Sunday, July 12th 2015 at 10:21 AM

      Never mind! :)

      • Replying to James

  • 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
      Saturday, September 12th 2015 at 2:28 PM

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

      • Replying to Anders

      • Adam Konieska
        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
    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
      Thursday, November 12th 2015 at 8:24 AM

      Thanks Gary! That's awesome to hear.

      • Replying to AK

  • 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?


    • Replying to David

    • Adam Konieska
      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
    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
      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

  • bryangag
    Friday, March 9th 2018 at 10:56 PM

    Приветствую Вас!
    Мигает ошибка 255 на частотном преобразователе FR-A540L-G220K-NA. Похожие приборы ATV61WD55N4A24, CIMR-G7A47P51, VFD002L21A установленные на компрессорах функционируют.
    Расшифровку нашел через google на сайте prom elektrik <a href="http://prom-electric.ru/articles/1/1/">Сервисный центр по ремонту преобразователей частоты Петербург</a>. Был неисправен контроллер.
    Всем успехов!

    • Replying to Bryangag