false, 'errno' => $errno, 'msg' => $err, 'info' => $info, ]; } curl_close($ch); return [ 'ret' => true, 'msg' => $ret, ]; } /** * 异步发送一个请求 * @param string $url 请求的链接 * @param mixed $params 请求的参数 * @param string $method 请求的方法 * @return boolean TRUE */ public static function sendAsyncRequest($url, $params = [], $method = 'POST') { $method = strtoupper($method); $method = $method == 'POST' ? 'POST' : 'GET'; //构造传递的参数 if (is_array($params)) { $post_params = []; foreach ($params as $k => &$v) { if (is_array($v)) { $v = implode(',', $v); } $post_params[] = $k . '=' . urlencode($v); } $post_string = implode('&', $post_params); } else { $post_string = $params; } $parts = parse_url($url); //构造查询的参数 if ($method == 'GET' && $post_string) { $parts['query'] = isset($parts['query']) ? $parts['query'] . '&' . $post_string : $post_string; $post_string = ''; } $parts['query'] = isset($parts['query']) && $parts['query'] ? '?' . $parts['query'] : ''; //发送socket请求,获得连接句柄 $fp = fsockopen($parts['host'], $parts['port'] ?? 80, $errno, $errstr, 10); if (!$fp) { return false; } //设置超时时间 stream_set_timeout($fp, 10); $out = "{$method} {$parts['path']}{$parts['query']} 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 ($post_string !== '') { $out .= $post_string; } fwrite($fp, $out); //不用关心服务器返回结果 //echo fread($fp, 1024); fclose($fp); return true; } /** * 发送文件到客户端 * @param string $file * @param bool $delaftersend * @param bool $exitaftersend */ public static function sendToBrowser($file, $delaftersend = true, $exitaftersend = true) { if (file_exists($file) && is_readable($file)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment;filename = ' . basename($file)); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check = 0, pre-check = 0'); header('Pragma: public'); header('Content-Length: ' . filesize($file)); ob_clean(); flush(); readfile($file); if ($delaftersend) { unlink($file); } if ($exitaftersend) { exit; } } } }