gpt4 book ai didi

laravel - 在 Laravel 中将 Multipart 和 Json 与 Guzzle 一起发布

转载 作者:行者123 更新时间:2023-12-02 20:47:02 25 4
gpt4 key购买 nike

我正在尝试使用 Guzzle 发布多部分和 json 数据使用 Phonegap Build API 构建我的应用程序。我尝试了很多调整,但仍然得到错误结果。这是我正在使用的最新功能:

public function testBuild(Request $request)
{
$zip_path = storage_path('zip/testing.zip');
$upload = $this->client->request('POST', 'apps',
['json' =>
['data' => array(
'title' => $request->title,
'create_method' => 'file',
'share' => 'true',
'private' => 'false',
)],
'multipart' =>
['name' => 'file',
'contents' => fopen($zip_path, 'r')
]
]);
$result = $upload->getBody();
return $result;
}

这是我正确的curl格式,它从API中获得了成功结果,但我的桌面上有文件:

curl -F file=@/Users/dedenbangkit/Desktop/testing.zip 
-u <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="096c64686065496c64686065276a6664" rel="noreferrer noopener nofollow">[email protected]</a>
-F 'data={"title":"API V1 App","version":"0.1.0","create_method":"file"}'
https://build.phonegap.com/api/v1/apps

最佳答案

如前所述,您不能同时使用 multipartjson

在您的 curl 示例中,它只是一个多部分形式,因此在 Guzzle 中使用相同的形式:

$this->client->request('POST', 'apps', [
'multipart' => [
[
'name' => 'file',
'contents' => fopen($zip_path, 'r'),
],
[
'name' => 'data',
'contents' => json_encode(
[
'title' => $request->title,
'create_method' => 'file',
'share' => 'true',
'private' => 'false',
]
),
]
]
]);

关于laravel - 在 Laravel 中将 Multipart 和 Json 与 Guzzle 一起发布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43930204/

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