PHP Functions untuk Cek Total Yahoo Backlink

Kemarin iseng-iseng bikin function ini untuk SEO analysis.

Requirement:
PHP 5.xx
SimpleXML enable
Yahoo API (bisa didapatkan di sini)

Cara pakai:
$total_yahoo_bl = ybl(”http://ngkong.net/”);
echo $total_yahoo_bl;

  1. $yahoo_api = "yahoo-api-kamu"; //ganti yahoo-api-kamu dengan yahoo api kamu
  2.  
  3. function get_data ($url){
  4.     $ch = curl_init();
  5.     curl_setopt($ch, CURLOPT_URL, $url);
  6.     curl_setopt($ch, CURLOPT_HEADER, false);
  7.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
  8.     $xml = curl_exec($ch);
  9.     curl_close($ch);
  10.     $data = simplexml_load_string($xml);
  11.     return $data;
  12. }
  13.  
  14. function ybl ($url){
  15.     global $yahoo_api;
  16.     $yurl = "http://api.search.yahoo.com/SiteExplorerService/V1/inlinkData?appid=$yahoo_api&query=".urlencode($url)."&results=1&entire_site=1";
  17.     $xml = get_data($yurl);
  18.     $att = $xml->attributes();
  19.     return $att['totalResultsAvailable'];
  20. }

PHP Functions untuk Query Google PageRank

Sumber: PHP PageRank query script

Berikut functions-nya, sudah saya coba dan bekerja dengan sempurna.

Cara pakai:
$pr = getpr(”http://example.com/dir/anu/ini.html”);
echo $pr;

  1. <?php
  2. // 3/20/2008 - Updated by Roger Collins (http://www.rogercollins.com/)
  3. // to remove graphing step
  4.  
  5. //PageRank Lookup v1.1 by HM2K (update: 31/01/07)
  6. //based on an alogoritham found here: http://pagerank.gamesaga.net/
  7.  
  8. //settings - host and user agent
  9. $googlehost='toolbarqueries.google.com';
  10. $googleua='Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.6) Gecko/20060728 Firefox/1.5';
  11.  
  12. //convert a string to a 32-bit integer
  13. function StrToNum($Str, $Check, $Magic) {
  14.     $Int32Unit = 4294967296// 2^32
  15.  
  16.     $length = strlen($Str);
  17.     for ($i = 0; $i < $length; $i++) {
  18.         $Check *= $Magic;     
  19.         //If the float is beyond the boundaries of integer (usually +/- 2.15e+9 = 2^31),
  20.         //  the result of converting to integer is undefined
  21.         //  refer to http://www.php.net/manual/en/language.types.integer.php
  22.         if ($Check >= $Int32Unit) {
  23.             $Check = ($Check - $Int32Unit * (int) ($Check / $Int32Unit));
  24.             //if the check less than -2^31
  25.             $Check = ($Check < -2147483648) ? ($Check + $Int32Unit) : $Check;
  26.         }
  27.         $Check += ord($Str{$i});
  28.     }
  29.     return $Check;
  30. }
  31.  
  32. //genearate a hash for a url
  33. function HashURL($String) {
  34.     $Check1 = StrToNum($String, 0x1505, 0x21);
  35.     $Check2 = StrToNum($String, 0, 0x1003F);
  36.  
  37.     $Check1 >>= 2;     
  38.     $Check1 = (($Check1 >> 4) & 0x3FFFFC0 ) | ($Check1 & 0x3F);
  39.     $Check1 = (($Check1 >> 4) & 0x3FFC00 ) | ($Check1 & 0x3FF);
  40.     $Check1 = (($Check1 >> 4) & 0x3C000 ) | ($Check1 & 0x3FFF);   
  41.    
  42.     $T1 = (((($Check1 & 0x3C0) << 4) | ($Check1 & 0x3C)) <<2 ) | ($Check2 & 0xF0F );
  43.     $T2 = (((($Check1 & 0xFFFFC000) << 4) | ($Check1 & 0x3C00)) << 0xA) | ($Check2 & 0xF0F0000 );
  44.    
  45.     return ($T1 | $T2);
  46. }
  47.  
  48. //genearate a checksum for the hash string
  49. function CheckHash($Hashnum) {
  50.     $CheckByte = 0;
  51.     $Flag = 0;
  52.  
  53.     $HashStr = sprintf('%u', $Hashnum) ;
  54.     $length = strlen($HashStr);
  55.    
  56.     for ($i = $length - 1$i >= 0$i --) {
  57.         $Re = $HashStr{$i};
  58.         if (1 === ($Flag % 2)) {             
  59.             $Re += $Re;     
  60.             $Re = (int)($Re / 10) + ($Re % 10);
  61.         }
  62.         $CheckByte += $Re;
  63.         $Flag ++;   
  64.     }
  65.  
  66.     $CheckByte %= 10;
  67.     if (0 !== $CheckByte) {
  68.         $CheckByte = 10 - $CheckByte;
  69.         if (1 === ($Flag % 2) ) {
  70.             if (1 === ($CheckByte % 2)) {
  71.                 $CheckByte += 9;
  72.             }
  73.             $CheckByte >>= 1;
  74.         }
  75.     }
  76.  
  77.     return '7'.$CheckByte.$HashStr;
  78. }
  79.  
  80. //return the pagerank checksum hash
  81. function getch($url) { return CheckHash(HashURL($url)); }
  82.  
  83. //return the pagerank figure
  84. function getpr($url) {
  85.     global $googlehost,$googleua;
  86.     $pr = 0;    // default return
  87.     $ch = getch($url);
  88.     $fp = fsockopen($googlehost, 80, $errno, $errstr, 30);
  89.     if ($fp) {
  90.        $out = "GET /search?client=navclient-auto&ch=$ch&features=Rank&q=info:$url HTTP/1.1\r\n";
  91.        //echo "<pre>$out</pre>\n"; //debug only
  92.        $out .= "User-Agent: $googleua\r\n";
  93.        $out .= "Host: $googlehost\r\n";
  94.        $out .= "Connection: Close\r\n\r\n";
  95.    
  96.        fwrite($fp, $out);
  97.        
  98.        //$pagerank = substr(fgets($fp, 128), 4); //debug only
  99.        //echo $pagerank; //debug only
  100.        while (!feof($fp)) {
  101.             $data = fgets($fp, 128);
  102.             //echo $data;
  103.             $pos = strpos($data, "Rank_");
  104.             if($pos === false){} else{
  105.                 $pr=substr($data, $pos + 9);
  106.                 $pr=trim($pr);
  107.                 $pr=str_replace("\n",'',$pr);
  108.                 return $pr;
  109.             }
  110.        }
  111.        //else { echo "$errstr ($errno)<br />\n"; } //debug only
  112.        fclose($fp);
  113.     }
  114.     return $pr;
  115. }
  116.  
  117. ?>

Tips Optimasi Kode PHP

Barusan cek memory VPS memakai perintah top dari putty, menemukan kalau salah satu script php saya makan memory sampai 80% ~X( , padahal script ini cuma 1 file (2 file dengen file config), yang isinya adalah loop untuk menyedot content dari suatu source (source legal loh :P ). Karena itu langsung buka kode dan googling cari tips untuk optimasi kode php saya. Selidik-selidik ternyata membengkaknya penggunaan memory karena di dalam loop yang saya buat terdapat gumpalan array yang sangat besar, yang seharusnya di -unset(); setiap kali loop. Berikut saya rangkum tips-tips yang dikutip dari berbagai sumber.
Read more

Membuat Grafik Cantik dengan Google Chart dan PHP

google chartGoogle Chart adalah fasilitas API atau web service dari Google Code yang berguna untuk menciptakan grafik gambar on the fly. Gambar yang dihasilkan sangat bagus, ada berbagai model grafik, dan yang paling penting adalah fasilitas ini disediakan gratis oleh Google. Saya membuat sebuah function PHP agar penerapannya lebih mudah untuk aplikasi dinamik.
Read more