Jump to content

Recommended Posts

Hi all

 

I have a curl http class that i created, basically here is the constructor and destrcutor

 

class CurlHTTPClient {
	private $ch;

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

	public function __destruct() {
		curl_close($this->ch);
	}
               public function get($url, $referer = '') {

		return 'the url data';
	}

 

then i write thing like this, for example


session_start();  // Because i'm planning to use session

$a = new CurlHTTPClient();
echo $a->get('www.google.com');	

 

It told me there is a warning (which is i think the problem in __destruct) described below

Warning: curl_close(): supplied argument is not a valid cURL handle

 

If i remove session_start(), it works nicely.

 

How do i solve this problem? or suggestion.

(Since i'm relatively new to php, but not to programming)

 

Thank you :)

i dont think putting that suppressor is the solution  because it says invalid argument so it will not show error and it will not work

 

It's not invalid argument but an invalid resource that's passed.

 

It doesn't matter because the resource has probably already been cleared out (closed) by the time the actual call to curl_close() in the destructor has taken place.

 

If that error was for the curl_init() function, then I wouldn't be recommending to suppressor anything.

 

When there's PHP sessions involved, PHP internally does a lot of clean up, like save the session, clear memory, close open resources, etc.  I suspect that the curl resource got cleaned up by PHP internally, then it called your destructor, which by that time, the resource is no longer available (and hence nothing to close and you get the warning).

 

nekokumi, it also could be that the curl_init() never really worked in the first place. That function could return a boolean FALSE value instead of returning a valid resource. So, always check for errors and don't assume you got back what you're expecting.

 

http://us.php.net/manual/en/function.curl-init.php

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.