curl php post - Service Unavailable - Maximum number of active clients reached
<p>Hello I am doing <code>curl</code> PHP POST request (because of the cross domain origin) and I am facing a problem. Post request is send to a machine that hosts some custom web server that I can't control. First requests (like 15-20 are ok) but after this number of requests I am getting <code>503 error</code> response with the error message </p>

<blockquote>
  <p>Service Unavailable - Maximum number of active clients reached.</p>
</blockquote>

<p>I suppose that curl is creating a new connection every time I send a request. I also suppose that the machine server can have only a few connections opened.</p>

<p>Here is my php code:</p>

<pre><code>$data = array("getTags" =&gt; array("Start_dav","CutON"), "includeTagMetadata" =&gt; false);                                                                    
$data_string = json_encode($data);

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,"http://domainipaddres/");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
curl_setopt($ch,CURLOPT_POSTFIELDS,$data_string);
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIE, "SID=8c81775da6");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER , false);
curl_setopt($ch, CURLOPT_FORBID_REUSE, false);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string),
       'Connection: Keep-alive',
       'Keep-alive: 300'
    )                                                                       
);  

//execute post
$result = curl_exec($ch);   
//close connection
curl_close($ch);
</code></pre>

<p>I really need to figure it out but after 2 days of trying different options and header datas I cant get it working. How to force curl to use only one connection?</p>

<p>Thank you very much</p>

<p>EDIT: I found a problem today. Server can only have 3 active clients. I found that in first response header from server is SET cookie and I need to use that cookie in another requests. I set it manually and it works. Is there any way how to do this automatically?</p>