Send an SMS from PHP using cURL and the eSMS Africa SMS API.
<?php
$ch = curl_init("https://api.esmsafrica.io/v1/sms/send");
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => [
"Authorization: Bearer " . getenv("ESMS_API_KEY"),
"Content-Type: application/json",
],
CURLOPT_POSTFIELDS => json_encode([
"to" => "+254712345678",
"from" => "MyBrand",
"message" => "Hello from PHP",
]),
]);
$response = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
echo $status . " " . $response;Read your API key from the environment and POST a JSON body.
<?php
$ch = curl_init("https://api.esmsafrica.io/v1/sms/send");
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => [
"Authorization: Bearer " . getenv("ESMS_API_KEY"),
"Content-Type: application/json",
],
CURLOPT_POSTFIELDS => json_encode([
"to" => "+254712345678",
"from" => "MyBrand",
"message" => "Hello from PHP",
]),
]);
$response = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
echo $status . " " . $response;Yes. Any HTTP client works - the endpoint is a standard JSON POST. An official PHP SDK is also available.