使用Guzzle 6.3上传带有数据的音频文件

时间:2019-01-23 17:25:30

标签: php laravel guzzle

我正在使用guzzle 6.3将文件和数据发送到基于laravel构建的api。当我发布数据时,除了发布的文件之外,我无法将数据发送到api。

客户端

$client = new Client([
            'headers' => [ 'Content-Type' => 'application/json' ]
        ]);

        $response = $client->request('POST',$url, [
            'multipart' => [
                [
                    'name'     => 'body',
                    'contents' => json_encode(['documentation' => 'First Doc', 'recipient' => ['78011951231']]),
                    'headers'  => ['Content-Type' => 'application/json']
                ],
                [
                    'name'     => 'file',
                    'contents' => fopen('/path/public/media/aaaah.wav', 'r'),
                    'headers'  => ['Content-Type' => 'audio/wav']
                ],
            ],
        ]);
        echo($response->getBody()->getContents());

API控制器

 if (($Key)) {
        return response()->json([
            'status' => 'success',
            'documenation' => $request->get('documentation'),
            'recipient' => $request->get('recipient'),
            'file' =>$request->get('file'),
            'media'=>$request->hasFile('file')
        ]);
    }

回复

{"status":"error","documentation":null,,"recipient":null,"file":null,"media":true}

为什么我要发布的数据返回 NULL ?可能是因为我发布的文件?

0 个答案:

没有答案