X Twitter v2 API Automatin with PHP and 3rd Party Lib

Sometimes you may find third party libs helpful to use.

In this tutorial, we will use Abraham's lib for building Twitter automation


1. Download the latest version of "composer" from here:

https://getcomposer.org/download/


2. Create a folder like "twitterbot" and open it in VS Code.


3. Open a new terminal in VS Code, and install Abraham's lib using the following command:

"composer require abraham/twitteroauth"


4. Create a php file, in which your code will work. Let's name it as "index.php"

If you are successfully up to here, below is what you will see on your VS Code:


5. Now, copy the code below, and paste it in the index.php.

<?php
require "vendor/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;

$twitterConsumerKey="YOUR_API_KEY";
$twitterConsumerSecret="YOUR_API_SECRET";
$twitterOauthAccessToken="YOUR_ACCESS_TOKEN";
$twitterOauthAccessTokenSecret="YOUR_ACCESS_SECRET";

// Connect
$connection = new TwitterOAuth($twitterConsumerKey,             // Your API key
                               $twitterConsumerSecret,          // Your API secret key
                               $twitterOauthAccessToken,        // From your app created at https://developer.twitter.com/
                               $twitterOauthAccessTokenSecret); // From your app created at https://developer.twitter.com/

// Set API version to 2                          
$connection->setApiVersion('2');

// POST the tweet; the third parameter must be set to true so it is sent as JSON
// See https://developer.twitter.com/en/docs/twitter-api/
tweets/manage-tweets/api-reference/post-tweets for all options
$response = $connection->post('tweets', ['text' => 'Hello Twitter'], true);

// Post Update
// $response = $connection->post('statuses/update', array('status' => 'Test Tweet'));

var_dump($response);

6. Okay, now we need to open a developer account on Twitter.

Go to the url below, and choose a plan. Free one may be good for beginning:

https://developer.twitter.com/en/products/twitter-api


7. Go to your dashboard and click App Settings, as you see on the image below:





8. Click "Edit" under "User Authentication Settings".



9. Choose "Read and Write", and "Web App, Automated App or Bot" among options. 

Fill in "Callback URI" and "Website URL", and click "Save".

You can write something like "http://localhost" and "http://localhost:3000/index.php". Write whatever is aligning with your situation. Wrong information here is not expected to hinder your tests.



10. Now, go to "Keys and Tokens" tab. Regenerate API Key, API Secret, Access Token and Access Secret. Copy and paste each to the related fields within the code in your index.php

Here you should know that, whenever you change your permissions, you have to regenerate your keys for your code to work. Otherwise, you will get an error like this:

"
Your client app is not configured with
the appropriate oauth1 app permissions for this endpoint."



Now, go to your index.php over your server, and you will see your first outomated post on your twitter account as "Hello Twitter".

You can dive into X Developer API and use any kind of available requests for your project.

Happy coding!..

Comments