在Laravel中将Multipart和Json与Guzzle一起发布
我正在尝试使用发布多部分和json数据来构建我的应用程序。我尝试了许多调整,但仍然得到错误的结果。下面是我正在使用的最新函数:在Laravel中将Multipart和Json与Guzzle一起发布,laravel,phonegap-build,guzzle,Laravel,Phonegap Build,Guzzle,我正在尝试使用发布多部分和json数据来构建我的应用程序。我尝试了许多调整,但仍然得到错误的结果。下面是我正在使用的最新函数: public function testBuild(Request $request) { $zip_path = storage_path('zip/testing.zip'); $upload = $this->client->request('POST', 'apps', ['json' =>
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 email@email.com
-F 'data={"title":"API V1 App","version":"0.1.0","create_method":"file"}'
https://build.phonegap.com/api/v1/apps
如前所述,不能同时使用
multipart
和json
在您的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',
]
),
]
]
]);
此选项不能与body、form_params或json一起使用
那么您的建议是什么?我应该把这篇文章还原成普通的CURL文章吗?你可以做2个请求,或者把它编码到URL中。也许这会有帮助:谢谢你,你的代码对我有用。但是为什么数组不能用于内容数据呢?因为contents
是一个字节数组json_encode()
,但它只适用于“简单”实体,multipart选项没有这样的助手。明白了,现在我明白Guzzle是如何工作的。非常感谢!