2. TWITTER API
Twitter API is tools that provided by Twitter to
facilitate Twitter Developer to get data for their apps
API (short for Application Programming Interface) is
a system that allows other applications to talk to an
application- receive information from it and send
information to it (iag.me).
Twitter API can be developed in PHP, Pyton, Java, etc.
This tutorial used PHP to be connected with Twitter
API (based on iag.me)
3. 1. GET YOUR TWITTER API ACCESS!
It is important step before you get data from Twitter
How?
1. Go to https://dev.twitter.com/apps/new
2. Login with your Twitter account
3. There are some textfields that you have to fill in, i.e. Application Name,
Description of your apps, website, callback URL (you can leave this empty).
4. Accept the Term of Service and enter the captcha.
5. Click ‘Create your Twitter Application’
6. You can see the consumer key (CK) and consumer secret (CS) in Oauth settings.
7. Click ‘Create my access token’ to get your access token (AT) and access token
secret (ATS).
8. Now, you have anything you need to access Twitter API. CK, CS, AT, and ATS
should be copied in your apps (don’t forget to make it as secret since every
developer will have different code).
4. 2. LET’S USE TWITTER API!
1. Get Twitter Wrapper tools to facilitate you to connect Twitter API. In this
tutorial, I used PHP Wrapper Script that available in
https://github.com/J7mbo/twitter-api-php/archive/master.zip . It works
for Twitter’s API v1.1. This wrapper require you to already installed cURL.
If you want to run your app in localhost, make sure the root certificates
installed for cURL is avaliable in your xampp settings.
2. After you’ve download the wrapper, unzip it. You will see file
‘TwitterAPIExchange.php’. This file contains of how our app can be
connected with Twitter API.
3. Make a PHP file to connect the wrapper and our app. Let’s name it as
‘myTweetApp.php’. Place this file in the same place with
‘TwitterAPIExchange.php’.
4. In our file ‘myTweetApp.php’, we need to use ‘TwitterAPIExchange.php’
so we add
5. 5. We need to set our access tokens by adding it to the $settings array
$settings = array(
'oauth_access_token' => "YOUR_ACCESS_TOKEN",
'oauth_access_token_secret' => "YOUR_ACCESS_TOKEN_SECRET",
'consumer_key' => "YOUR_CONSUMER_KEY",
'consumer_secret' => "YOUR_CONSUMER_SECRET"
);
6. We need to give the PHP Twitter wrapper the URL so it can make the
correct API request for us.
$url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
7. GET or POST?
$requestMethod = "GET";
8. What do we need?
$getfield = '?screen_name=iagdotme&count=20';
*for more details about any query that we can request, visit: https://dev.twitter.com
6. 9. We need to make the call using the PHP Twitter Wrapper
$twitter = new TwitterAPIExchange($settings);
$string = json_decode($twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest(),$assoc = TRUE);
10.Make the output
foreach($string as $items)
{
$username = $items['user']['screen_name'];
$tweet = $items['text'];
echo "Username: ". $username."<br />";
echo "Tweet: ". $tweet."<br />";
}
11. Program Run!