PHP
How to send SMS from PHP
Send an SMS from PHP using cURL and the eSMS Africa SMS API.
php
<?php
$ch = curl_init("https://sms.esmsafrica.io/api/messages/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",
"sender_id" => "MyBrand",
"text" => "Hello from PHP",
]),
]);
$response = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
echo $status . " " . $response;Send an SMS with cURL
Read your API key from the environment and POST a JSON body.
php
<?php
$ch = curl_init("https://sms.esmsafrica.io/api/messages/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",
"sender_id" => "MyBrand",
"text" => "Hello from PHP",
]),
]);
$response = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
echo $status . " " . $response;FAQ
Questions, answered
Can I use Guzzle instead of cURL?
Yes. Any HTTP client works - the endpoint is a standard JSON POST. SDKs are documented at docs.esmsafrica.io.
Ready to send?
Get an API key free and reach every African network.