PHP通过fsock实现异步HTTP请求

2016-06-29

参考文章:

PHP实现异步调用方法研究 使用fscok实现异步调用PHP how-to-make-async-requests-in-php

function asyncRequest($url, $params = array(), $type = 'GET')
{
    $query = http_build_query($params);

    $parts = parse_url($url);

    $fp = fsockopen(
        $parts['host'],
        isset($parts['port']) ? $parts['port'] : 80,
        $errno,
        $errstr,
        30
    );

    $location = $parts['path'] . (empty($parts['query']) ? '' : "?{$parts['query']}");
    $location .= ($type == 'GET' && !empty($query)) ? (empty($parts['query']) ? '?' : '&').$query : '';

    $out = "{$type} {$location} HTTP/1.1\r\n";
    $out .= "Host: {$parts['host']}\r\n";
    $out .= "Connection: Close\r\n\r\n";
    $out .= $type == 'POST' ? "Content-Type: application/x-www-form-urlencoded\r\n" : '';
    $out .= 'Content-Length: '.strlen($query)."\r\n";
    $out .= ($type == 'POST' && isset($query)) ? $query : '';

    fwrite($fp, $out);
    fclose($fp);
}

注意点:

服务端 PHP 要设置 ignore_user_abortture,Apache 需要在 Apache.conf 中设置对应的 php_value ignore_user_abort,nginx 需要设置 fastcgi_ignore_client_aborton