Jump to content

curl problems or my code problems?


chaiwei

Recommended Posts


class curl2{

    private $curl_init; 
    private $CURLOPT_URL;

    public function connect(){ 
       $this->curl_init = curl_init(); 
    }

    public function debug(){

       curl_setopt($this->curl_init, CURLOPT_VERBOSE, TRUE);
       $fp = fopen("curl2.txt", "w"); 
       curl_setopt($this->curl_init, CURLOPT_STDERR, $fp); 
       curl_setopt($this->curl_init, CURLOPT_RETURNTRANSFER, TRUE);

    }

    public function setUrl($url = null){
       $this->CURLOPT_URL = $url;
       curl_setopt($this->curl_init, CURLOPT_URL, $this->CURLOPT_URL);
    }

    public function execute(){

       $out = curl_exec($this->curl_init);
       curl_close($this->curl_init);

       return $out;
    }
}

$curl2 = new curl2;
$curl2->connect();
$curl2->setUrl("http://www.linuxformat.co.uk");
$curl2->debug();
echo $curl2->execute();

 

It display a blank page like attachment result1.jpg, but if I move the

$fp = fopen("curl2.txt", "w"); 
curl_setopt($this->curl_init, CURLOPT_STDERR, $fp); 
curl_setopt($this->curl_init, CURLOPT_RETURNTRANSFER, TRUE);

from function debug() and join it with function execute()

like this:

  public function execute(){
       $fp = fopen("curl2.txt", "w"); 
       curl_setopt($this->curl_init, CURLOPT_STDERR, $fp); 
       curl_setopt($this->curl_init, CURLOPT_RETURNTRANSFER, TRUE);

       $out = curl_exec($this->curl_init);
       curl_close($this->curl_init);

       return $out;
    }

 

it return me Linuxformat content ( expected result ) like result2.jpg

 

below is the working code :

class curl2{

    private $curl_init; 
    private $CURLOPT_URL;

    public function connect(){ 
       $this->curl_init = curl_init(); 
    }

    public function debug(){

       curl_setopt($this->curl_init, CURLOPT_VERBOSE, TRUE);
      

    }

    public function setUrl($url = null){
       $this->CURLOPT_URL = $url;
       curl_setopt($this->curl_init, CURLOPT_URL, $this->CURLOPT_URL);
    }

    public function execute(){
       $fp = fopen("curl2.txt", "w"); 
       curl_setopt($this->curl_init, CURLOPT_STDERR, $fp); 
       curl_setopt($this->curl_init, CURLOPT_RETURNTRANSFER, TRUE);

       $out = curl_exec($this->curl_init);
       curl_close($this->curl_init);

       return $out;
    }
}

$curl2 = new curl2;
$curl2->connect();
$curl2->setUrl("http://www.linuxformat.co.uk");
$curl2->debug();
echo $curl2->execute();

 

Why I couldn't split "CURLOPT_STDERR, CURLOPT_RETURNTRANSFER" with "curl_exec"

 

 

[attachment deleted by admin]

Link to comment
Share on other sites

ok.. found out the reason,

 

It seems like I need to share the file open resources between those function

below is the working code.

class curl2{

    private $curl_init; 
    private $CURLOPT_URL;
    private $fp;

    public function connect(){ 
       $this->curl_init = curl_init(); 
       return $this;
    }

    public function debug(){

       curl_setopt($this->curl_init, CURLOPT_VERBOSE, TRUE);

       $this->fp = fopen("curl2.txt", "w");  
       curl_setopt($this->curl_init, CURLOPT_STDERR, $this->fp); 

       curl_setopt($this->curl_init, CURLOPT_RETURNTRANSFER, TRUE);

       return $this;
    }

    public function setUrl($url = null){
       $this->CURLOPT_URL = $url;
       curl_setopt($this->curl_init, CURLOPT_URL, $this->CURLOPT_URL);
       return $this;

    }

    public function execute(){
        
       $out = curl_exec($this->curl_init);
       curl_close($this->curl_init);

       return $out;
    }
}

$curl2 = new curl2;

$result = $curl2->connect()
         ->setUrl("http://www.linuxformat.co.uk")
         ->debug()
         ->execute();
print $result;

 

just change the $fp to $this->fp

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.