我正在开发一个应用程序,并且正在Laravel上使用自己的内置API。在我的API中,我使用以下代码在下面的APIController中以数组形式接收收件人编号。
现在,在尝试在DocController中测试我的API时,我不断收到错误消息。
将收件人字段作为数组传递
在DocController中,我已经将收件人作为数组传递了。我在下面的代码中可能还缺少什么?
ApiController
public function gearCall(Request $request)
{
$recipients = $request->post('recipient');
if (!is_array($recipients)) {
return response()->json([
'status' => 'error',
'message' => 'pass recipients field as an array'
]);
}
}
DocController
public function testAPI(Request $request)
{
$endPoint = 'https://lara.codein.com/api/';
$apiKey = '****';
$url = $endPoint . '?key=' . $apiKey;
$curlFile = curl_file_create('public/media/test.mp3');
$data = [
'recipient' => ['9082300239', '9052384200'],
];
$ch = curl_init();
$headers = array();
$headers[] = "Content-Type: multipart/form-data";
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, http_build_query($data));
$result = curl_exec($ch);
$result = json_decode($result, TRUE);
curl_close($ch);
当我在DocController中打印$ r $ data
Array
(
[recipient] => Array
(
[0] => 9082300239
[1] => 9052384200
)
{"status":"error","message":"pass recipients field as an array"}
答案 0 :(得分:0)
您必须告诉CURL这是HTTP POST请求,请尝试添加以下内容:
curl_setopt($ch, CURLOPT_POST, 1);