gpt4 book ai didi

php - Guzzle 6 发送多部分数据

转载 作者:行者123 更新时间:2023-12-04 21:09:03 28 4
gpt4 key购买 nike

我想向 Guzzle Http 请求添加一些数据。有文件名,文件内容和带有授权 key 的标题。

$this->request = $this->client->request('POST', 'url', [
'multipart' => [
'name' => 'image_file',
'contents' => fopen('http://localhost:8000/vendor/l5-swagger/images/logo_small.png', 'r'),
'headers' =>
['Authorization' => 'Bearer uCMvsgyuYm0idmedWFVUx8DXsN8QzYQj82XDkUTw']
]]);

但我得到错误

Catchable Fatal Error: Argument 2 passed to GuzzleHttp\Psr7\MultipartStream::addElement() must be of the type array, string given, called in vendor\guzzlehttp\psr7\src\MultipartStream.php on line 70 and defined in vendor\guzzlehttp\psr7\src\MultipartStream.php line 79



在 Guzzle 6 文档中是这样的: http://docs.guzzlephp.org/en/latest/request-options.html#multipart

谁知道我哪里出错了?

最佳答案

这是解决方案。带有访问 token 的标题应该在 multipart 部分之外。

$this->request = $this->client->request('POST', 'request_url', [
'headers' => [
'Authorization' => 'Bearer access_token'
],
'multipart' => [
[
'Content-type' => 'multipart/form-data',
'name' => 'image_file',
'contents' => fopen('image_file_url', 'r')
]
]
]);

关于php - Guzzle 6 发送多部分数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37205939/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com