I'm trying to implement following cURL request in php:
curl -X POST \
-H "Content-Type: application/json" \
-H "Authorization: login:password" \
-d '{"phone": "+48667421797", "msg": "Great app! Thank you!"}' \
https://some.endpoint.url
It works fine in console, I also managed to use this request in tools like postman/insomnia.
But when I try to implement it in PHP
like that:
$fields = [
'phone' => $number,
'msg' => $msg,
];
$data = json_encode($fields);
$headers = [
'Content-Type: application/json',
'Authorization: login:password'
];
$ch = curl_init($url);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $data);
curl_setopt($ch,CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, config('kajaki.sms_url'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$response = curl_exec($ch);
curl_close($ch);
I get 400: Incorrect schema
which AFAIK indicates, that the request is not being sent as valid JSON request.
What is wrong? How can I send this request correctly as a JSON?
Unfortunately this API does not provide any documentation.