Asynchronous HTTP requests in PHP

Fire an HTTP request at a URL and move on without waiting for a response.

function http_async($method, $host, $url, $data) {
    $query = http_build_query($data);
    $request = "$method $url HTTP/1.1\r\n"
        ."HOST: $host\r\n"
        ."Content-type: application/x-www-form-urlencoded"."\r\n"
        ."Content-Length: ".strlen($query)."\r\n"
        ."Connection: Close\r\n\r\n"
        .$query;
    $fp = fsockopen($host, 80, $errno, $errstr, 5);
    fwrite($fp, $request);
    fclose($fp);
}

And don’t forget to ignore_user_abort(true); on the target url.

No Comment

No comments yet

Leave a reply