Writing multiple post requests using single connection - PHP
I had found a solution to this at that point using php_curl
and KEEP-ALIVE
Here is my updated version:
function sendCall(&$curl_handle, $data){
curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt(
$curl_handle,
CURLOPT_HTTPHEADER,
array(
'Content-Type: application/json',
'Connection: Keep-Alive',
'Content-Length: ' . strlen($data)
)
);
$response = curl_exec($curl_handle); //Check response?
if($err = curl_error($curl_handle)) {
error_log("Error - $err Status - Reconnecting" );
$curl_handle = curl_init(curl_getinfo($curl_handle, CURLINFO_EFFECTIVE_URL));
sendCall($curl_handle, $data);
}
}
This function gives me an almost always alive connection. (Never got the error log in more than a week). Hope it helps anyone looking for the same.
Using fsockopen to POST to a GET query URL
You basically just have a typo:
Conent-Length: 9
See the missing t
in Content-Length:
The real question is (not to malign your efforts at constructing something yourself), why aren't you using cURL or PEARs HTTP Request class?
Request with fsockopen - response without headers
Use fread() to read the response. Change the fgets part with the following code:
while (!feof($fp)) {
$odpowiedz .= fread($this->socket, 1024);
}
Then, you can use the following code to strip only the body part:
$crlf = "\r\n";
$position = strpos($response, $crlf.$crlf);
$content = substr($response, $position + 2 * strlen($crlf));
Related Topics
Differencebetween ' and " in PHP
Should Lock_Ex on Both Read & Write Be Atomic
How Is an Array in a PHP Foreach Loop Read
MySQL Query In() Clause Slow on Indexed Column
Is There an Equivalent in C++ of PHP's Explode() Function
Require_Once :Failed to Open Stream: No Such File or Directory
Sending Correct File Size with PHP Download Script
How to Store Birthdate and Age So That Age Can Be Updated Daily in PHP/Mysql
Href="" Automatically Adds to Current Page Url (In PHP). Can't Figure It Out