public static function formFilePost2($url, $sourceInfo) { $fileName = $sourceInfo['name']; $fileContent = file_get_contents($sourceInfo['tmp_name']); $fileType = $sourceInfo['type']; $filesize = $sourceInfo['size']; // 生成随机字符boundary $boundary = '----WebKitFormBoundary'.Str::getRandChar(15); $multipart_body = "--{$boundary}\r\nContent-Disposition: form-data; name=\"filesize\"\r\n\r\n{$filesize}\r\n--{$boundary}\r\nContent-Disposition: form-data; name=\"name\"\r\n\r\n{$fileName}\r\n--{$boundary}\r\nContent-Disposition: form-data; name=\"file\"; filename=\"{$fileName}\"\r\nContent-Type: {$fileType}\r\n\r\n". $fileContent ."\r\n--{$boundary}--"; $header = array( //"Authorization: Bearer $TOKEN", "cache-control: no-cache", "Content-Type: multipart/form-data; boundary=" . $boundary, "Content-Length:" . strlen($multipart_body) ); $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 300, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST =>"POST", CURLOPT_POSTFIELDS => $multipart_body, CURLOPT_HTTPHEADER => $header, )); $response = curl_exec($curl); //默认成功返回文件id if(is_numeric($response)) return $response; // $info = curl_getinfo($curl); // print_r($multipart_body); // var_dump($response); $err = curl_error($curl); if($err) { throw new \Exception($err); } curl_close($curl); return json_decode($response, 1); }