PHP执行异步http请求

如果不用消息队列 简单情况下可以用一下代码 让php执行异步http请求,不会等待返回结果

代码来源:http://stackoverflow.com/questions/2662926/faster-alternative-to-file-get-contents

function curl_post_async($url, $params)
{
    foreach ($params as $key => &$val) {
      if (is_array($val)) $val = implode(',', $val);
        $post_params[] = $key.'='.urlencode($val);
    }
    $post_string = implode('&', $post_params);


    $parts=parse_url($url);


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


    $out = "POST ".$parts['path']." HTTP/1.1\r\n";
    $out.= "Host: ".$parts['host']."\r\n";
    $out.= "Content-Type: application/x-www-form-urlencoded\r\n";
    $out.= "Content-Length: ".strlen($post_string)."\r\n";
    $out.= "Connection: Close\r\n\r\n";
    if (isset($post_string)) $out.= $post_string;


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

你可能感兴趣的:(PHP执行异步http请求)