Sending SMS in PHP: Overview of API Libraries and Integration Examples
Sending SMS messages remains a popular feature for web applications: two-factor authentication, order notifications, marketing campaigns. In PHP, this task is solved via third-party service APIs. In this article, we will review popular libraries, how to install them, and provide working code examples.
1. Popular Services and Their PHP Libraries
The choice of provider depends on region, price, and delivery requirements. Let's consider three options:
- Twilio — an international leader, provides an SDK for PHP (twilio/sdk). Suitable for global projects.
- SMS Aero — a Russian service with a simple REST API and an official PHP library.
- smsc.ru — another popular domestic service with low rates and ready-made scripts.
2. Installation via Composer
Most modern libraries are installed via Composer. Example for Twilio:
composer require twilio/sdkFor SMS Aero use:
composer require smsaero/smsaero-phpIf you use smsc.ru, you can do without Composer — just copy their PHP class from GitHub.
3. Example of Sending SMS via Twilio
The Twilio library requires Account SID and Auth Token, which you get in your personal account.
<?phprequire_once 'vendor/autoload.php';
use Twilio\\Rest\\Client;
// Your credentials$sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";$token = "your_auth_token";$twilio = new Client($sid, $token);
// Sending SMS$message = $twilio->messages ->create( "+71234567890", // recipient number [ "from" => "+15017122661", // sender number (purchased in Twilio) "body" => "Hello! This is a test SMS from Twilio." ] );
echo "Message sent! SID: " . $message->sid;?>4. Sending SMS via SMS Aero (REST API)
SMS Aero uses an API key and login. The library provides a convenient interface:
<?phprequire_once 'vendor/autoload.php';
use Smsaero\\Smsaero;
$client = new Smsaero('your_email@example.com', 'your_api_key');
// Sending a message$response = $client->send('71234567890', 'Test message from SMS Aero');
if ($response->success) { echo "SMS sent. ID: " . $response->id;} else { echo "Error: " . $response->error;}?>5. Working with smsc.ru (without Composer)
For smsc.ru, you can use direct HTTP requests or their ready-made class. A simple example via cURL:
<?php$login = "your_login";$password = "your_password";$phones = "71234567890";$message = "Hello from smsc.ru!";
$url = "https://smsc.ru/sys/send.php?login=" . urlencode($login) . "&psw=" . urlencode($password) . "&phones=" . urlencode($phones) . "&mes=" . urlencode($message) . "&fmt=3"; // fmt=3 — JSON response
$ch = curl_init($url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);$response = curl_exec($ch);curl_close($ch);
$data = json_decode($response, true);if ($data['error'] ?? false) { echo "Error: " . $data['error'];} else { echo "SMS sent. ID: " . $data['id'];}?>6. Error Handling and Logging
When sending SMS, it is important to handle errors correctly. Typical problems:
- Invalid credentials (401 Unauthorized).
- Insufficient balance.
- Number is blacklisted.
- Sending limits exceeded.
Example of exception handling for Twilio:
<?phptry { $message = $twilio->messages->create(...); // success logging error_log("SMS sent: " . $message-&g