How to Post Tweets Using PHP : Twitter API Part 2


Few months back, I posted an article on Twitter API and explained how to login using Twitter API and PHP. In that article, I just talked about login and getting Twitter access token. In this tutorial, I will take the series ahead to tell you how to do more things on Twitter using Twitter API.

If you have not read the old article, you should read it first. I will not repeat how to get access token again. And access token will be needed to post a Tweet.

In previous article, I talked about a config.php file that contained consumer key and secret. It will be same in this as well.

twitteroauth.php will also be needed here. We obtained twitter_token and twitter_secret In previous article. The method of obtaining and saving in the database will be same.

Post tweet with image


Now you just need the following code to post a new tweet.

This code will publish a new Tweet and will return the Tweet id.

Post tweet with image using PHP

If you want to upload an image, this is also easy. You can upload media files on Twitter via API as well. But there is a size restriction. You can upload a photo of up to 5MB or GIF file of up to 15MB.

Final Words

Now you know how to use PHP and Twitter API to post a new tweet. This code will work until Twitter decides to change something. I have tested this code before posting here. If you still find it hard to implement this, you can always leave a comment.


Written by Deepanker

Deepanker Verma is the founder of Techlomedia. He is a tech blogger, developer and gadget freak.

Leave a Reply

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


fetch latest Twitter trends using PHP

How to fetch latest Twitter trends using PHP: Twitter API Part 2