Hello I am doing curl
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 503 error
response with the error message
Service Unavailable - Maximum number of active clients reached.
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.
Here is my php code:
$data = array("getTags" => array("Start_dav","CutON"), "includeTagMetadata" => 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);
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?
Thank you very much
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?
I solved the problem today. I inspected response header and saw that server sets cookies. I just had to save cookies and use it in another requests and its working. Here is part of the code:
$data_string = json_encode($data);
$agent= 'Mozilla/5.0 (Windows; U;Windows NT 5.1; ru; rv:1.8.0.9) Gecko/20061206 Firefox/1.5.0.9';
//open connection
$ch = curl_init();
$f = fopen('request.txt', 'w'); //writes headers to this file
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,"domainipaddress");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch,CURLOPT_POSTFIELDS,$data_string);
$tmpfname = dirname(__FILE__).'/cookie.txt'; //saves the cookie from server
curl_setopt($ch, CURLOPT_COOKIEJAR, $tmpfname);
curl_setopt($ch, CURLOPT_COOKIEFILE, $tmpfname);
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER , false);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
curl_setopt( $ch, CURLOPT_VERBOSE, 1 );
curl_setopt( $ch, CURLOPT_STDERR, $f );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
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);
$headers = curl_getinfo($ch);
fclose($f);
Thank you anyway