简单的 html dom - 无法重新声明 str_get_html()

发布于 2024-09-28 20:38:06 字数 7229 浏览 4 评论 0原文

我试图创建一个类来帮助 SEO 并将 google API 与实际结果进行比较

类:

    <?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();    
}


?>

我的问题是我不断收到错误:

致命错误:无法在第 41 行的 C:\wamp\www\seo\classes\simple_html_dom.php 中重新声明 file_get_html()(之前在 C:\wamp\www\seo\classes\simple_html_dom.php:37 中声明)

这是由于类 get_actual_description() 中的最后一个函数造成的。

谁能看到我在哪里兴奋吗?

问候,

菲尔

Im trying to make a class to help with SEO and also compare google API with actual results

class:

    <?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();
    }
}

?>

called by :

<?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();    
}


?>

My problem being that I keep getting the error:


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

which is due to the last function in the class get_actual_description().

Can anyone see where im cocking up?

regards,

Phil

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼泪也成诗 2024-10-05 20:38:06

将 require_once "simple_html_dom.php" 放在函数之外

put require_once "simple_html_dom.php" outside of function

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文