我正在使用在线laravel api文档执行程序。当我在邮递员中运行代码时,出现错误“将数字字段作为数组传递”。但是number字段已经作为数组字段传递。我的代码下面可能缺少什么?预先感谢
public function testAPI(Request $request)
{
$on_call_back = 'https://learntoday.co.uk/var';
$id = '*****';
$url = $on_call_back.'?key='.$id;
$variables = [
'number' => ['44234200234,44234242002'],
'from' => 'world',
'content' => 'I love to code',
];
$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, http_build_query($variables));
$result = curl_exec($ch);
$result = json_decode($result, TRUE);
return $data;
curl_close($ch);
}
当我打印出结果时,这就是我看到的
Array
(
[number] => Array
(
[0] => '44234200234,44234242002'
)
[from] => world
[content] => i love to code
)
{"status":"error","message":"Pass number field as an array"}
这是现在的显示方式
Array
(
[recipient] => Array
(
[0] => 44234200234
[1] => 44234242002
)
[from] => world
[content] => i love to code
)
答案 0 :(得分:0)
您应该尝试以下操作:
public function testAPI(Request $request)
{
$on_call_back = 'https://learntoday.co.uk/var';
$id = '*****';
$url = $on_call_back.'?key='.$id;
$variables = [
'number' => ['44234200234','44234242002'],
'from' => 'world',
'content' => 'I love to code',
];
$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, http_build_query($variables));
$result = curl_exec($ch);
$result = json_decode($result, TRUE);
return $data;
curl_close($ch);
}
答案 1 :(得分:0)
尝试这样:
$numbersArray = array("44234200234","44234242002");
$variables = [
//'number' => ['44234200234','44234242002'],
'number' => $numbersArray,
'from' => 'world',
'content' => 'I love to code',
];
理想情况下,旧的解决方案应该可以工作,但是您可以像这样尝试并检查它。
答案 2 :(得分:0)
您应该尝试在数组的不同索引上传递数字:
module.exports = {
runtimeCompiler: true
}
当前,您正在传递两个数字,并在索引0上用分隔。
答案 3 :(得分:0)
您将数组作为1个字符串传递。要使用这种(不正确的)方法,您需要用逗号分隔字符串,然后将每个字符串转换为整数。
'number' => ['44234200234,44234242002,1,2,3,4'], // this is ONE string
'number' => ['1', '2', '3', '4'] // this is many strings
您想要做的是传递一个整数数组
numbers => [1, 2, 3]
快乐编码:-)
答案 4 :(得分:0)
尝试使用序列化功能:
$on_call_back = 'https://learntoday.co.uk/var';
$id = '*****';
$url = $on_call_back.'?key='.$id;
$variables = [
'number' => serialize(['44234200234','44234242002']),
'from' => 'world',
'content' => 'I love to code',
];