cURL Loop Leaving Connections Open on Apache

Hello Guys,

My host is getting mad at me because they say I have “over 80 open connections” to the IP address that is being used in my cURL application.

my cURL loop, which runs about 25 times a page load, then refresh’s itself 30 seconds later, uses both curl_init($ch) and curl_close($ch).

Is there a reason why cURL is not closing the connection? Or would it be better to remove curl_close($ch) and leave the (Handler?) open.

I don’t seem to understand why it is creating a connection on apache and if so why it is keeping it open once I close it and the page closes.

Any insight please?

private function makeRequest($path){					
				//Use cURL
				
				$ch = curl_init($path);
				curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
				curl_setopt($ch, CURLOPT_HEADER, false);
				curl_setopt($ch, CURLOPT_FORBID_REUSE, true);
				curl_setopt($ch, CURLOPT_MAXCONNECTS, 1);
				$result = curl_exec($ch);
				
				if(!$path || (isset($ch) && curl_getinfo($ch, CURLINFO_HTTP_CODE) != 200)){
				//echo curl_getinfo($ch, CURLINFO_HTTP_CODE);
				throw new Exception('transfer failed');
			}

			curl_close($ch);
							
			$result = json_decode($result);
			if(!$result){
				throw new Exception('json_decode failed');
			}
				
			return $result;
		}

Try adding


curl_setopt(CURLOPT_FORBID_REUSE, true);
curl_setopt(CURLOPT_HTTPHEADER, array('Connection: close'));

Why do you need 25 curl connections in one request btw? Seems a bit extensive (depending on what you’re doing, of course). Any chance you could reduce that number, or maybe put some caching in place somewhere?

PS. Have a look at [fphp]curl_setopt_array[/fphp]; it’s a lot nicer than curl_setopt if have you have a lot of options to set (IMHO).

Connections will take time to close, regardless of whether you explicitly close them or not. It’s part of the negotiating process.

That said, why do you open 25 curl connections per page load??? Definitely agree with Scallio, caching will save you (and your host) a lot of headaches…

Because it is a site that connects to the Bungie API loading Halo: Game Stats from there site.

Each page loads 25 games, which need to be requested one a time.

Cache is not an option.

But no one even touched on the issue here?

well it sounds like their servers are terribly slow. have you tested that?
If that’s the case there is nothing you can do. well, except for caching, but you said you don’t want that.