Use of Twitter in disaster management (part-1)

Geospatial Lab’s one of the research going on is the use of social media’s API (application programming interface) and mobile services in post/pre disaster management for disaster response, relief and recovery.

For the time being our focus is earthquake. Earthquakes are inevitable to humankind. In case of these type of rapid onset disasters, it’s almost impossible to predict the extent to which it effect the economy, society and the environment. Although it’s hard to predict, proper post disaster management helps to minimize its effect. The medium depends upon the economic level of society, if it’s developed nations social networks are considered best (Lessons Learned in Using Social Media for Disaster Relief – ASU Crisis Response Game) . On the contrary, the society with less accessible to internet, use of mobile is a best medium ( TERA (Trilogy Emergency Relief Application)). Taking this into consideration, social media and mobile phones can be a medium for earthquake response, relief and recovery.

Our assumption before going through the article is you should have a basic understanding of webhost, HTML, PHP. Here in this article we will be discussing about use of twitter REST API 1.1. Further posts will come as a part of sequence of this post about using facebook and twitter apis integrated with mobile services. Now, you can proceed on with the steps:

1. Creating an application

a. The foremost step is going to https://apps.twitter.com and signing in with your twitter account. If you don’t have a twitter account, sign up and go to above url.
create_app_signin

create_app_signin1
b. If you are signing in, it’s time to create a new application. For creating an app click on Create New App on the top right hand side of the page.

create_app_newc. Enter your application name, description and website: Here name refers to your application name which should be unique, description refers to the answer of what, how and why of your application and website field seeks the name of your website which you will use with the twitter application. If you are using it on a personal use you can use blog’s url as well.

create_app_register1

d. Agree the terms and conditions and click on “create your new twitter application”. You will get the details (permissions, api keys, …).

 

create_app_api

e. Under keys and access token tab, create access token by clicking on “create my access token” which will be used for authentication later. Now you are done with creating secrets. Next task will be using these secrets in your application.

create_app_create_access_token

create_app_access

create_app_details

2. Using twitter REST API 1.1 : Using twitter API is easy but it’s only limited to web host. You should have atleast one webhost that works even without domain name as well.

a. Get a php wrapper for using twitter api : Go to https://github.com/J7mbo/twitter-api-php and download a whole project. Take a brief look before using it. There you will also get an example of using it (index.php). Upload the index.php and TwitterAPIExchange.php into the webhost.

github

Thanks to Ian Anderson Gray for this note.Please Note It’s best to run this on a web server (i.e. not locally) and you may need to check a few things with your host first. Firstly make sure you have a modern version of PHP. I have tested it using PHP 5.2 an above, but really your host should be using 5.4 or higher. You will also need to have cURL installed. Don’t worry about what this is- but the wrapper script requires this to connect to the Twitter API. Most hosting environments should have this as standard- check with your host. You may also want to check that your host has the latest root certificates installed for cURL as I’ve had reports of some hosting hosts not bothering to do this. If Twools doesn’t work, you’ll need to ask your host! Thanks to Tyler Hakes for looking further into this! (iag.me, 1/10/2014)

b. Copy and note down the required parameters for authentication( oauth_access_token, oauth_access_token_secret, consumer_key and consumer_secret) from application’s Keys and Access Tokens tab. The authentication is necessary in order control the use of data.

c. Again have a look into the downloaded file, It has two main files php and index.php. Open index.php and copy the secrets as obtained in previous step.

d. For the time being lets omit the post part as we will cover later. So, The total code is below. You can understand through the comments or if you want deeper understanding you can refer Iag Twitter tutorial or stackoverflow

/** outputs in json format **/
header(‘Content-Type: application/json’);
ini_set(‘display_errors’, 1);
require_once(‘TwitterAPIExchange.php’);

/** Set access tokens here – see: https://dev.twitter.com/apps/ **/
$settings = array(
‘oauth_access_token’ => “……………..B359yt4ghdMLYIvrQLJn7f0jhGDMq5z…….”,
‘oauth_access_token_secret’ => “……………..y2X90OSs3rEFx641Dwc………..”,
‘consumer_key’ => “……………………….5qE6yjQJAV6pu……”,
‘consumer_secret’ => “……………7MZMST439RccyEIPvaJ………..”
);

/** Perform a GET request and echo the response **/
/** Note: Set the GET field BEFORE calling buildOauth(); **/
/ ** url is the api request ; here it is obtaining the followers id **/
$url = ‘https://api.twitter.com/1.1/followers/ids.json’;
/** screen_name is the your twitter account’s unique name e.g. @shumanbaral **/
$getfield = ‘?screen_name=shumanbaral’;
$requestMethod = ‘GET’;
/** connecting with twitter api **/
$twitter = new TwitterAPIExchange($settings);
echo $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();

This example gives the list of follower’s id in json format.

If you want to access the wrapper from multiple sites then include the code below at the top of index.php. Replace “website_name” with your desired website.

$http_origin = $_SERVER[‘HTTP_ORIGIN’];

if ( strrpos($http_origin, “website_name”) || strrpos($http_origin, “website_name”) ){

header(“Access-Control-Allow-Origin: $http_origin”);

}

The output will be in json format as below. I prefer to output in JSON format because it is lightweight and easy to parse data.

{

“ids”: [

     2836814491,

     130408154,

     2516492312,

     2324029284,

     2810232181,

     2811069344,

     1945524625,

     205727811,

     2744473592

],

“next_cursor”: 0,

“next_cursor_str”: “0”,”previous_cursor”: 0,

“previous_cursor_str”: “0”

}

With this we are completed with the basic authentication and usage of twitter api. Next article will be obtaining the tweets with certain hashtag and putting it in a data table.

Advertisements

3 thoughts on “Use of Twitter in disaster management (part-1)

  1. Pingback: Links of the Week (2015/18) | GIS-Blog.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s