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

3
  • new $google => new NULL => fatal error... Drop the $. Commented Jul 9, 2013 at 22:35
  • Try: $google = new google('http://www.google.com/'); Commented Jul 9, 2013 at 22:36
  • i did droped it but still have nothing Commented Jul 9, 2013 at 22:42

1 Answer 1

1

Looks like you are not localizing the results of the output buffer before calling return, try this:

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;
}

And the child class:

<?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();
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

it didn't print anything too . i think the problem i missed is with the hierarchical of the classes and miss use of abstract method because i tried the function separately out of the class and it goes well and printed the outpput
Ok, a few problems. You have CURLOPT_RETURNTRANSFER set to true, but you are trying to trap the output using the buffer, you only need one or the other. Also, in the child's curl_grab_page you are making a call to parent but not returning the results. If it were me I would just remove the child's version, unless you plan on doing some custom logic specific to google. I updated code.
this was the problem curl_grab_page not returning output in the child class u though it must return the output since it return within the parent class. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.