How to post Tweets & Images on Twitter using TwitterOAuth & PHP


Prerequisite

  1. Twitter Account
  2. Twitter App
  3. Twitter API Library

 

There are many libraries available in PHP. In this blog, I am using TwitterOAuth because it is simple and well documented.

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.

GET https://api.twitter.com/1.1/statuses/home_timeline.json?count=25&exclude_replies=true
$statuses = $connection->get("statuses/home_timeline", ["count" => 25, "exclude_replies" => true]);

Don’t worry if you don’t have a Twitter App yet, we’ll help you create one.

In order to use Twitter API directly (using your username and password), you have to have a Twitter App.

But, to create a Twitter App you will need a Twitter account. So first, signup on Twitter. It’s easy!

 

How to create a Twitter App

Step 1: To create a Twitter App click this link, https://dev.twitter.com/apps. Which will take you to the twitter login page. Login to proceed.

Step 2: Press the “Create New App” button.

 

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

submit twitter app form

 

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

Step 5: Go to “Keys and Access Tokens” TAB and press the “Create my access token” button. This will generate your access token.

Step 6: From “keys and Access Tokens” TAB, you can get your required credentials.

After you create your Twitter App, you will get a Consumer Key (API Key), a Consumer Secret (API Secret), an Access Token and an Access Token Secret. These credentials will be used to communicate between your app and the API, so do not share them with anyone.

 

Implementation to post tweets in PHP

Open the Abraham/TwitterOAuth document. Firstly, You need to install the library in your project. You can install the library using the Composer or manual downloading it and place it inside your project. Follow the instruction as in the documentation.

After completing the installation and import you can start coding.

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

define('CONSUMER_KEY','YOUR_CONSUMER_KEY');
define('CONSUMER_SECRET','YOUR_CONSUMER_SECRET');
define('OAUTH_TOKEN','YOUR_AUTH_TOKEN');
define('OAUTH_SECRET','YOUR_AUTH_TOKEN_SECRET');
define('SCREEN_NAME','YOUR_PROJECT_NAME');

Step 2: Then, create an object of TwitterOAuth class to access its functions.

require("config.php");
require("vendor/autoload.php");

use Abraham\TwitterOAuth\TwitterOAuth;

$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_SECRET);
$content = $connection->get("account/verify_credentials");

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

$statues = $connection->post("statuses/update", ["status" => "hello world"]);

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

require("config.php");
require("vendor/autoload.php");

use Abraham\TwitterOAuth\TwitterOAuth;

$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_SECRET);
$media1 = $connection->upload('media/upload', ['media' => '/path/to/file/kitten1.jpg']);
$media2 = $connection->upload('media/upload', ['media' => '/path/to/file/kitten2.jpg']);
$parameters = [
    'status' => 'Hello Word with Images!',
    'media_ids' => implode(',', [$media1->media_id_string, $media2->media_id_string])
];
$result = $connection->post('statuses/update', $parameters);

Hope this was helpful.

checkout our Best laravel packages to help you extend your project’s functionalities easily.

 

Tags:  

2 Responses

  1. Thanks man! Wicked helpful, it has been probably 5 years or more since I coded a twitter bot, this helped get me there, hello tweet worked, now I am onto coding an automated / random meme generator by selecting from a directory of pics, and an array of text data, and pairing them together! (I will probably use something like imagemagik to do the trick) Gonna be awesome / wicked simple but effective!

Leave a Reply

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