sempico / api-sms-php
Official PHP SDK for the Sempico Solutions SMS API: send SMS, HLR and MNP requests and handle delivery reports. Laravel and Yii2 integrations included.
Requires
- php: >=8.2
- ext-json: *
- psr/http-client: ^1.0
- psr/http-factory: ^1.0
- psr/http-message: ^1.1 || ^2.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.64
- guzzlehttp/guzzle: ^7.9 || ^8.0
- orchestra/testbench: ^9.0 || ^10.0 || ^11.0
- phpstan/phpstan: ^2.0
- phpunit/phpunit: ^11.0 || ^12.0
Suggests
- ext-curl: Needed by the default transport. Without it, pass a Psr18Transport.
- guzzlehttp/guzzle: PSR-18 HTTP client for Psr18Transport.
- laravel/framework: Service provider, facade, notification channel and DLR route are included.
- yiisoft/yii2: Application component and DLR action are included.
Provides
None
Conflicts
None
Replaces
None
README
Sempico Solutions SMS API — PHP SDK
Official PHP SDK for the Sempico Solutions SMS API.
- Send SMS, Flash SMS, WAP Push, vCard, HLR and MNP requests
- Typed requests and results, one exception class per API error
- Delivery reports (DLR) webhook handler with
API_KEYcheck and confirmation mode - Works with any PSR-18 HTTP client, or with ext-curl and no extra packages
- Ready integrations: Laravel (facade, notification channel, DLR route) and Yii2 (component, DLR action)
- Guides for Symfony, Slim / PSR-7 and plain PHP
API reference: https://api.sempico.solutions/desc/
Requirements
- PHP 8.2 or newer
ext-curlfor the default transport, or any PSR-18 client (Guzzle, Symfony HttpClient and others)- A Sempico account and an API token from the Web app
Installation
composer require sempico/api-sms-php
Quick start
use Sempico\Api\SempicoClient; $sempico = new SempicoClient(getenv('SEMPICO_TOKEN')); $result = $sempico->sms()->sendText('380501234567', 'MyShop', 'Your code: 1234'); echo $result->idState(); // 1855153903, use it to match delivery reports
Sending
All parameters of /send are in SendRequest. It is immutable: every with*() method returns a copy.
use Sempico\Api\Enum\MessageType; use Sempico\Api\Sms\SendRequest; $request = SendRequest::create('380501234567', 'MyShop', 'Sale starts tomorrow! https://shop.example/sale') ->withSendAt(new DateTimeImmutable('tomorrow 10:00')) // beginDate + beginTime ->withLifetime(3600) // try to deliver for 1 hour ->withCallbackUrl('https://shop.example/sempico/dlr') // where to send delivery reports ->withDelivery(true); $result = $sempico->sms()->send($request); $result->phone; // "380501234567" $result->idStates; // [1855153223, 1855443903], one ID per part $result->parts(); // 2 $result->isMultipart(); // true
Other message types:
$sempico->sms()->send(SendRequest::create('380501234567', 'MyShop', 'Hello')->withType(MessageType::FlashSms)); $sempico->sms()->send(SendRequest::create('380501234567', 'MyShop', 'HLR')->withType(MessageType::Hlr));
MessageType |
API value |
|---|---|
Sms (default) |
sms |
FlashSms |
flashsms |
WapPush |
wappush |
VCard |
vcard |
Hlr |
hlr |
Mnp |
mnp |
A parameter the SDK does not know yet can be added with ->withParameter('name', 'value').
Checking replacement rules
/replacement shows how the replacement rules of your account will change an SMS, or why it will be rejected. It does not send anything.
$check = $sempico->sms()->checkReplacement('380501234567', 'MyShop', 'Hello world!'); if ($check->allowed) { echo $check->content; // text after replacement echo $check->senderId; // sender after replacement echo $check->parts; // number of parts echo $check->mcc . '/' . $check->mnc; } else { echo $check->error; // for example "no_client_price" }
Errors
Every error of the SDK implements Sempico\Api\Exception\SempicoException. Catch the specific class when you want to react to one case:
use Sempico\Api\Exception\InsufficientFundsException; use Sempico\Api\Exception\InvalidPhoneException; use Sempico\Api\Exception\SempicoException; use Sempico\Api\Exception\TransportException; try { $sempico->sms()->sendText($phone, 'MyShop', $text); } catch (InvalidPhoneException $e) { // ask the user to correct the number } catch (InsufficientFundsException $e) { // alert: top up the balance } catch (TransportException $e) { // network problem: the SMS may or may not be created, do not resend blindly } catch (SempicoException $e) { $e->getErrorCode(); // "no_active_aggregator", "Incorrect token", ... }
The full table of error codes is in docs/errors.md.
Delivery reports (DLR)
Sempico sends the state of every SMS part with a GET request to your callback_url.
use Sempico\Api\Webhook\DlrHandler; $handler = new DlrHandler( apiKey: getenv('SEMPICO_WEBHOOK_KEY'), // security key of the account, checked in the API_KEY header confirmationMode: false, // true if the confirmation mode is enabled for the account ); $handler->handleGlobals(function (\Sempico\Api\Webhook\DeliveryReport $report): void { // $report->idState, $report->state (DeliveryState enum), $report->errorCode, $report->time(), $report->hlr });
Read docs/webhooks.md before going live: nginx and Apache drop headers with underscores, such as API_KEY, by default.
Frameworks
| Framework | What is included | Guide |
|---|---|---|
| Laravel 11–13 | Service provider (auto-discovered), Sempico facade, sempico notification channel, DLR route with an event |
docs/laravel.md |
| Yii2 | Application component, DlrAction, delivery report event |
docs/yii2.md |
| Symfony 6.4+ | Service configuration, DLR controller, Messenger example | docs/symfony.md |
| Slim, Mezzio and other PSR-7/PSR-15 | DLR handler for ServerRequestInterface |
docs/plain-php.md |
| Plain PHP | — | docs/plain-php.md |
HTTP client
By default the SDK uses ext-curl, with TLS verification on, a 10 s connect timeout and a 30 s request timeout:
use Sempico\Api\Config; $sempico = new SempicoClient(new Config(token: $token, timeout: 15, connectTimeout: 5));
To use your own PSR-18 client (for proxies, retries, logging):
use Sempico\Api\Http\Psr18Transport; $sempico = new SempicoClient($token, new Psr18Transport( new \GuzzleHttp\Client(['timeout' => 15]), new \GuzzleHttp\Psr7\HttpFactory(), ));
In tests, implement Sempico\Api\Http\TransportInterface and return prepared responses.
Security
- The token is sent in the URL, as the API requires. The SDK always uses HTTPS with certificate verification, hides the token in
var_dump()and removes it from error messages. - Do not log full request URLs to the API.
- Enable the IP white list and the webhook security key in the Web app.
Report vulnerabilities to support@sempico.solutions. See SECURITY.md.
Upgrading from 1.x
Sms::send() and Sms::replacement() still work in 2.x and are deprecated. See UPGRADE.md.
Development
composer install
composer check # code style, PHPStan (max level), PHPUnit
License
MIT © Sempico Solutions. See LICENSE.md.