PHP Post Data with Fsockopen

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



Leave a reply



Submit