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 <rochakchauhan@gmail.com>
  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.    echo $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.    //        print_r($records);
  57.            $count=count($records);
  58.            echo "<ul>";
  59.            for($k=0;$k<$count;$k++){
  60.                $j=$k+1;
  61.                $link=$records[$k][2];
  62.                $link=strip_tags($link);
  63.                $link=str_replace("http://www.","",$link);
  64.                $link=str_replace("http://","",$link);
  65.                echo $link=str_replace("www.","",$link);
  66.            echo    $pos=strpos($link, "/");
  67.              $link=trim(substr($link,0,$pos));
  68.  
  69.                if($this->website==$link){
  70.                    $domain=$this->website;
  71.                    echo "<li><h1>Result was found in Page: $c and Record: $j</h1></li>";
  72.                    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>";
  73.                    exit;
  74.                }
  75.                else{
  76.                    echo "<li>Result not found on Page: $c and Record: $j</li>";
  77.                }
  78.            }
  79.            echo "</ul></ul>";
  80.            $c++;
  81.            $this->updateUrl($this->keyword, $i);
  82.        }
  83.        echo "Crawled through all 10 pages.";
  84.  
  85.        if($this->page==false){
  86.            $domain=$this->website;
  87.            $keyword=$this->keyword;
  88.            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>";
  89.        }
  90.        else {
  91.            $page=$this->page;
  92.            $records=$this->records;
  93.            $domain=$this->website;
  94.            $keyword=$this->keyword;
  95.            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>";
  96.        }
  97.    }
  98.  
  99.    /**
  100.      * Function to get records as an array.
  101.      *
  102.      * @access private
  103.      * @param string $url
  104.      *
  105.      * @return array
  106.      */
  107.    private function getRecordsAsArray($url){
  108.        $matches=array();
  109.        $pattern='/<div class="s"(.*)<cite>(.*)</cite>/Uis';
  110.        $html=$this->getCodeViaFopen($url);
  111.        preg_match_all($pattern, $html, $matches, PREG_SET_ORDER);
  112.    //    print_r($matches);
  113.        return $matches;
  114.    }
  115.  
  116.    /**
  117.      * Function to update the google search query.
  118.      *
  119.      * @access private
  120.      * @param string $keyword
  121.      * @param string $start
  122.      *
  123.      * @return string
  124.      */
  125.    private function updateUrl($keyword, $start){
  126.        $this->start=$this->start+$start;
  127.        $keyword=trim($keyword);
  128.        $keyword=urlencode($keyword);
  129.        return "http://www.google.pl/search?q=$keyword&hl=pl&lr=&start=".$this->start."&sa=N";
  130.    }
  131.  
  132.    /**
  133.      * Function to get HTML code from remote url
  134.      *
  135.      * @access private
  136.      * @param string $url
  137.      *
  138.      * @return string
  139.      */
  140.    private function getCodeViaFopen($url){
  141.        $returnStr="";
  142.        $fp=fopen($url, "r") or die("ERROR: Invalid search URL");
  143.        while (!feof($fp)) {
  144.            $returnStr.=fgetc($fp);
  145.        }
  146.        fclose($fp);
  147.        return $returnStr;
  148.    }
  149. }
  150. ?>

uzycie:
  1. <?php
  2. $keyword="programy";
  3. $website="pobieraj24.pl";
  4.  
  5. // optional for accepting parameters from Query string
  6. if(isset($_GET['keyword'])) { $keyword=$_GET['keyword']; }
  7. if(isset($_GET['website'])) { $website=$_GET['website']; }
  8.  
  9. require_once("PhpKeywordAnalyser.inc.php");
  10. $phpKeywordAnalyser = new PhpKeywordAnalyser($keyword, $website);
  11. $phpKeywordAnalyser->initSpider();
  12. ?>

ten skrypt działa, jezeli jest dana strona na pierwszej stronie a jak zrobić, żeby jak np cos jest dalej w wynikach czyli na drugiej albo 3 stronie to żeby wyswietlało, że się tam znajduje bo tak to ciągle sprawdza tylko pierwszą strone w kółko.jak zrobić, żeby dodawało +10 do $this->start i tak za kazdym przejsciem pętli?