2010-10-25 2 views
0

Im пытается сделать класс, чтобы помочь с SEO, а также сравнить Google API с фактическими результатамипростой HTML DOM - не redeclair str_get_html()

класс:

<?php 

class true_seo { 

    public $string, $amount; 
    private $arr; 

    public function __construct(){} 

    public function set_g_key($key) { 
     $this->g_key = $key;  
    } 

    public function set_phrase($string){ 
     if(is_string ($string)) { 
      $string = array($string); 
     } 
     if(is_array ($string)) { 
      $this->phrases = $string; 
     }else{ 
      Throw new exception("incorect input for phrase, string or array"); 
     } 
    } 

    public function get_sites_use_spider($amount) { 
     require "simple_html_dom.php"; 
     $main_result = array(); 
     foreach($this->phrases as $phrase) { 
      $APIparams = array("key" => $this->g_key, "q" => $phrase, "start" => 0, "maxResults" => $amount, "filter" => true, "restrict" => "", "safeSearch" => false, "lr" => "lang_en", "ie" => "", "oe" => ""); 
      $data = true_seo::google_search_api($APIparams, 'http://www.google.co.uk/search', false); 
      new simple_html_dom(); 
      $html = str_get_html($data); 
      $result = array(); 
      foreach($html->find('li.g h3 a') as $g) { 
       $data = $g->parent()->nextSibling(); 
       $other = $data->find('span a'); 
       $x = 0; 
       foreach($other as $d) { 
        ($x == 0 ? $cache = $d->href : $simular = $d->href); 
        $x++; 
       } 
       $excess_span = $data->find('span',0)->outertext; 
       if(isset($data->find('div',0)->tag)) { 
        $excess_div = $data->find('div',0)->outertext; 
        $title = str_replace(array($excess_span, $excess_div, '<em>', '</em>', '<br>', '<b>', '</b>'), array('','','','','','',''), $data->outertext); 
       }else{ 
        $title = str_replace(array($excess_span, '<em>', '</em>', '<br>', '<b>', '</b>'), array('','','','','',''), $data->outertext);      
       } 
       $result[] = array('link' => $g->href, 'title' => strip_tags($title), 'cache' => $cache, 'simular' => 'http://www.google.co.uk' . $simular); 
      } 
      $main_result[$phrase] = $result; 
      $html->clear(); 
     } 
     $this->non_api_data = $main_result; 
    } 

    public function get_sites_use_api($amount) { 
     $arr = array(); 
     foreach($this->phrases as $phrase) { 
      if($amount > 4) { 
       $times = $amount/4; 
      }else{ 
       $times = 1; 
      } 
      $arg = array(); 
      for($x = 0; $x < $times; $x++) { 
       $APIparams = array("key" => $this->g_key, "q" => $phrase, "start" => ($x * 4), "maxResults" => 4, "filter" => true, "restrict" => "", "safeSearch" => false, "lr" => "lang_en", "ie" => "", "oe" => ""); 
       if($data = true_seo::google_search_api($APIparams, 'http://ajax.googleapis.com/ajax/services/search/web')) { 
        $arg = array_merge($arg, $data->responseData->results); 
       }else{ 
        Throw new exception("Request error: no results returned from Google."); 
       } 
      } 
      $arg = array_reverse($arg); 
      $remove = $amount % 4; 
      if($amount < 4) { 
       $remove = 4 - $amount; 
      } 
      for($x=0; $x < $remove; $x++) { 
       unset($arg[$x]); 
      } 
      $arg = array_reverse($arg); 
      foreach($arg as $g) { 
       $result = array('link' => $g->url, 'title' => strip_tags($g->content), 'cache' => $g->cacheUrl, 'simular' => 'na'); 
       $arr[$phrase][] = $result; 
      } 
     } 
     $this->api_data = $arr; 
    } 

    public function google_search_api($args, $url, $api = true){ 
     if (!array_key_exists('v', $args)) { 
      $args['v'] = '1.0'; 
     } 
     $url .= '?'.http_build_query($args, '', '&'); 
     if($result = @file_get_contents($url)) { 
      if($api == true) { 
       return json_decode($result);  
      }else{ 
       return $result; 
      } 
     }else{ 
      Throw new exception("No data returned from url: $url"); 
     } 
    } 

    public function set_get_actual($string) { 
     $this->actual->name = $string; 
     $this->actual->data = file_get_contents($string); 
    } 

    public function get_actual_description(){ 
     require_once "simple_html_dom.php"; 
     new simple_html_dom(); 
     $html = str_get_html($this->actual->data); 
     return $html->find('head meta[name=description]',0)->content; 
     $html->clear(); 
    } 
} 

?> 

называют:

<?php 

try{ 
    require "./classes/class_true_seo.php"; 
    $seo = new true_seo(); 
    $seo->set_g_key('ABQIAAAAsWzmZ4RXdIk0a-LqpqKCBRSl_WmKnmsXGmN0kkjN2wkrfEOY-hT2sL-_x5v4NtT3DgElKNsR7FDJDQ'); 
    $seo->set_phrase(array("web design mansfield")); 
    $seo->get_sites_use_api(10); 
    ob_start(); 
    foreach($seo->api_data as $key => $phrase_return){ 
     echo "<h2>" . $key . "</h2>"; 
     foreach($phrase_return as $rank => $results){ 
      $seo->set_get_actual($results['link']); 
      echo "<p class=\"link-head\"><strong>#" . ($rank + 1) . "</strong> <a href=\"" . $results['link'] . "\">" . $results['link'] . "</a></p>";  
      echo "<p>" . $results['title'] . "</p>"; 
      #echo "<p>" . $seo->get_actual_title() . "</p>"; 
      echo "<p>" . $seo->get_actual_description() . "</p>"; 
      #echo "<p>" . $seo->get_actual_amount_of('p') . "</p>"; 
      #echo "<p>" . $seo->get_actual_amount_of('h2') . "</p>"; 
     } 
    } 
    $api_return = ob_get_clean(); 
    ob_start(); 
    $seo->get_sites_use_spider(10); 
    foreach($seo->non_api_data as $key => $phrase_return){ 
     echo "<h2>" . $key . "</h2>"; 
     foreach($phrase_return as $rank => $results){ 
      echo "<p class=\"link-head\"><strong>#" . ($rank + 1) . "</strong> <a href=\"" . $results['link'] . "\">" . $results['link'] . "</a></p>";  
      echo "<p>" . $results['title'] . "</p>";  
     } 
    } 
    $non_api_return = ob_get_clean(); 
}catch(Exception $err){ 
    $error = $err->getMessage();  
} 


?> 

Моя проблема в том, что я получаю ошибку:

Fatal error: Cannot redeclare file_get_html() (previously declared in C:\wamp\www\seo\classes\simple_html_dom.php:37) in C:\wamp\www\seo\classes\simple_html_dom.php on line 41

который вызван последней функцией класса get_actual_description().

Может ли кто-нибудь увидеть, где именно взводится?

С уважением,

Phil

+0

жаль врезаться, но им застрял! –

ответ

4

положить require_once "simple_html_dom.php" вне функции

Смежные вопросы