Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

curl_close(): is not a valid cURL handle resource ... but WHY?

I'm making a PHP class that handles some traffic with the use of CURL and everything works quite well (except for cookies but hey that's another thing). One thing that doesn't work that great is the curl_close() function though and I have no idea why...

$curlSession = &$tamperCurl->getCURLSession();

var_dump($curlSession);
curl_close($curlSession);
die();

I previously called curl_exec() and everything worked perfectly. The output this is giving me is: resource(6) of type (curl)
Warning: curl_close(): 6 is not a valid cURL handle resource in filename.php on line 58

Does anybody have any idea why this is happening? (the var_dump is saying that it's obviously a curl session).

ADDITION:

Because of copyright problems I cannot post the whole TamperData class atm (it will be GPL later).

I have simplified it to this:

$tamperCurl = new TamperCurl('test.xml');
echo var_dump($tamperCurl->getCURLSession());
curl_close($tamperCurl->getCURLSession());
die();

The constructor of TamperCurl is like this:

public function __construct($xmlFilePath, $options=null)    
{
    if($options != null) $this->setOptions($options);

    $this->headerCounter = 0;
    $this->setXMLHeader($xmlFilePath);
    $this->init();
}

public function init($reuseConnection=false,$resetSettings=null)
{
    $this->curlSession = curl_init();
}

Again the same output: resource(8) of type (curl) PHP Warning: curl_close(): 8 is not a valid cURL handle resource in TamperCurl.php on line 58

like image 679
Tim Strijdhorst Avatar asked Mar 09 '12 11:03

Tim Strijdhorst


1 Answers

Eventually the problem turned out to be this:

public function __destruct()
{
    if($this->curlSession != null) curl_close($this->curlSession);
}

If you have already closed the curlSession, the variable containing the resource isn't set to NULL but it's set to 'unknown type'. So this fixes the problem:

public function __destruct()
{
    if(gettype($this->curlSession) == 'resource') curl_close($this->curlSession);
}

I'm not sure why but this also fixed my problem with cookies, so it might be that if you try to close an already closed curl session something else goes wrong.

like image 132
Tim Strijdhorst Avatar answered Nov 04 '22 06:11

Tim Strijdhorst