Ver Mensaje Individual
  #3 (permalink)  
Antiguo 04/10/2011, 17:09
Avatar de andresdzphp
andresdzphp
Colaborador
 
Fecha de Ingreso: julio-2011
Ubicación: $this->Colombia;
Mensajes: 2.749
Antigüedad: 12 años, 9 meses
Puntos: 793
Respuesta: Funcion para ver el ranking de una keyword en google

Por hay buscando en google me encontré varias clases, dejo una:

Código PHP:
Ver original
  1. <?php  
  2. /**
  3.  * This class crawls google and find out on which page a website is lisited, based on the keyword searched.
  4.  *
  5.  * @author Rochak Chauhan <[email protected]>
  6.  * @see This class does NOT calculated the Google Page Rank
  7.  */
  8. class PhpKeywordAnalyser {
  9.     private $website="";
  10.     private $keyword="";
  11.     private $url="";
  12.     private $start=0;
  13.     private $page=false;
  14.     private $records=false;
  15.  
  16.     /**
  17.      * Function to pre process and store the values of Keyword and Website
  18.      *
  19.      * @param string $keyword
  20.      * @param string $website
  21.      * @return resource
  22.      */
  23.     public function __construct($keyword, $website){
  24.         if(trim($keyword)==""){
  25.             trigger_error("Keyword cannot the left blank.",E_USER_ERROR); die();
  26.         }
  27.         if(trim($website)==""){
  28.             trigger_error("Website cannot the left blank.",E_USER_ERROR); die();
  29.         }
  30.         $website=strtolower(trim($website));
  31.         $website=str_replace("http://www.","",$website);
  32.         $website=str_replace("http://","",$website);
  33.         $website=str_replace("www.","",$website);
  34.         $website=str_replace("/","",$website);
  35.      
  36.         $this->website=$website;
  37.         $this->keyword=trim($keyword);
  38.         $this->enableVerbose=$enableVerbose;        
  39.         $this->url=$this->updateUrl($keyword, $this->start);
  40.     }
  41.  
  42.     /**
  43.      * This function starts the crawling process and it verbose the content as it goes to next page.
  44.      *
  45.      * @return string [buffer]
  46.      */
  47.     public function initSpider(){
  48.         echo "<p>Searching: <b>".$this->keyword."</b> and Looking for: <b>".$this->website."</b></p>";
  49.         echo str_repeat(" ", 256);
  50.         $i=10;
  51.         $c=1;
  52.         while($c<=10) {            
  53.             echo "<ul><li><b>Searching in Page: $c</b></li>";                
  54.             flush();ob_flush();
  55.             $records= $this->getRecordsAsArray($this->url);  
  56.             $count=count($records);
  57.             echo "<ul>";
  58.             for($k=0;$k<$count;$k++){
  59.                 $j=$k+1;
  60.                 $link=$records[$k][2];
  61.                 $link=strip_tags($link);
  62.                 $link=str_replace("http://www.","",$link);
  63.                 $link=str_replace("http://","",$link);
  64.                 $link=str_replace("www.","",$link);
  65.                 $pos=strpos($link, "/");
  66.                 $link=trim(substr($link,0,$pos));
  67.                  
  68.                 if($this->website==$link){
  69.                     $domain=$this->website;
  70.                     echo "<li><h1>Result was found in Page: $c and Record: $j</h1></li>";
  71.                     echo "<div>Congrats, We searched google's top 10 pages for <b>\"".$this->keyword."</b>\", we found your domain <b>\"$domain\"</b> listed on page: $c at $j place </div>";echo "</ul></ul>";
  72.                     exit;
  73.                 }
  74.                 else{
  75.                     echo "<li>Result not found on Page: $c and Record: $j</li>";
  76.                 }                
  77.             }
  78.             echo "</ul></ul>";
  79.             $c++;
  80.             $this->updateUrl($this->keyword, $i);
  81.         }
  82.         echo "Crawled through all 10 pages.";            
  83.          
  84.         if($this->page==false){
  85.             $domain=$this->website;
  86.             $keyword=$this->keyword;
  87.             echo "<div>Sorry, We searched google's top 10 pages for <b>\"$keyword\"</b>, but was unable to find your domain <b>\"$domain\"</b> listed anywhere. </div>";
  88.         }
  89.         else {
  90.             $page=$this->page;
  91.             $records=$this->records;
  92.             $domain=$this->website;
  93.             $keyword=$this->keyword;
  94.             echo "<div>Congrats, We searched google's top 10 pages for <b>\"$keyword\"</b>, we found your domain <b>\"$domain\"</b> listed on page: $page at $record place </div>";
  95.         }
  96.     }
  97.      
  98.     /**
  99.      * Function to get records as an array.
  100.      *
  101.      * @access private
  102.      * @param string $url
  103.      *
  104.      * @return array
  105.      */
  106.     private function getRecordsAsArray($url){
  107.         $matches=array();
  108.         $pattern='/<div class="s"(.*)\<cite\>(.*)\<\/cite\>/Uis';
  109.         $html=$this->getCodeViaFopen($url);
  110.         preg_match_all($pattern, $html, $matches, PREG_SET_ORDER);
  111.         return $matches;
  112.     }
  113.      
  114.     /**
  115.      * Function to update the google search query.
  116.      *
  117.      * @access private
  118.      * @param string $keyword
  119.      * @param string $start
  120.      *
  121.      * @return string
  122.      */
  123.     private function updateUrl($keyword, $start){
  124.         $this->start=$this->start+$start;    
  125.         $keyword=trim($keyword);
  126.         $keyword=urlencode($keyword);
  127.         return "http://www.google.com/search?start=".$this->start."&q=$keyword";
  128.     }
  129.  
  130.     /**
  131.      * Function to get HTML code from remote url
  132.      *
  133.      * @access private
  134.      * @param string $url
  135.      *
  136.      * @return string
  137.      */
  138.     private function getCodeViaFopen($url){
  139.         $returnStr="";
  140.         $fp=fopen($url, "r") or die("ERROR: Invalid search URL");
  141.         while (!feof($fp)) {
  142.             $returnStr.=fgetc($fp);
  143.         }
  144.         fclose($fp);
  145.         return $returnStr;
  146.     }
  147. }
  148.  
  149. $keyword="Google";
  150. $website="www.google.com";
  151.  
  152. // optional for accepting parameters from Query string
  153. if(isset($_GET['keyword'])) { $keyword=$_GET['keyword']; }
  154. if(isset($_GET['website'])) { $website=$_GET['website']; }
  155.  
  156. $phpKeywordAnalyser = new PhpKeywordAnalyser($keyword, $website);
  157. $phpKeywordAnalyser->initSpider();

En http://www.phpclasses.org/ hay varias parecidas para que saques ideas.

Aclaro que la clase es gracias a Rochak Chauhan
License: Free For Educational Use

Saludos:
__________________
Si sabemos como leer e interpretar el manual será mucho más fácil aprender PHP. En lugar de confiar en ejemplos o copiar y pegar - PHP