Blog

How to post Tweets & Images with PHP

twitter featured
A nice simple way to insert TwitterOAuth Library in your PHP project. Post statues w/o images on Twitter from your project in PHP.

Prerequisite

1. Twitter Account

2. Twitter App

3. Twitter API library

A Twitter account is required to create a Twitter App.

You cannot use Twitter API directly (using your username and password), so you have to create a Twitter App.

After you create your Twitter App, you get Consumer Key (API Key), Consumer Secret (API Secret), Access Token, Access Token Secret and you have to use them as credentials to communicate using Twitter API.

Additionally, you need a library for your programming language (PHP in my case) to easily use Twitter API. There are many libraries available. In this blog, I am using TwitterOAuth.

Unlike many Twitter API libraries, TwitterOAuth doesn’t provide a custom function for every API method. Instead, there are a couple of generic(e.g get, post, delete) functions so that as Twitter adds features to the API you don’t need to update the library. Here is an example of GET statuses/home_timeline.

How to create a Twitter App

1. To create a Twitter App click this link, https://dev.twitter.com/apps.login-page

2. Press “Create New App” button.create new app

3. Complete the “Create an application” form with right data and press “create your Twitter application” button.create-an-application

4. Go to “Permission” TAB and set Type of Application to ‘Read and Write’. Then press “Update Settings” button.update permissions

 

5. Go to “keys and Access Tokens” TAB and press “Create my access token” button. This will generate your access token.create my token

6. From “keys and Access Tokens” TAB, you can get your required credentials.credentialsImplementation to post tweets in PHP

Follow the Abraham/TwitterOAuth document. So First, You need to install the library in your project. After completing the installation and import you can start coding.

1. Define your credentials into your project’s configuration file from where you can access them.

2. Create an object of TwitterOAuth class to access its functions.

3. If you just want to tweet on your twitter account, make below API request once you verified your credentials.

4. Similarly, check below code if you want to post an image.

Share on linkedin
Share on twitter
Share on facebook
Share on email

One Reply to “How to post Tweets & Images with PHP”

Leave a Reply

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

Categories

Newsletter

Get In Touch

Our team is happy to answer your questions. Fill out the below form and we'll be in touch as soon as possible.

Send us a Message

Contact Information