You use stream_socket_client()
. Do your GET request then get the results later.
Make Request with stream_socket_client()
$host = 'www.example.com';
$path = '/';
$http = "GET $path HTTP/1.0
Host: $host
";
$stream = stream_socket_client("$host:80", $errno,$errstr, 120,STREAM_CLIENT_ASYNC_CONNECT|STREAM_CLIENT_CONNECT);
if ($stream) {
$sockets[] = $stream; // supports multiple sockets
fwrite($stream, $http);
}
else {
$err .= "$id Failed<br>
";
}
Get Responses with stream_select()
$timeout = 120;
$buffer_size = 8192;
while (count($sockets)) {
$read = $sockets;
stream_select($read, $write = NULL, $except = NULL, $timeout);
if (count($read)) {
foreach ($read as $r) {
$id = array_search($r, $sockets);
$data = fread($r, $buffer_size);
if (strlen($data) == 0) { // done
fclose($r);
unset($sockets[$id]);
}
else {
$result[$id] .= $data; // append buffer to result
}
}
}
else {
// echo 'Timeout: ' . date('h:i:s') . "
";
break;
}
}
UPDATE
You can make a request at anytime, and get the response anytime after the request. When a socket is created the $sockets array's key is the $id.
You do not have to have the while loop if you want to use some other control method. The buffer in the example is 8K. If the response is more than 8K it will take multiple reads.
If you do not want to retrieve the response then just close the socket and do not use the $sockets array. You may or may not need a delay before the fclose(). It depends upon how the host responds to a dropped connection.
$host = 'www.example.com';
$path = '/?param=value';
$http = "GET $path HTTP/1.0
Host: $host
";
$stream = stream_socket_client("$host:80", $errno,$errstr, 120,STREAM_CLIENT_ASYNC_CONNECT|STREAM_CLIENT_CONNECT);
if ($stream) {
fwrite($stream, $http);
fclose($stream);
}
else {
$err .= "$id Failed<br>
";
}