Last Updated on April 29, 2021 by Amit

Unfollow twitter users using Twitter PHP API

In this short tutorial I will show you how we can unfollow Twitter users using Twitter PHP API .
We are using the latest Twitter php auth in this tutorial.

What you need to get started?

To unfollow Twitter users from a website , you must have the following things ready :

  • A PHP web server (PHP 5.4 or above) .
  • Custom domain with SSL
  • Live Twitter API app
  • Twitter PHP oauth code

The first thing you need to get started with Twitter unfollow is the Twitter login. You need to log Twitter users to your website.
You can read here how to implement Twitter login for PHP and post tweet direct from a website.

See Sign in with Twitter Demo

Follow these simple steps to implement Twitter login on your php website:

  • Create an application on developer.twitter.com/en/apps
  • Copy the API key and API secret for your Twitter developer app because you will need it for your Twitter login code.
  • Download the Twitter auth PHP source code from this link and upload/unzip it on your web directory.
  • Modify the index.php file.

The only one file you need to modify in order to be able to log Twitter users to your website is index.php .

Find this file in your twitter login project directory and update API key, API secret and Callback URL fields with your app.

Here is the block of code from the index.php that needs to be updated :

define('CONSUMER_KEY', 'add_your_key_here');
define('CONSUMER_SECRET', 'add_your_app_secret_here');
define('OAUTH_CALLBACK', 'https://example.com/demo/twitter/login/callback.php');

Update these three lines and your site will be ready to log Twitter users.

Unfollow twitter users from website using API

Now that you have successfully implemented Twitter Login for your website. The next step is to create a code that can unfollow twitter users from a logged in twitter account.

You can use POST friendships/destroy method provided by Twitter API to unfollow twitter users using their screen_name or user_id .

For example (using screen name) :

$connection->post('friendships/destroy',array('screen_name' =>'twitter'));

Or using “user id of the user to unfollow”

$connection->post('friendships/destroy',array('user_id' =>'123456'));

To unfollow Twitter users using the twitter source code provided by us, create a new file named unfollow.php in the same directory as your index.php and put the following code :


require 'autoload.php';
use Abraham\TwitterOAuth\TwitterOAuth;
define('CONSUMER_KEY','your key');
define('CONSUMER_SECRET', 'your_app_secret');
define('OAUTH_CALLBACK', 'https://example.com/demo/twitter/login/callback.php');
if (!isset($_SESSION['access_token'])) {

$access_token = $_SESSION['access_token'];
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
$user = $connection->get("account/verify_credentials", ['include_email' => 'true']);
// $user1 = $connection->get("https://api.twitter.com/1.1/account/verify_credentials.json", ['include_email' => true]);

//$connection->post('statuses/update', array('status' =>$tweet));
$connection->post('friendships/destroy',array('screen_name' =>'twitter'));

echo "User unfollowed";


This code will unfollow an specified account Twitter from the following list of your twitter account. You must be logged in to Twitter from your developer app otherwise the code will redirect you back to index.php for sign in with Twitter .

I hope this article was helpful. Thanks for reading!

Related : Sign in with Twitter for PHP with demo +code

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.