Creating Databases for Web Applications Twitter example Classwork/homework: Projects
Twitter grab request from senior Example of application involving an XML (non-relational) database Approach: –combination of researching what is available and doing your own coding –I found and could understand a wrapper around the raw Twitter API: started-with-the-twitter-api/ started-with-the-twitter-api/
Requirements…so far grab the latest tweets with a certain hashtag –I used a search on content so will get html5 AND #html5. Can do the restriction. do NOT include retweets and replies –I do a check on the returned results. There MAY be a better way: filter these out earlier. refresh automatically: without user action, every 30 seconds –for debugging, I set this to 5 seconds –I do this strictly 'in' HTML / with JavaScript
So far Mine: meyer/twittersearch.html meyer/twittersearch.html Danielle's much prettier version:
wrapper (helper) Downloaded from thinkvitamin.com: class.twitter.php Tester script (form): twittersearch.html Working script: twittersearch.php –makes calls to the class.twitter.php functions –filters out the items starting with RT –prepares HTML for display –straight html does the refresh
xml versus json json is an object oriented format using -> The class.twitter.php code converts this. I just followed their example…. Sample of the XML is aripatila.xml aripatila.xml Note: a tweet is a status element
my twittersearch.php <?php header('Content-type: text/html; charset=utf-8'); require_once 'class.twitter.php'; // from ?> Finding tweets for a specified tag function init() { window.setTimeout(function() {location.reload(true);},5000); };
twittersearch.php, cont. <?php $t = new twitter; $tag = $_GET['key']; print "Searching for $tag..."; $s = new summize; $data = $s->search($tag); $data = $data->results;
Comment $data contains any number of items. the foreach($data as $d) loops over each one giving each the name $d Each $d has properties (using the json arrow notation), including $d->text Extra credit for someone who decodes the use of regular expression replace: preg_replace Notice going into and out of php
foreach($data as $d){ $st2= substr($d->text,0,2); $st1 = substr($d->text,0,1); if ((strcasecmp($st2,"RT") !=0) && { //case insensitive compare to RT ?> profile_image_url; ?>" alt="" /> <?php ', $d->text); ?> by <a href=" from_user; ?>"> from_user; ?> created_at; ?> from source); ?>
Classwork / homework Work on projects