Categories
PHP Twitter API

How to unfollow twitter users using Twitter php API

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 :

session_start();

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'])) {
header("Location:index.php");}
else
{

$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

Categories
Twitter Twitter API

Post tweet using Twitter PHP API | Live demo with zip code archive

Post Tweet using Twitter API

Do you want to post tweets from your website to your Twitter account?
If yes, then read on!
This post is for you.

In this post I will show you how to post a tweet using Twitter API for PHP. We will use a live demo here to explain the process in detail.
If you want to see the live demo first , then Visit this URL and log in with your Twitter account to see how it works.

What you need to get started?

Before we get started, here are a few requirements that you must meet in order to implement Tweeting from your php website

  • A live web server with PHP 5.4 or above
  • Domain with https certificate
  • Twitter API app.
  • Twitter Auth for PHP

1) Your webserver must have installed PHP version 5.4 or above.

2) Your domain must be SSL enabled.
All API calls from your website to twitter server must be using a secure URL scheme.
If you don’t have an SSL certificate , you can buy one or get a free SSL certificate for your domain from Cloudflare .

3) Create an application on developer.twitter.com for your project.
You will need this application to log twitter users to your website.

To be able to create your first app the twitter developer platform, first you will need to apply for a developer account. Create your developer account on Twitter and send it for review. Once its reviewed you can then create your first app.
3) Download twitter oauth on your server. You need twitter oauth to integrate twitter on your PHP website. You can download the popular twitter Auth library from Here .

What’s next?

We will first implement the Twitter login so that we can log Twitter users to our website.

Download the zip source code from This link and upload it to your web directory.
You can use any Online ftp ediors to upload and unzip the zip archive on your server.

After unzipping the source code file, you need to modify only one single file index.php .
Open the file in your editor and change App key , App secret and callback URL with your values.

You will find API key andsecret on the dashboard of your Twitterper application.
The callback URL is the URL of your site where Twitter users will come after successful authorization on Twitter. You need to add this URL to the settings of your app . If you don’t know the exact location of this URL, just find where the callback.php is located in your Twitter login code.

To verify everything is working perfectly fine, just vist the index.php project file in your browser. This will now ask you to login with your Twitter.

Congratulations : you have successfully implemented Log in with Twitter for your php website. Now we have finished our first step. The next step is to Post a tweet from website .

Post tweet using Twitter PHP API

To post a tweet using Twitter API , we use POST statuses/update method provided by Twitter.
To be able to send tweet using Twitter PHP API, you just need to create a new file in the same directory where your Twitter login source code is located.

Create a new file named post_tweet.php and put the following php code in that file :

session_start();

require 'autoload.php';
use Abraham\TwitterOAuth\TwitterOAuth;
define('CONSUMER_KEY', 'Your_consumer_key');
define('CONSUMER_SECRET', 'your_consumer_secret');
define('OAUTH_CALLBACK', 'https://example.com/demo/twitter/login/callback.php');
if (!isset($_SESSION['access_token'])) {
header("Location:index.php");}
else
{

$tweet=$_POST["mytweet"];
$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' =>'Hello world!'));

}

We use statuses/update’, array(‘status’ =>’Hello world!’) method to post tweet from website to Twitter account.

With the php code above, anyone who is logged in to Twitter with your application will be able to post a tweet direct from your website to their Twitter account.
The php code above in the post_tweet.php post a short text tweet “Hello world!” to Twitter.

I hope this article was helpful! Thank you so much reading!

Related : Twitter Login for PHP websites with demo and source code .

Categories
PHP Twitter Twitter API

How to implement Twitter login for PHP websites (2021)

Sign in with twitter for PHP websites

Login with Twitter for php

With Twitter APIs you can log twitter users to your website. This helps reduce the signup process and enables twitter functionalities such as “post tweets” , “follow users” directly from your own web server. You can put a “login with twitter” button on your website to securely log twitter users to your website.

In this post, we will learn how to use twitter login for a PHP websites.

With the source code provided on this post you will be able to use a “sign in with twitter” button on your website that will allow your twitter users to log in to your site using their twitter credentials.

See the live demo


You can use your real twitter account for the login. Don’t worry your account is safe and your account credentials are saved only in the sessions between this site and your browser. You can revoke access to this application any time from your twitter account settings.


Sign in with Twitter

Download the source code :

You can download the “Sign in with twitter for PHP” source code as zip file from here and upload it to your webserver using any web based ftp client.
The only one file that you need to modify is the index.php .

Open it in your editor and change the Consumer key, Consumer sercret and callback URL with your own values .

Find the following three lines in the index.php file and replace the values

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

Update these three lines and you will be ready to use Twitter login on your php website.

What you need to get started with Twitter PHP APIs

  • PHP (5.4 or above)
  • Custom domain with SSL cert.
  • Twitter API App
  • Twitter oauth source code

Things to setup before you start

PHP server :Your php version must be 5.4 or greater to use twitter login.

Custom domain with SSL cert :In order to make secure API calls from your website to twitter, you need an SSL certificate for your domain. Make sure your URLs use https perfix.

Twitter API app : you need a twitter developer API Api to get Consumer token and secret. See instructions bellow on how to create one.

Twitter oauth source code : Download the zip source code file file . Upload and unzip it to your web directory.

Create a twitter developer API app

API app will provide you “consumer token” and “consumer secret” that will be used to verify your app on the twitter oauth code.

In order to be able to create an application on the twitter developer platform, first of all you need to submit an developer application and once its approved you can then create your first App.

To create your first app , go to https://developer.twitter.com/en/apps and click on the crete a new app button.

Creating an app on the developer platform is easy and it only takes a few seconds to make the app Live.

Fill up all the required app details

Twitter Dev app

  • App name : The name of your Application ie: “Demo app”.
  • App description : The short description of your Application in 10 to 200 characters. Write a short description about your application.
  • Website URL : Add the URL of your website ie https://exmple.com/ . You can add the URL on which you are hosting the twitter oauth code. Add https://yoursitename.com/twitter/login if you are using the oauth source code provided on this post.
  • Click on the “enable sign in with twitter checkbox. If you do not check mark this then your application will not be able to authorize twitter login requests.
  • Callback URL : callback URL is the URL where twitter users will get redirected to after completing the twitter login or after pressing the “Authorize app” button.If you are using the source code provided by us then add http://yoursitename.com/twitter/login/callback.php to this field.

Twitter developer app for PHP login
The last three fiels except the “Tell us how your app will be used” are optional fields you can leave them blank.
You need to write a short description about how you will use the application. Example : Write “I want use this for a simple login” . This field is required and only visible to twitter employees.

Finally click on the create button. This will create your app and you can now copy the “customer token and secret ” from the settings. You need these app token and secret to update the config.php file of the source code.

Download the Twitter oauth source code

Now that you have app token and secret your next step is to download the source code zip file from the link mentioned above. Upload and unzip it to your document root folder. The zip file will create a new folder named “twitter/login” on your root directory. All the source files are located inside the twitter directory.

The only one file that you will need to modifyis index.php. You can find the index.php file in Twitter/login folder of your twitter source code.
Go to /twitter/login on the ftp editor and open index.php for writing.

Index.php contains your consumer token/secret and callback URL
[code]
define(‘CONSUMER_KEY’,’Add_your_key_here’);
define(‘CONSUMER_SECRET’,’add_secret_here’);
define(‘OAUTH_CALLBACK’, ‘https://example.com/twitter/login/callback.php’);
[/code]
Replace define values with your own values .
You can find your application consumer key and secret on Keys and token menu of your Twitter API app.
Twitter API app key and token dashboard


That’s it.
Now you can go to “yoursite.com/twitter/” to check your sign in with twitter demo.

Twitter user properties

User properties are used to display user informations on a web page. To display profile informations of logged in twitter users you can use the following properties provided by Twitter API.
[code]
echo “User name “.$user->name;
[/code]
The php code above will print twitter name of the user who is logged in to your page using Twitter API.

A list of twitter user properties:

  • name – the full name of the user
  • screen_name – screen name of the twitter user ie. “foobar” without the leading “@”.
  • profile_image_url_https – URL of the twitter user profile pic with secure https scheme.
  • statuses_count – the number of tweets posted by the user.
  • followers_count : follwers count of the user.
  • friends_count – following count of the user.

To see a complete list of Twitter user properties, you can print the $user variable (after authorising the user) .
code>print_r($user);