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

I am making a PHP class that handles some traffic using CURL and everything works very well (with the exception of cookies, but this is one more thing). One thing that doesn't work so great is the curl_close () function, although I don't know why ...

$curlSession = &$tamperCurl->getCURLSession(); var_dump($curlSession); curl_close($curlSession); die(); 

I previously called curl_exec () and everything worked fine. The result that gives me: resource (6) of type (curl)

Warning : curl_close (): 6 is not a valid cURL handle resource in filename.php on line 58

Does anyone know why this is happening? (var_dump says this is obviously curl).

Addition:

Due to copyright issues, I cannot publish the entire TamperData atm class (it will later be the GPL).

I simplified this:

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

The constructor of TamperCurl is as follows:

 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 conclusion: resource (8) of type (curl) PHP Warning: curl_close (): 8 is not a valid cURL handle resource in TamperCurl.php on line 58

+6
source share
1 answer

In the end, the problem turned out to be this:

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

If you have already closed curlSession, the variable containing the resource is not set to NULL, but set to an "unknown type". Thus, this fixes the problem:

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

I'm not sure why, but it also fixed my cookie cookie problem, so it may be that if you try to close an already closed curl session, something will go wrong.

+7
source

Source: https://habr.com/ru/post/910342/


All Articles