Reputation: 49
I am using an online laravel api documentation in my app. When i run my code in my browser, i get the error "pass phonenumber field as an array". But the number field is being already passed as an array field. What could i be missing below in my code ? Thanks in advance
public function testAPI(Request $request)
{
$on_call_back = 'https://learntoday.co.uk/var';
$id = '*****';
$url = $on_call_back.'?key='.$id;
$variables = [
'phoneNumber' => ['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);
curl_close($ch);
}
When i return $variables, i get the response
Array
(
[phoneNumber] => Array
(
[0] => 44234200234
[1] => 44234242002
)
[from] => test
[content] => I love to code
)
{"status":"error","message":"Make sure you are passing the phoneNumber field as an array"}
Upvotes: 1
Views: 74
Reputation: 17815
Change
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($variables));
to
curl_setopt($ch, CURLOPT_POSTFIELDS, $variables);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($variables));
You can also do curl_setopt($ch, CURLOPT_POST, 1);
instead of CURLOPT_CUSTOMREQUEST
.
Upvotes: 0
Reputation:
You should try this:
$variables = [
'phoneNumber' => ['44234200234','44234242002'],
'from' => 'world',
'content' => 'I love to code',
];
Updated answer
public function testAPI(Request $request)
{
$on_call_back = 'https://learntoday.co.uk/var';
$id = '*****';
$url = $on_call_back.'?key='.$id;
$variables = [
'phoneNumber' => ['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);
curl_close($ch);
}
Upvotes: 0
Reputation: 329
Please try send $variable as below code
$variables = array(
'phoneNumber' => array('44234200234','44234242002'),
'from' => 'world',
'content' => 'I love to code',
);
Upvotes: 0
Reputation: 14288
Your syntax of the array seems to be wrong, have you tried putting each number into single quotes like this:
'phoneNumber' => ['44234200234', '44234242002'],
Upvotes: 1