0

im trying to have a good practice at abstract class Methods so i created a simple curl wrapper class but unfortunately it doesn't work .
abstract

<?php
abstract class curl{
    private $url;

    public function __construct($url){
        $this->url = $url ;
    }
    public function curl_grab_page()
    {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
            curl_setopt($ch, CURLOPT_URL, $this->url);
            curl_setopt($ch, CURLOPT_HEADER, TRUE);
            curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
            ob_start(); // prevent any output
            return curl_exec ($ch); // execute the curl command
            ob_end_clean(); // stop preventing output
            curl_close ($ch);
    }
    public abstract function getHTML();

}
?>

child

<?php
class google extends curl{
    private $url;
    function __construct($url) {
        parent::__construct($url);
    }
    function curl_grab_page(){
        parent::curl_grab_page();
    }
    function getHTML(){
        return $this->curl_grab_page();
    }
}

and this is how i call in my front page .

<?php
include 'classes/class.curl.php';
include 'classes/class.google.php';
$google = new google('http://www.google.com/');
echo $google->getHTML();
?>

it didn't print out anything .
i tried the function separately and it goes fine

4

1 回答 1

1

看起来您在调用 return 之前没有本地化输出缓冲区的结果,试试这个:

public function curl_grab_page()
{
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_URL, $this->url);
        curl_setopt($ch, CURLOPT_HEADER, TRUE);
        curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);

        // Don't need any output buffering b/c you set CURLOPT_RETURNTRANSFER to true, which means the results will be returned via curl_exec
        //ob_start(); // prevent any output
        //return curl_exec ($ch); // execute the curl command
        //ob_end_clean(); // stop preventing output

        $contents = curl_exec($ch);

        curl_close ($ch);

        return $contents;
}

和子类:

<?php
class google extends curl{
    // Don't need this, if you set the parent's scope to protected which means child class has access
    // private $url;

    function __construct($url) {
        parent::__construct($url);
    }

    // Don't really need this method unless you plan on doing some custom logic        
    function curl_grab_page(){

        // If you keep this method I added a return here so we can get the results of the call
        return parent::curl_grab_page();
    }

    function getHTML(){
        return $this->curl_grab_page();
    }
}
于 2013-07-09T22:50:37.237 回答