Friendfeed Api Kullanarak Uygulama Geliştirmek .

Friendfeed tartışması çok fazla sansasyona sebep oldu sanırım artık buna bir son vermek gerekir düşüncesiyle basit bir şekilde friendfeed api yazmak nasıl oluyor açıklamaya çalışacağım . Sistem için çeşitli class ' lar olmasına karşın biz curl kullanarak sisteme ulaşacağız . Ve sonrasında istediğim türde işlemleri yapmaya çalışacağız . İlk önce öğrenmeniz gereken en önemli şey http://www.friendfeed.com/remotekey adresinden öğreneceğiniz uzak anahtarınız . Eğer adresi öğrendik şimdi api yazmak istiyoruz diyorsanız buyrun başlıyoruz .
ilk önce localhostunuza veyahut internet sitenizin içerisine bir klasör açmanız gerekiyor örneğin : ff klasörünü açtınız . Aynı klasörün içerisine bir mycurl.php dosyası oluşturuyoruz ve içerisine aşağıda verdiğim kodları yazıyorsunuz.

mycurl.php kodları :

PHP:
  1. <?
  2.  
  3. class MyCurl
  4. {
  5.   public $getHeaders = true; // Header için çıktı verilsin mi ?
  6.   public $getContent = true;
  7.   public $followRedirects = true; // Sayfa yönlendirmesi gerekiyorsa yönlendirme gerçekleşsin mi ?
  8.    
  9.   private $fCookieFile;
  10.   private $fSocket;
  11.    
  12.   function MyCurl()
  13.   {
  14.     $this->fCookieFile = tempnam("/tmp", "g_");
  15.     $this->auth_nickname = $auth_nickname;   
  16.     $this->auth_key = $auth_key// işte uzak anahtar ile ilgili kısım .
  17.   }
  18.  
  19.   function init()
  20.   {
  21.     return $this->fSocket = curl_init();
  22.   }
  23.  
  24.  
  25.  
  26.   function setopt($opt, $value)
  27.   {
  28.     return curl_setopt($this->fSocket, $opt, $value);
  29.   }
  30.    
  31.   function load_defaults()
  32.   {
  33.     $this->setopt(CURLOPT_RETURNTRANSFER, 1);
  34.     $this->setopt(CURLOPT_FOLLOWLOCATION, $this->followRedirects);
  35.     $this->setopt(CURLOPT_REFERER, "http://google.com");
  36.     $this->setopt(CURLOPT_VERBOSE, false)
  37.     $this->setopt(CURLOPT_SSL_VERIFYPEER, false);
  38.     $this->setopt(CURLOPT_SSL_VERIFYHOST, false);
  39.     $this->setopt(CURLOPT_HEADER, $this->getHeaders);
  40.     $this->setopt(CURLOPT_NOBODY, !$this->getContent);
  41.     $this->setopt(CURLOPT_COOKIEJAR, $this->fCookieFile);
  42.     $this->setopt(CURLOPT_COOKIEFILE, $this->fCookieFile);
  43.     $this->setopt(CURLOPT_USERAGENT, "MyCurl");
  44.     if ($this->auth_nickname && $this->auth_key) {
  45.     $this->setopt ( CURLOPT_USERPWD, $this->auth_nickname . ":" . $this->auth_key);
  46.     $this->setopt (CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  47.     }
  48.     $this->setopt(CURLOPT_POST, 1);
  49.     $this->setopt(CURLOPT_CUSTOMREQUEST,'POST');
  50.     $fp = fopen("c:\\__curl.log", "a");
  51.     if($fp)
  52.       $this->setopt(CURLOPT_STDERR, $fp);
  53.   }
  54.  
  55.  
  56.   function destroy()
  57.   {
  58.     return curl_close($this->fSocket);
  59.   }
  60.  
  61.   function head($url)
  62.   {
  63.     $this->init();
  64.     if($this->fSocket)
  65.     {
  66.       $this->getHeaders = true;
  67.       $this->getContent = false;
  68.       $this->load_defaults();
  69.       $this->setopt(CURLOPT_POST, 0);
  70.       $this->setopt(CURLOPT_CUSTOMREQUEST,'HEAD');
  71.       $this->setopt(CURLOPT_URL, $url);
  72.       $result = curl_exec($this->fSocket);
  73.       $this->destroy();
  74.       return $result;
  75.     }
  76.     return 0;
  77.   }
  78.  
  79.   function get($url)
  80.   {
  81.     $this->init();
  82.     if($this->fSocket)
  83.     {
  84.       $this->load_defaults();
  85.       $this->setopt(CURLOPT_POST, 0);
  86.       $this->setopt(CURLOPT_CUSTOMREQUEST,'GET');
  87.       $this->setopt(CURLOPT_URL, $url);
  88.       $result = curl_exec($this->fSocket);
  89.       $this->destroy();
  90.       return $result;
  91.     }
  92.     return 0;
  93.   }
  94.  
  95.   function post($url, $post_data, $arr_headers=array(), &$http_code)
  96.   {
  97.     $this->init();
  98.     if($this->fSocket)
  99.     {
  100.       $post_data = $this->compile_post_data($post_data);
  101.       $this->load_defaults();
  102.       if(!empty($post_data))
  103.         $this->setopt(CURLOPT_POSTFIELDS, $post_data);
  104.  
  105.       if(!empty($arr_headers))
  106.         $this->setopt(CURLOPT_HTTPHEADER, $arr_headers);
  107.        
  108.       $this->setopt(CURLOPT_URL, $url);
  109.  
  110.       $result = curl_exec($this->fSocket);
  111.       $http_code = curl_getinfo($this->fSocket, CURLINFO_HTTP_CODE);
  112.       $this->destroy();
  113.       return $result;
  114.     }
  115.     return 0;
  116.   }
  117.  
  118.   function compile_post_data($post_data)
  119.   {
  120.     $o="";
  121.     if(!empty($post_data))
  122.       foreach ($post_data as $k=>$v)
  123.         $o.= $k."=".urlencode($v)."&";
  124.     return substr($o,0,-1);
  125.   }
  126.    
  127.   function get_parsed($result, $bef, $aft="")
  128.   {
  129.     $line=1;
  130.     $len = strlen($bef);
  131.     $pos_bef = strpos($result, $bef);
  132.     if($pos_bef===false)
  133.       return "";
  134.     $pos_bef+=$len;
  135.      
  136.     if(empty($aft))
  137.     { //try to search up to the end of line
  138.       $pos_aft = strpos($result, "\n", $pos_bef);
  139.       if($pos_aft===false)
  140.         $pos_aft = strpos($result, "\r\n", $pos_bef);
  141.     }
  142.     else
  143.       $pos_aft = strpos($result, $aft, $pos_bef);
  144.      
  145.     if($pos_aft!==false)
  146.       $rez = substr($result, $pos_bef, $pos_aft-$pos_bef);
  147.     else
  148.       $rez = substr($result, $pos_bef);
  149.      
  150.     return $rez;
  151.   }
  152.  
  153. }
  154. ?>

Evet işte şimdi mycurl.php dosyasını yazdık şimdide index.php dosyası oluşturuyoruz . Bu sayfadan kişiler hazırladığınız api'ye giriş yapacak ve kullanmaya başlayacak .

index.php kodları :

PHP:
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3.   <head>
  4.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  5.     <title>FriendFeed Api Giriş - byumut.com</title>
  6.     <STYLE TYPE="text/css">
  7.         a{
  8.         text-decoration:none;
  9.         font-size:12px;
  10.         font-family:"Trebuchet Ms";
  11.         }
  12.     </STYLE>
  13.   </head>
  14.   <body>
  15. <a href="<?=$_SERVER['HTTP_REFFER']?>" style="color: #1030cc" target="_blank">Geri</a><br>
  16.  <?
  17.  
  18.  
  19. include "mycurl.php";
  20.  
  21. $mc = new MyCurl()
  22. $mc->getHeaders = false;
  23. $mc->getContent = true;
  24. $mc->auth_nickname = $_REQUEST['friendfeed_nickname'];
  25. $mc->auth_key = $_REQUEST['friendfeed_remote_key'];
  26.  
  27. $url = "http://friendfeed-api.com/v2/feedinfo/".$mc->auth_nickname."?format=json";
  28. $response = $mc->get($url);
  29. $ciktiArray = json_decode($response);
  30. $servisler = $ciktiArray->services;
  31.  
  32. $abone_Olduklarim = array();
  33. $aboneOlduklarim = $ciktiArray->subscriptions;
  34. $abonelikSay = count($aboneOlduklarim);
  35. for($i=0;$i<$abonelikSay;$i++){
  36.  
  37. $abone_Olduklarim[$aboneOlduklarim[$i]->name] = array
  38. (
  39.     "id"=>$aboneOlduklarim[$i]->id,
  40.     "type"=>$aboneOlduklarim[$i]->type,
  41. );
  42.  
  43. }
  44. $abone_Olanlar = array();
  45. $aboneOlanlar = $ciktiArray->subscribers;
  46. $abonelikSay = count($aboneOlanlar);
  47. for($i=0;$i<$abonelikSay;$i++){
  48.  
  49.  
  50. //bak şöylede olur...
  51.  
  52. $abone_Olanlar[ $aboneOlanlar[$i]->name   ] = array
  53. (
  54.     "id"=>$aboneOlanlar[$i]->id,
  55.     "type"=>$aboneOlanlar[$i]->type,
  56. );
  57.  
  58.  
  59.  
  60. }
  61.  
  62.  
  63. ksort($abone_Olduklarim);
  64. ksort($abone_Olanlar);
  65.  
  66. $usr_arr1 = array_keys($abone_Olanlar); //usernameleri alır, yani keyleri.. //bu array sadece username tutuyor..
  67. $usr_arr2 = array_keys($abone_Olduklarim); //usernameleri alır, yani keyleri..
  68.  
  69. $result = array_diff($usr_arr1,$usr_arr2);
  70. $result2 = array_diff($usr_arr2,$usr_arr1);
  71.  
  72. echo "<b>Benim abone olduklarım ama bana abone olmayanlar : </b><br> ";
  73. foreach($result2 as $diff_usr)
  74. {
  75.  echo "$diff_usr>>>>  (".print_r($abone_Olduklarim[$diff_usr],1).")<br>";
  76.    
  77.  
  78. }
  79.  
  80.  
  81. echo "<hr>";
  82. echo "<b>Bana Abone Olan ama benim olmadıklarım : </b><br> ";
  83. foreach($result as $diff_usr)
  84. {
  85.  echo "$diff_usr>>>>  (".print_r($abone_Olanlar[$diff_usr],1).")<br>";
  86.    
  87.  
  88. }
  89.  
  90.  
  91. echo "<hr>";
  92.  
  93. echo "<b>Bana Abone Olanlar : </b><br> ";
  94. foreach($abone_Olanlar as $username=>$val)
  95. {
  96.     list($id,$type)=array_values($val);
  97.     echo "($id,$username,$type)<br>";
  98.  
  99. }
  100.  
  101. echo "<hr>";
  102. echo "<b>Benim Abone Olduklarım : </b><br> ";
  103.  
  104. foreach($abone_Olduklarim as $key=>$val)
  105. {
  106.     list($id,$username,$type)=array_values($val);
  107.     echo "($id,$username,$type)<br>";
  108.  
  109. }
  110.  
  111. if(!$_POST){
  112.  
  113. ?>
  114. <form action="" method="post">
  115.   <table style="border-collapse: collapse; border-spacing: 0; padding: 0; margin: 0; font-family: Arial, sans-serif; border: 4px solid #6797d3; color: #222222">
  116.     <tr>
  117.       <td style="background-color: #ecf2fa; padding: 3px; padding-left: 5px; padding-top: 5px; border: 0; border-bottom: 1px solid #6797d3"><a href="http://friendfeed.com/" target="_blank"><img src="http://friendfeed.com/static/images/logo-api.png" width="160" height="34" alt="FriendFeed" style="padding:0; border:0; margin:0"/></a></td>
  118.       <td style="background-color: #ecf2fa; padding: 3px; padding-right: 20px; border: 0; border-bottom: 1px solid #6797d3; text-align: right; vertical-align: middle; font-size: 16pt; font-weight: bold; color: gray">Api Girişi</td>
  119.     </tr>
  120.     <tr>
  121.       <td style="background-color: white; padding: 15px; border: 0" colspan="2">
  122.         <table style="border-collapse: collapse; border-spacing: 0; border: 0; padding: 0; margin: 0">
  123.           <tr>
  124.             <td style="border: 0; padding: 5px; font-size: 10pt">FriendFeed Kullanıcı Adınız:</td>
  125.         <td style="border: 0; padding: 5px; font-size: 10pt"><input type="text" name="friendfeed_nickname" style="width: 10em"/></td>
  126.       </tr>
  127.       <tr>
  128.         <td style="border: 0; padding: 5px; font-size: 10pt">Uzak Anahtarınız  [ <a href="http://friendfeed.com/remotekey" style="color: #1030cc" target="_blank">bilmiyorsanız</a> ]:</td>
  129.         <td style="border: 0; padding: 5px; font-size: 10pt"><input type="password" name="friendfeed_remote_key" style="width: 10em"/></td>
  130.       </tr>
  131.       <tr>
  132.         <td style="border: 0; padding: 0; padding-right: 5px; padding-top: 8px; text-align: right" colspan="2"><input type="submit" value="Giriş Yap" style="font-weight: bold; color: #222222; font-family: Arial, sans-serif; font-size: 10pt"/></td>
  133.       </tr>
  134.     </table>
  135.       </td>
  136.     </tr>
  137.   </table>
  138. </form>
  139.  <?
  140. }
  141. ?>
  142.   </body>
  143. </html>

Gibi gerisini geliştirmek size kalmış arkadaşlar uykum geldi ondan daha fazla örnek ekleyemiyorum :( kolay gelsin .

38 views

Leave a Reply

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word