2.0, 'debug' => false, ]; public static function get(string $url, array $params = [], array $headers = []) { if (!empty($params)) { $url .= '?'; foreach ($params as $pk => $pv) { if (is_array($pv)) { foreach ($pv as $key => $item) { $url .= $pk . "[{$key}]=" . $item . '&'; } } else { $url .= $pk . '=' . $pv . '&'; } } $url = rtrim($url, '&'); } if (empty($headers)) { $rst = self::request($url); } else { $rst = self::request($url, 'get', [], $headers); } return $rst; } public static function post(string $url, array $data = [], array $headers = [], string $format = 'json') { return self::request($url, 'post', $data, $headers, $format); } /** * @name: * @msg: 上传图片,图片必须是本地图片才可以上传成功 * @param {string} $upload_url 上传路径 * @param {string} $filename 本地图片路径 * @param {array} $headers * @return {*} */ public static function upload(string $upload_url, string $filename = '', array $headers = []) { if (!empty($headers)) { $headers = array_merge(['Content-Type' => 'multipart/form-data'], $headers); } else { $headers = ['Content-Type' => 'multipart/form-data']; } $img_str = file_get_contents($filename); if (false === $img_str) throw new \Exception('文件不存在'); $path = '/tmp/'; if (!file_exists($path)) mkdir($path, 0755, true); $file_name = rtrim($path, '/') . '/' . pathinfo($filename, PATHINFO_BASENAME); file_put_contents($file_name, $img_str); $post_data = [ "upload_type" => "images", //要上传的本地文件地址 "file" => new \CURLFile($file_name) ]; $header = []; foreach ($headers as $key => $val) { $header[] = $key . ':' . $val; } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $upload_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); $output = curl_exec($ch); curl_close($ch); $rst = json_decode($output, true); @unlink($file_name); return $rst; } public static function postFile(string $upload_url, array $params = [], array $headers = []) { // $file_path = $params['file_path']; // unset($params['file_path']); // $post_data = ["file" => new \CURLFile($file_path)]; // $post_data = array_merge($post_data,$params); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $upload_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $params); $output = curl_exec($ch); curl_close($ch); return json_decode($output, true); } private static function request(string $url, string $method = 'get', array $params = [], array $headers = [], string $format = 'json') { $client = new Client(self::$config); if ('get' == $method) { $response = $client->request($method, $url, ['headers' => $headers,'timeout'=>20]); } else { $response = $client->request($method, $url, [$format => $params, 'headers' => $headers]); } $status_code = $response->getStatusCode(); if (200 != $status_code) { throw new \Exception('网络请求错误, http code: ' . $status_code . ', reason: ' . $response->getReasonPhrase()); } $rst = json_decode($response->getBody()->getContents(), true); return $rst; } //下载文件 public static function downImgRar($url, $file_name, $ext,$absolute_dir) { if (!is_dir($absolute_dir)) { mkdir($absolute_dir, 0777, true); } $ch = curl_init($url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); $rawdata = curl_exec($ch); curl_close($ch); // 使用中文文件名需要转码 $fp = fopen($absolute_dir . iconv('UTF-8', 'GBK', $file_name) . "." . $ext, 'w'); fwrite($fp, $rawdata); fclose($fp); // 返回路径 return $absolute_dir . $file_name . "." . $ext; } /** * post * * @param string $url * @param array $params * @param array $headers * @return string */ public static function postJson(string $url, array $params = [], array $headers = []) { $http = new Client(['headers' => [ "Content-type" => 'application/json' ]]); $response = $http->post($url, [ 'form_params' => $params ]); //返回值 $result = $response->getBody()->getContents(); return Json::decode($result); } public static function postJsonUnicode(string $url, array $params = [], array $headers = []){ $post_data = json_encode($params,JSON_UNESCAPED_UNICODE); $ch = curl_init(); curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS,$post_data); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); $headers = array_merge($headers,[ 'Content-Type: application/json', 'Content-Length: ' . strlen($post_data) ]); curl_setopt($ch, CURLOPT_HTTPHEADER,$headers); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//这里的意思是请求https curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);//这里的意思是请求https $res = curl_exec($ch); $res = json_decode($res,true);//转成数组 curl_close($ch); return $res; } public static function postV2(string $url, array $params = [], array $headers = []){ $post_data = json_encode($params,JSON_UNESCAPED_UNICODE); $ch = curl_init(); curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS,$post_data); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); $newHeaders = []; foreach ($headers as $k => $v) { $newHeaders[] = sprintf('%s: %s', $k, $v); } curl_setopt($ch, CURLOPT_HTTPHEADER, $newHeaders); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//这里的意思是请求https curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);//这里的意思是请求https $res = curl_exec($ch); $sourceData = $res; $res = json_decode($res,true);//转成数组 curl_close($ch); return is_array($res) ? $res : $sourceData; } public static function setConfig(array $config) { self::$config = $config; } }