skip to Main Content

Programmatically Publish Tweet On Twitter Using PHP

Programmatically Publish tweet on Twitter using PHP


The Twitter is the most popular social network. Twitter is the best for marketing of your product or blogs article. Twitter is the social network many times we don’t have time to manually publish tweet on time. So let’s how we can Programmatically publish tweet to Twitter using PHP

Let’s see how we can publish tweet on the Twitter platform using PHP programmatically. We used the TwitterOAuth package for publishing tweet on the Twitter. So let’s First installed the TwitterOAuth package on your PHP Application.

composer require abraham/twitteroauth

After, the package was installed let’s start the written code for publishing a tweet.

Publish tweet to Twitter

<?php
require "vendor/autoload.php";

use Abraham\TwitterOAuth\TwitterOAuth;

$connection = new TwitterOAuth(
    CONSUMER_KEY, 
    CONSUMER_SECRET, 
    ACCESS_TOKEN, 
    ACCESS_TOKEN_SECRET
);

$content = $connection->get("account/verify_credentials");

$tweetPost = $connection->post("statuses/update", ["status" => "Hello, I'm currently feeling Awesome");

if ($connection->getLastHttpCode() == 200) {
    echo "Tweet Published Successfully.";
} else {
    echo "Something went wrong please try again.";
}

Now let’s create the Twitter app for getting the API key at “https://apps.twitter.com”. Then set API key on above PHP Script and replace the status tweet as you want.

Now let’s run the script for publishing tweet programmatically on the Twitter.

write the comment below if you face any issue.

Saroj

I am a web designer and developer part-time blogger. Write something on some free time. Find my every day thought here :)

Leave a Reply

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

Back To Top