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

Ded*_*kit 2 laravel guzzle phonegap-build

我正在尝试使用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;
}
Run Code Online (Sandbox Code Playgroud)

这是我正确的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
Run Code Online (Sandbox Code Playgroud)

Ale*_*kov 5

如前所述,不能将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',
                ]
            ),
        ]
    ]
]);
Run Code Online (Sandbox Code Playgroud)