美文网首页
PHP CURL 异步请求

PHP CURL 异步请求

作者: 可望不可j | 来源:发表于2022-05-07 13:49 被阅读0次
/**
 * php异步请求 (全路径:www.a.com/a/b.php)
 * @param string $host  主机地址 例如: www.a.com
 * @param string $path  路径    例如: /a/b.php
 * @param array $param  请求参数
 * @param int $port 端口
 * @return string
 */
function asyncRequest($host, $path, $param = array(),$port=80){
    $query = isset($param) ? http_build_query($param) : '';
    $errno = 0;
    $errstr = '';
    $timeout = 30; //连接超时时间(S)

    $fp = @fsockopen($host, $port, $errno, $errstr, $timeout);

    if (!$fp) {
        //连接失败
        return false;
    }
    if ($errno || !$fp) {
        // $errstr
        return false;
    }
    stream_set_blocking($fp,0); //非阻塞
    stream_set_timeout($fp, 1);//响应超时时间(S)
    $out  = "POST " . $path . " HTTP/1.1\r\n";
    $out .= "host:" . $host . "\r\n";
    $out .= "content-length:" . strlen($query) . "\r\n";
    $out .= "content-type:application/x-www-form-urlencoded\r\n";
    $out .= "connection:close\r\n\r\n";
    $out .= $query;

    $result = @fputs($fp, $out);

    @fclose($fp);
    return $result;
}

相关文章

网友评论

      本文标题:PHP CURL 异步请求

      本文链接:https://www.haomeiwen.com/subject/wzfxurtx.html