Twitter

2,903 views

Published on

Twitter API Integration Using Open Source Technology

Published in: Technology

Twitter

  1. 1. Twitter API Integration<br />PHP, MySQL, Apache & Me<br />
  2. 2.
  3. 3.
  4. 4.
  5. 5. API API & More API<br />
  6. 6. Search API Methods <br />search<br />trends<br />trends/current<br />trends/daily<br />trends/weekly <br /> REST API Methods <br /> Timeline Methods<br />statuses/public_timeline <br />statuses/home_timeline<br />statuses/friends_timeline<br />statuses/user_timeline <br />statuses/mentions<br />statuses/retweeted_by_me<br />statuses/retweeted_to_me<br />statuses/retweets_of_me<br /> Status Methods<br />statuses/show<br />statuses/update<br />statuses/destroy  <br />statuses/retweet<br />statuses/retweets<br />statuses/id/retweeted_by<br /> statuses/id/retweeted_by/ids<br /> User Methods<br />users/show <br />users/lookup<br />users/search<br />users/suggestions<br />users/suggestions/category<br />statuses/friends<br />statuses/followers<br /> List Methods<br />         POST lists      (create)<br />         POST lists id  (update)<br />         GET lists        (index)<br />         GET list id      (show)<br />         DELETE list id (destroy)<br />         GET list statuses<br />         GET list memberships<br />         GET list subscriptions<br /> List Members Methods<br />         GET list members<br />         POST list members<br />         DELETE list members<br />         GET list members id<br /> List Subscribers Methods<br />         GET list subscribers<br />         POST list subscribers<br />         DELETE list subscribers<br />         GET list subscribers id<br /> Direct Message Methods <br />direct_messages<br />direct_messages/sent<br />direct_messages/new<br />direct_messages/destroy <br />API API & More API<br />
  7. 7. Spam Reporting Methods<br />  report_spam<br /> Saved Searches Methods<br />saved_searches<br />saved_searches/show<br />saved_searches/create<br />saved_searches/destroy<br />OAuth Methods<br />oauth/request_token<br />oauth/authorize<br />oauth/authenticate<br />oauth/access_token<br />  Geo methods<br />geo/nearby_places<br />  geo/reverse_geocode<br />  geo/id<br /> <br />Friendship Methods<br />friendships/create<br />friendships/destroy <br />friendships/exists<br />friendships/show<br />friendships/incoming<br />friendships/outgoing<br />  <br /> Account Methods<br />account/verify_credentials <br />account/rate_limit_status<br />account/end_session<br />account/update_delivery_device <br />account/update_profile_colors <br />account/update_profile_image <br />account/update_profile_background_image<br />account/update_profile <br /> Favorite Methods<br />favorites<br />favorites/create  <br />favorites/destroy <br /> Notification Methods<br />notifications/follow <br />notifications/leave <br /> Block Methods<br />blocks/create  <br />blocks/destroy<br />blocks/exists<br />blocks/blocking<br />blocks/blocking/ids<br />Social Graph Methods<br />friends/ids   <br />followers/ids <br /> <br />API API & More API<br />
  8. 8. Latest Trends on Twitter as We Speak<br />function getlatest(){ $url = 'http://search.twitter.com/trends/current.json?exclude=hashtags'; // create a new cURL resource $ch = curl_init();// set URL and other appropriate optionscurl_setopt($ch, CURLOPT_URL,$url);curl_setopt($ch, CURLOPT_HEADER, 0);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // grab URL and pass it to the browser$json=curl_exec($ch);curl_close($ch);$obj_t=json_decode($json,true);echo “Latest Trends Today on Twitter.com @" .date('l jS of F Y h:i:s A‘)" ;}<br />
  9. 9. Display Function (Array)<br />function display($i_array)<br />{<br />foreach ($i_array as $i => $val)<br /> {<br /> // check if element is array and trends only <br /> if (is_array($val)){<br /> display($val);}<br /> else<br /> {<br /> if($i=='name') {<br /> echo "<a href='http://twitter.com/search?q=$val'>$val</a><br>";<br /> }<br /> }<br /> } // end for<br />}<br />
  10. 10. Display Friends<br />function display_following($screen_name)<br />{<br />if (!($screen_name==""))<br />{<br />$url = 'http://api.twitter.com/1/statuses/friends.xml?screen_name='.$screen_name ;<br />$xml = simplexml_load_file($url) or exit ("Unable to load XML!"); <br />echo "<div id='result'>";<br />echo "<b>You Follow " .count($xml->user) ." Users</b><br>";<br />echo "<table>";<br />echo "<tr>";<br />$count = 0;<br />foreach ($xml as $user => $u) <br />{ <br />if ($count==3)<br />{echo "</tr>";<br />$count=0;}<br /> echo "<td><a href='http://twitter.com/$u->screen_name'>"<br /> ."<img height='50' width='50' title=".$u->screen_name ." src='" .$u->profile_image_url ."'/>" ."</a></td>"; <br /> $count = $count + 1;<br />} <br />echo "</tr></table>";<br />echo "</div>";<br />}<br />else<br />{}<br />}//end display_following()<br />
  11. 11. DisplayFollowers<br />function display_followers($screen_name)<br />{<br /> if (!($screen_name==""))<br /> {<br /> $curl_conn = curl_init();<br /> $url = 'http://twitter.com/statuses/followers/' .$screen_name .'.xml';<br /> $xml = simplexml_load_file($url) or exit ("Unable to load XML!"); <br /> echo "<div id='result'>";<br /> echo "<b>Your " .count($xml->user) ." Followers</b><br>";<br /> echo "<table>";<br /> echo "<tr>";<br /> $count = 0;<br />foreach ($xml as $user => $u) <br /> { <br /> if ($count==3)<br /> {echo "</tr>“; $count=0;}<br /> echo "<td><a href='http://twitter.com/$u->screen_name'>"<br /> ."<img height='50' width='50' title=".$u->screen_name ." src='".$u->profile_image_url ."'/>" <br /> ."</a></td>"; <br /> $count = $count + 1;<br /> } //end for<br /> echo "</tr></table>";<br /> echo "</div>";<br /> } <br /> else<br /> {<br /> //display_form();<br /> }<br />}//end display_followers()<br />
  12. 12. Add Tweet<br />function post($username,$password,$tweet)<br />{<br />$status = urlencode(stripslashes(urldecode($_POST['tweet'])));<br />if ($status) {<br />$tweetUrl = 'http://www.twitter.com/statuses/update.xml';<br />$curl = curl_init();<br />curl_setopt($curl, CURLOPT_URL, "$tweetUrl");<br />curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);<br />curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);<br />curl_setopt($curl, CURLOPT_POST, 1);<br />curl_setopt($curl, CURLOPT_POSTFIELDS, "status=$status");<br />curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");<br />$result = curl_exec($curl);<br />$resultArray = curl_getinfo($curl);<br /> if ($resultArray['http_code'] == 200)<br /> {<br /> //echo 'Tweet Posted';<br /> display();<br /> }<br /> else<br /> {<br /> echo 'Could not post Tweet to Twitter right now. Try again later.';<br />display_form();<br /> }<br />curl_close($curl);<br /> } // end status<br />}//end post<br />
  13. 13. Linkify<br />function linkify($text)<br /> {<br /> $text = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_+.~#?&//=]+)‘, '<a href=""></a>', $text);<br /> text = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_+.~#?&//=]+)‘, '<a href="http://"></a>', $text);<br /> $text = eregi_replace('([_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3})‘, '<a href="mailto:"></a>', $text);<br /> return $text;<br /> }<br />
  14. 14. Like Shorter URLS<br />function make_bitly_url($url,$login,$appkey,$format = 'xml',$version = '2.0.1')<br />{<br /> //create the URL<br /> $bitly = 'http://api.bit.ly/shorten?version='.$version.'&longUrl='.urlencode($url).'&login='.$login.'&apiKey='.$appkey.'&format='.$format;<br /> //get the url<br /> //could also use cURL here<br /> $response = file_get_contents($bitly);<br /> //parse depending on desired format<br /> if(strtolower($format) == 'json')<br /> {<br /> $json = @json_decode($response,true);<br /> return $json['results'][$url]['shortUrl'];<br /> }<br /> else //xml<br /> {<br /> $xml = simplexml_load_string($response);<br /> return 'http://bit.ly/'.$xml->results->nodeKeyVal->hash;<br /> }<br />}<br />/* usage */<br />$short = make_bitly_url($_GET['url'],'davidwalshblog','R_9acc320c5c423e4f5192e00ff24980','json');<br />//echo 'The short URL for "http://www.devitsolutions.com" is: <a style="color:Green" href='.$short .'>Link</a>'; <br />echo $short;<br />
  15. 15. Last status<br />function get_status($twitter_id, $hyperlinks = true) {<br /> $c = curl_init();<br />curl_setopt($c, CURLOPT_URL, "http://twitter.com/statuses/user_timeline/$twitter_id.xml?count=1");<br />curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);<br /> $src = curl_exec($c);<br />curl_close($c);<br />preg_match('/<text>(.*)</text>/', $src, $m);<br /> $status = htmlentities($m[1]);<br /> if( $hyperlinks ) <br />$status = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]", "<a href=""></a>", $status);<br /> return($status);<br />}<br />
  16. 16. LIVE DEMO<br />WWW.iwilltypeurl.com<br />
  17. 17. THANKS TWITTER , IBM<br />AND HOW CAN I FORGET U ALL<br />
  18. 18. CATCH ME ON TWITTER @utpalbetai<br />

×