Ushahidi post tweets to API php example

The below code takes an rss search result from twitter, detects the authors location then posts the information to Ushahidi via the API.

Application I used it inMy School Closures for detecting people who tweet about school closures.  Unfortunately I have to use pipes for the location builder which isn’t elegant as Yahoo Pipes sucks and I wish I never started playing with it.  Feel free to play with the pipe I created to get geo location information from a location name, it is the pipe that has the json_decode.

You will need mysql and a table for the guids (this stops us from spamming twitter for authors we have already located). Make sure you change the configuration settings and create a table in mysql.

The method is simple, the code isn’t pretty and will require polishing but here it is:

<pre>
<?php
//CONFIGURE THE SETTINGS BELOW
//twitter settings
$twitterusername = "mytwitteraccount";
$twitterpassword = "apasswordgoeshere";

//database settings
$host = "databasehostname";
$user = "databaseusername";
$pass = "databasepassword";
$dbname = 'databasename';

// You should create a table called tweetguids in your database
$tablename = "tweetguids";

// Change the below feed to a rss feed similar to below
$rssurl = "http://pipes.yahoo.com/pipes/pipe.run?_id=a9663ebc09e69b9195fb2407fba9f2bc&_render=rss";

// END OF CONFIG

$conn = mysql_connect($host, $user, $pass) or die                      ('Error connecting to mysql');
mysql_select_db($dbname);

ini_set('display_errors', 1);
ini_set('log_errors', 1);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');
error_reporting(E_ALL);

$rss = simplexml_load_file($rssurl) or die("failure");
$count = 0;
foreach ($rss->channel as $chan)
{
$time = $chan->pubDate;
}
foreach ($rss->channel as $chan){$time = $chan->pubDate;}
foreach ($rss->channel->item as $item)
        {
        // only do first 10 records
        if ($count < 10)
                {
                $foundguid=0;
                $guidnew=$item->guid;
                // Check to see if guid exists in db already
                $sql="SELECT * FROM $tablename where guid = \"$guidnew\"";
                $result=mysql_query($sql, $conn);
                while ($row=mysql_fetch_array($result))
                        {
                        $foundguid=1;
                        echo "Foundguid: $foundguid";
                        }
                if($foundguid==1)
                        {
                        echo "We found an alredy existing guid...";
                        }
else
                        {
                        $count = $count + 1;
                        $task="report";
                        $incident_title=$item->title;
                        $guidnew=$item->guid;
                        $incident_title=str_replace(" ","+",$incident_title);
                        $incident_description=$item->description;
                        $incident_description=str_replace(" ","+",$incident_description);
                        $incident_date=date("m/d/Y",$time);
                        $incident_hour=date("H",$time);
                        $incident_minute=date("i",$time);
                        $incident_ampm=date("a",$time);
                        $incident_category="Possible Closure";
                        $location_name="Unknown";
                        $author = $item->author;
                        $author = str_replace(" ","+",$author);
                        $pos = strpos($author,"+");
                        //$author = substr($author,0,$pos);
                        $author = str_ireplace("http://twitter.com/","",$author);
                        $url = "http://$twitterusername:$twitterpassword@api.twitter.com/1/users/lookup.xml?screen_name=$author";
                        //echo $url;
                        $output = simplexml_load_file($url) or die("badauth");
                        foreach ($output->user as $item)
                                {
                                // Get the values out of the XML
                                $location=$item->location;
                                // Clean up the location
                                $location=str_ireplace("-",",",$location);
                                $location=str_ireplace(" ",",",$location);
//                              $location=preg_replace("/[^a-zA-Z0-9\s]/", "", $location);
                                echo "<br/><b>Location content: $location<br/>";
                                // Pass the location to yahoo pipe
                                $location = str_ireplace(" ","",$location);
                                //echo "<br/>Loc: $location";
                                $locationurl = "http://pipes.yahoo.com/pipes/pipe.run?_id=03539616e4cdd62eb15ed26b81e3041e&_render=json&q=$location";

                                 $ch = curl_init();
                                curl_setopt($ch, CURLOPT_URL, $locationurl);
                                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

                                //make the request
                                $json = curl_exec($ch);
                                $arr = (json_decode($json,true));
                                $blah = $arr['value']['items'][0]['name']['loc'];
                                //print_r($blah);
                                $latitude = $blah['lat'];
                                $longitude = $blah['lon'];
                                $location_name = $blah['city'];
                        }

                        $incident_description="Closure reported from twitter";
                        $url = "task=report&incident_title=$incident_title&incident_description=$incident_description&incident_date=$incident_date&incident_hour=$incident_hour&incident_minute=$incident_minu$
                        $posturl = "http://myschoolclosures.com/ushahidi/api";
                        $Curl_Session = curl_init($posturl);
                         curl_setopt ($Curl_Session, CURLOPT_POST, 1);
                         curl_setopt ($Curl_Session, CURLOPT_POSTFIELDS,
                        $url);
                        curl_setopt ($Curl_Session, CURLOPT_FOLLOWLOCATION, 1);
                        curl_exec ($Curl_Session);
                        curl_close ($Curl_Session);
                        //Write to the database
                        $sqlgo="INSERT INTO $tablename VALUES (\"$guidnew\")";
                        $result=mysql_query($sqlgo, $conn);
                }
        }
        //Write anything else to DB just in case anything trails behind
        $sql="INSERT INTO $tablename VALUES (\"$guidnew\")";
        $result=mysql_query($sql, $conn);
        $row=mysql_fetch_array($result);
        //echo "<br/>$sql<br/>";
}
?>

2 thoughts on “Ushahidi post tweets to API php example

  1. Hi, John !

    Sounds interesting… same interesting your school project

    I just setup a test platform to check the different features of Ushahidi… Actually it looks good in tendency

    http://crowdsource.p-forum.org

    I have the feeling, that the Ushahidi Forum is not really helpful as I find mostly only questions and no answers there… so I post my question here.

    Some single options in my Global Map project are yet not working, e.g. “Twitter feed” or email function… but I think, these are smaller issues to get under control…

    I am not a computer programmer, I am just a user… I need clear instructions to setup such systems, as I did with my own blog systems (WordPress 2.8.6 and 3.01)

    http://www.imcradio.net/electronics
    and the new blog which is now in test phase
    http://testblog.india-meets-classic.net

    So what to do with your code ? – I suppose I have to copy it and generate with an Ascii TXT editor (like Notepad) a *.php file, right ? Which name I have to give it ?

    Where to deposite this file via FTP? – – Which subdirectory? – And how to implement it into MySql ?

    My Provider offers me CPanel + myphpAdmin … I dont know how to create new tables in the specific database.

    As I dont want take the risk to crash the web server, better I ask first and I hope for your understanding that you might give me a more detailled orientation about the single steps.

    Tks in advance. Warm regards from Europe/North Germany..

  2. What do you do with the code?

    You need to provide an XML feed that the code can read. I used yahoo pipes to get the feed then my code reads the feed and sends it into Ushahidi.

    You will want the php to be on a cronjob, you can test it by doing php whatever.php

    You can give it whatever name you want

    You implement it into mysql by setting the mysql user/pass and database in the .php

    You can put it wherever you want. It has no file dependancies

    This code is really for properly hosted php/apache/mysql instances and not those that rely on any third party for management.

Leave a Reply

Your email address will not be published. Required fields are marked *