https://www.workerman.net/q/11997 https://www.workerman.net/doc/workerman/components/workerman-http-client.html#%E5%8D%8F%E7%A8%8B%E7%94%A8%E6%B3%95 ``` composer require workerman/workerman ^5.0.0\-beta.7 composer require revolt/event-loop ^1.0.1 composer require workerman/http-client ^2.0.1 ``` ~~~ /************************************ 下面是基于workman本身的http客户端 ********************************************/ /** * option * @param int $timeout * @param int $connect_timeout * @param int $keepalive_timeout * @param int $max_conn_per_addr * @return int[] */ static function options(int $timeout=30, int $connect_timeout=30, int $keepalive_timeout=15, int $max_conn_per_addr=128): array { return [ 'max_conn_per_addr' => $max_conn_per_addr, // 每个域名最多维持多少并发连接 'keepalive_timeout' => $keepalive_timeout, // 连接多长时间不通讯就关闭 'connect_timeout' => $connect_timeout, // 连接超时时间 'timeout' => $timeout, // 请求发出后等待响应的超时时间 ]; } /** * 有使用到的 参考该模块 * @param $url * @param string $method * @param array $params * @return void */ static function request($url, string $method='POST', array $params=[]) { $options = HttpService::options(); $http = new Client($options); $http->request($url,[ 'method' => $method, 'version' => $params['version'] ?? '1.1', 'headers' => $params['headers'] ?? ['Connection' => 'keep-alive'], 'data' => $params['data'] ?? '', 'success' => function ($response){ echo $response->getBody(); }, 'error' => function ($exception){ echo $exception; } ]); } ~~~