Posted By

kangcom on 01/26/11


Tagged

data api twitter


Versions (?)

Get Twitter Data using twitter API


 / Published in: PHP
 

  1. <FORM NAME=frm ACTION=search.php METHOD=POST>
  2. <INPUT TYPE=TEXT SIZE=20 NAME=q>
  3. <INPUT TYPE=SUBMIT VALUE=Search>
  4.  
  5. </FORM>
  6.  
  7. <?
  8.  
  9.  
  10. if($q != null) {
  11.  
  12. $url = "http://search.twitter.com/search.json?q=".urlencode(iconv("euc-kr","utf-8",$q));
  13.  
  14. $cont = get($url);
  15.  
  16. if($cont == null) {
  17. echo "no result";
  18. }
  19.  
  20.  
  21. // echo $cont;
  22.  
  23.  
  24. $idx = 0;
  25.  
  26. $PROFILE_IMAGE_URL = "\"profile_image_url\":\"";
  27. $USER = "\"from_user\":\"";
  28. $ID = "\"id_str\":\"";
  29. $TEXT = "\"text\":\"";
  30.  
  31.  
  32.  
  33. echo "<TABLE BORDER=0 WIDTH=500 STYLE=font-size:10pt;>";
  34.  
  35. while(($idx = strpos($cont,$PROFILE_IMAGE_URL,$idx))!=null) {
  36.  
  37. $idx2 = $idx+strlen($PROFILE_IMAGE_URL);
  38. $idx3 = strpos($cont,"\"",$idx2);
  39. $image_url = substr($cont, $idx2, $idx3-$idx2);
  40.  
  41. $idx4 = strpos($cont, $USER, $idx3);
  42. $idx5 = strpos($cont, "\"", $idx4+strlen($USER));
  43. $user = substr($cont, $idx4+strlen($USER), $idx5-($idx4+strlen($USER)) );
  44.  
  45. $idx6 = strpos($cont, $ID, $idx5);
  46. $idx7 = strpos($cont, "\"", $idx6+strlen($ID));
  47. $id = substr($cont, $idx6+strlen($ID), $idx7-($idx6+strlen($ID)) );
  48.  
  49. $idx8 = strpos($cont, $TEXT, $idx7);
  50. $idx9 = strpos($cont, "\"", $idx8+strlen($TEXT));
  51. $text = substr($cont, $idx8+strlen($TEXT), $idx9-($idx8+strlen($TEXT)) );
  52.  
  53.  
  54. // echo utf8_urldecode($text)."<br>";
  55.  
  56. echo "<TR><TD ALIGH=LEFT><IMG SRC=".$image_url."></TD>";
  57. echo "<TD> writer : ".$user."<BR>";
  58. echo utf8_urldecode($text)."</TD></TR>";
  59. echo "<TR><TD HEIGHT=1 BGCOLOR=SILVER COLSPAN=2></TD></TR>";
  60.  
  61. ++$idx;
  62. }
  63. }
  64.  
  65.  
  66.  
  67. // convert unicode to korean language
  68. function utf8_urldecode($str) {
  69.  
  70. $str = str_replace("\\","%",$str);
  71.  
  72. $str = preg_replace("/%u([0-9a-f]{3,4})/i","&amp;#x\\1;",urldecode($str));
  73.  
  74. return html_entity_decode($str,null,'UTF-8');
  75. }
  76.  
  77.  
  78. // get web page
  79. function get($url) {
  80.  
  81. $fp = fopen($url, "r");
  82.  
  83. if($url){
  84. while(!feof($fp)) {
  85. $tmp = fgets($fp, 1024);
  86. $temp .= $tmp;
  87. }
  88. fclose($fp);
  89. }
  90.  
  91. return $temp;
  92. }
  93.  
  94.  
  95. ?>

Report this snippet  

You need to login to post a comment.