02-06-2023 05:00 AM - last edited on 03-16-2023 04:51 AM by Shahzad_Ismail
I have copied the PHP code from your website and whenever I run it, I get the the error :-
"Use of undefined constant CURLAUTH_BEARER - assumed 'CURLAUTH_BEARER'", here is the code here below please help.
<?php
$service_plan_id = "acbb90139d844ac985e72d7760xxxxxx"; //ServiceplanID
$bearer_token ="-Uri https://sms.api.sinch.com/xms/v1/acbb90139d844ac985e72d7760xxxxxx/batches";
//Any phone number assigned to your API
$send_from = "447537404817";
//May be several, separate with a comma ,
$recipient_phone_numbers = "+277202xxxx";
$message = "Test message to {$recipient_phone_numbers} from {$send_from}";
// Check recipient_phone_numbers for multiple numbers and make it an array.
if(stristr($recipient_phone_numbers, ',')){
$recipient_phone_numbers = explode(',', $recipient_phone_numbers);
}else{
$recipient_phone_numbers = [$recipient_phone_numbers];
}
// Set necessary fields to be JSON encoded
$content = [
'to' => array_values($recipient_phone_numbers),
'from' => $send_from,
'body' => $message
];
$data = json_encode($content);
$ch = curl_init("https://us.sms.api.sinch.com/xms/v1/{$service_plan_id}/batches");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BEARER);
curl_setopt($ch, CURLOPT_XOAUTH2_BEARER, $bearer_token);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
if(curl_errno($ch)) {
echo "Curl error: " . curl_error($ch);
} else {
echo $result;
}
curl_close($ch);
}
?>
02-06-2023 07:43 AM
Hello, thank you for reaching out!
Can you let us know which versions of PHP/cURL you have installed? The CURLAUTH_BEARER constant has been available since PHP 7.3.0 and cURL 7.61.0, so you may be running into issues if you are using a prior version of either.
Thanks again!
02-06-2023 08:42 AM
Hi Sam
I'm using cURL 7.83.1
02-06-2023 09:05 AM - edited 02-06-2023 09:06 AM
Hello again!
Thank you for your response. I've reached out to someone on our SMS team to see if they can provide any more insight into this issue. In the meantime, you can try to use the code sample provided in our API reference (you can set the language to PHP using the drop-down list above the code sample). That sample makes use of the libcurl library, and does not reference the CURLAUTH_BEARER constant. Finally, do you know which version of PHP you are using? That may also be contributing to your issue.
Thanks again!