Sending SMS in PHP: API libraries of sms.ru and other services

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Sending SMS in PHP: overview of API libraries and ready-made solutions



In modern web applications, sending SMS messages remains a popular feature: registration confirmation, two-factor authentication, order status notifications. In this article, we will look at how to implement sending SMS in PHP using popular API libraries and services. We will consider both official SDKs and lightweight cURL solutions.



1. Choosing an SMS provider and library



Before starting, you need to register with one of the SMS services and obtain an API key. For PHP, there are several proven options:



  • sms.ru – a popular Russian service with a simple API and an official PHP library.
  • Twilio – an international giant with a powerful SDK for PHP (twilio/sdk).
  • ClickSend – a versatile service with REST API support.
  • Simple cURL class – if you don't want to include external dependencies.


2. Installing libraries via Composer



For working with Twilio or ClickSend, it is most convenient to use Composer. Installation example:



composer require twilio/sdkcomposer require clicksend/clicksend-php


For sms.ru, the official library is also installed via Composer:



composer require smsru/smsru


If you prefer not to use a dependency manager, you can download the PHP files manually from GitHub or write your own cURL class.



3. Example of sending SMS via sms.ru



The sms.ru service provides a simple and clear API. Below is an example of sending a message using the official library:



<?phprequire_once 'vendor/autoload.php';

use SMSRU\\SMSRU;

$sms = new SMSRU('YOUR_API_KEY');

$data = new stdClass();$data->to = '79123456789'; // Recipient number$data->text = 'Test message'; // SMS text$data->from = 'MySite'; // Sender name (signature)

$response = $sms->send_one($data);

if ($response->status == 'OK') { echo "Message sent. ID: " . $response->sms_id;} else { echo "Error: " . $response->status_text;}?>


Important: you must first register the sender signature and top up your balance in the sms.ru personal account.



4. Sending SMS via Twilio SDK



Twilio is one of the most reliable services for international sending. PHP code example:



<?phprequire_once 'vendor/autoload.php';

use Twilio\\Rest\\Client;

$account_sid = 'YOUR_ACCOUNT_SID';$auth_token = 'YOUR_AUTH_TOKEN';$twilio_number = '+1234567890'; // Number obtained from Twilio

$client = new Client($account_sid, $auth_token);

$message = $client->messages->create( '+79123456789', // Recipient number [ 'from' => $twilio_number, 'body' => 'Hello from Twilio and PHP!' ]);

echo "Sent with ID: " . $message->sid;?>


Twilio supports two-way communication, webhooks, and delivery status checking.



5. Implementation without third-party libraries (cURL)



If you don't want to use Composer, you can write a universal cURL function. Example for sms.ru:



<?phpfunction sendSms($api_key, $phone, $message) {    $url = 'https://sms.ru/sms/send';    $params = [        'api_id' => $api_key,        'to' => $phone,        'msg' => $message,        'json' => 1    ];

$ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $params); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

$response = curl_exec($ch); curl_close($ch);

return json_decode($response, true);}

$result = sendSms('

Recommendations