Bra*_*rad 23 php api laravel guzzle guzzle6
我可以使用Postman将文件上传到API端点.
我试图将其转换为从表单上传文件,使用Laravel上传并使用Guzzle 6发布到端点.
它在Postman中的外观截图(我故意省略了POST URL)

下面是您在POSTMAN中单击"生成代码"链接时生成的文本:
POST /api/file-submissions HTTP/1.1
Host: strippedhostname.com
Authorization: Basic 340r9iu34ontoeioir
Cache-Control: no-cache
Postman-Token: 6e0c3123-c07c-ce54-8ba1-0a1a402b53f1
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="FileContents"; filename=""
Content-Type:
----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="FileInfo"
{ "name": "_aaaa.txt", "clientNumber": "102425", "type": "Writeoff" }
----WebKitFormBoundary7MA4YWxkTrZu0gW
Run Code Online (Sandbox Code Playgroud)
以下是用于保存文件和其他信息的控制器功能.文件上传正确,我能够获取文件信息.
我认为我遇到的问题是使用正确的数据设置multipart和headers数组.
public function fileUploadPost(Request $request)
{
$data_posted = $request->input();
$endpoint = "/file-submissions";
$response = array();
$file = $request->file('filename');
$name = time() . '_' . $file->getClientOriginalName();
$path = base_path() .'/public_html/documents/';
$resource = fopen($file,"r") or die("File upload Problems");
$file->move($path, $name);
// { "name": "test_upload.txt", "clientNumber": "102425", "type": "Writeoff" }
$fileinfo = array(
'name' => $name,
'clientNumber' => "102425",
'type' => 'Writeoff',
);
$client = new \GuzzleHttp\Client();
$res = $client->request('POST', $this->base_api . $endpoint, [
'auth' => [env('API_USERNAME'), env('API_PASSWORD')],
'multipart' => [
[
'name' => $name,
'FileContents' => fopen($path . $name, 'r'),
'contents' => fopen($path . $name, 'r'),
'FileInfo' => json_encode($fileinfo),
'headers' => [
'Content-Type' => 'text/plain',
'Content-Disposition' => 'form-data; name="FileContents"; filename="'. $name .'"',
],
// 'contents' => $resource,
]
],
]);
if($res->getStatusCode() != 200) exit("Something happened, could not retrieve data");
$response = json_decode($res->getBody());
var_dump($response);
exit();
}
Run Code Online (Sandbox Code Playgroud)
我收到的错误,使用Laravel的调试视图显示它的显示截图:
rev*_*evo 47
POST数据的方式是错误的,因此收到的数据格式不正确.
值
multipart是一个关联数组数组,每个数组包含以下键值对:
name:(字符串,必填)表单字段名称
contents:(StreamInterface/resource/string,required)要在表单元素中使用的数据.
headers:(array)与表单元素一起使用的自定义标头的可选关联数组.
filename:(string)要作为部件中的文件名发送的可选字符串.
使用上面的列表中的键并设置不必要的标题而不将每个字段分成一个数组将导致发出错误的请求.
$res = $client->request('POST', $this->base_api . $endpoint, [
'auth' => [ env('API_USERNAME'), env('API_PASSWORD') ],
'multipart' => [
[
'name' => 'FileContents',
'contents' => file_get_contents($path . $name),
'filename' => $name
],
[
'name' => 'FileInfo',
'contents' => json_encode($fileinfo)
]
],
]);
Run Code Online (Sandbox Code Playgroud)