php 使用 Guzzle 6 上传文件到 API 端点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38133244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Upload file using Guzzle 6 to API endpoint
提问by Brad
I am able to upload a file to an API endpoint using Postman.
我可以使用 Postman 将文件上传到 API 端点。
I am trying to translate that into uploading a file from a form, uploading it using Laravel and posting to the endpoint using Guzzle 6.
我试图将其转换为从表单上传文件,使用 Laravel 上传并使用 Guzzle 6 发布到端点。
Screenshot of how it looks in Postman (I purposely left out the POST URL)
它在 Postman 中的外观截图(我故意省略了 POST URL)
Below is the text it generates when you click the "Generate Code" link in POSTMAN:
以下是当您单击 POSTMAN 中的“生成代码”链接时生成的文本:
POST /api/file-submissions HTTP/1.1
Host: strippedhostname.com
Authorization: Basic 340r9iu34ontoeitheitroad
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
Below is controller function for saving the file and other info. The file uploads correctly, I am able to get the file info.
下面是用于保存文件和其他信息的控制器功能。文件上传正确,我能够获取文件信息。
I think the problem I am having is setting the multipart and headers array with the correct data.
我认为我遇到的问题是使用正确的数据设置 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();
}
The error I am receiving, screenshot of how it displays using Laravel's debugging view:
我收到的错误,它是如何使用 Laravel 的调试视图显示的屏幕截图:
回答by revo
The way you are POSTing data is wrong, hence received data is malformed.
您发布数据的方式是错误的,因此接收到的数据格式错误。
狂饮文档:
The value of
multipart
is an array of associative arrays, each containing the following key value pairs:
name
: (string, required) the form field name
contents
:(StreamInterface/resource/string, required) The data to use in the form element.
headers
: (array) Optional associative array of custom headers to use with the form element.
filename
: (string) Optional string to send as the filename in the part.
的值
multipart
是一个关联数组的数组,每个数组包含以下键值对:
name
: (string, required) 表单字段名
contents
:(StreamInterface/resource/string, required) 要在表单元素中使用的数据。
headers
:(数组)与表单元素一起使用的自定义标题的可选关联数组。
filename
: (string) 作为文件名发送的可选字符串。
Using keys out of above list and setting unnecessary headers without separating each field into one array will result in making a bad request.
使用上面列表之外的键并设置不必要的标头而不将每个字段分成一个数组将导致发出错误的请求。
$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)
]
],
]);
回答by Pawel
$body = fopen('/path/to/file', 'r');
$r = $client->request('POST', 'http://httpbin.org/post', ['body' => $body]);
http://docs.guzzlephp.org/en/latest/quickstart.html?highlight=file
http://docs.guzzlephp.org/en/latest/quickstart.html?highlight=file