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
https://forums.phpfreaks.com/topic/212268-curl-problems-or-my-code-problems/
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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.