我用下面的代码将json和音频文件发布到基于laravel的api中。
$endPoint = 'http://localhost:9000/api/audio/send';
$apiKey = 'anvx7P7ackndaD8MvXlufSaG4uJ901raoWIwMPGZ93dkH';
$url = $endPoint . '?key=' . $apiKey;
$curlFile = new \CURLFile('/Users/desktop/myapp/public/Voice/aaaah.wav');
$data = array("request" =>
json_encode(array("test" => "First audio ",'recipient' =>['442342342', '35345242'])) . ";
type=application/json","file" => "@d8696c304d09eb1.wav;type=audio/wav");
$ch = curl_init();
$headers = array();
$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
$result = json_decode($result, TRUE);
curl_close($ch);
return $result;当我向api提交我的json时,它从api返回null。这意味着没有数据被发布到我的api。请问我将json和音频文件发布到laravel api的方式有什么错误吗?
PS: PHP初学者
发布于 2019-03-04 15:00:34
您不能在JSON!中发布文件。
需要一个普通的POST请求(从<form>元素生成的请求)来发布文件。
如果您绑定到使用JSON发送文件,请对其执行base64_encode操作并发送。不过,在另一端,服务器将需要首先base64_decode它。
https://stackoverflow.com/questions/54978022
复制相似问题