twitter_api

Introduction

This time we decided to take a look at the use of Twitter API 1.1 and PHP. Since the use of API 1.1 and oAuth authentication is necessary for API we will show you a couple of useful tricks for easy use.

What we need:

Preparation

Upload oAuth.php and twitteroauth.app files on your server. Then create a new blank file in the same location / folder. Generate the API keys before programming the main file.

Generating the Twitter API keys:

1) Go to https://apps.twitter.com/ and log in with your twitter account

2) Click on “Create new app” and fill in the following data

3) Generate the oAuth access details

Main PHP file

You can download all project files here.

Create an empty PHP file (for example twtest.php) in the same directory where oAuth.php and twitteroauth.php are located.

<?php
require_once('twitteroauth.php');

// place your own keys
$consumerKey = 'Qm3IsX9Wxr6X390QYf3V7Y7fDrZWnNzl4jy681F5Q54ILfAWdD0F1W9gSBr5lZyV';
$consumerSecret = '6i6WNcsX2LBIAef7nIsZkmy60NZMj152R7hExR4U';
$oAuthToken = '2o1852p5AuaeLoeUlqmUcqT408e28Yy3UXno7K0n';
$oAuthSecret = '71a796Ky7798595ia9H4K13D0I3S12bY34d3gr5Y';

// class creation
$tweet = new TwitterOAuth($consumerKey, $consumerSecret, $oAuthToken, $oAuthSecret);

Example 1 – get twitter followers

// get 5000 followers of user
// change "screen_name" for your twitter account
$result = $tweet->get('followers/ids', array('screen_name' => 'wpressio', 'cursor' => -1, 'count' => 5000));

$followers = json_decode($result);
$followers = $followers->ids;

var_dump($followers);

Example 2 – get twitter friends (following)

// get 5000 followers of user
// change "screen_name" for your twitter account
$result = $tweet->get('friends/ids', array('screen_name' => 'wpressio', 'cursor' => -1, 'count' => 5000));

$following = json_decode($result);
$following = $following->ids;

var_dump($following);

Example 3 – different users between following and followers

// get 5000 followers of user
// change "screen_name" for your twitter account
$result = $tweet->get('followers/ids', array('screen_name' => 'wpressio', 'cursor' => -1, 'count' => 5000));

$followers = json_decode($result);
$followers = $followers->ids;

// get 5000 followers of user
// change "screen_name" for your twitter account
$result = $tweet->get('friends/ids', array('screen_name' => 'wpressio', 'cursor' => -1, 'count' => 5000));

$following = json_decode($result);
$following = $following->ids;

$difference = array_diff($following, $followers);

var_dump($difference);

Example 4 – Follow twitter user

// replace user_id with real number
$result = $tweet->post('friendships/create', array('user_id' => 12345678900));

For get twitter user_id form username, please follow this url.

Example 5 – Unfollow twitter user (destroy friendship)

// replace user_id with real number
$result = $tweet->post('friendships/destroy', array('user_id' => 12345678900));

For get twitter user_id form username, please follow this url.

Conclusion

So this is how to create the simple twitter app and connect it to your custom PHP script. We will focus on sending the messages using the twitter API and how to create a simple thank you script for all your followers in the next topic.

Join the community
Comments are closed.

Our sponsors

We’re social

New in Lab

New Collection

Sorry, nothing found

We're sorry, but the your query has no result.

New Tutorial

Sorry, nothing found

We're sorry, but the your query has no result.

Knowledge base