smsapi/php-client
SMSAPI PHP Client is a Composer package for integrating your PHP app with the SMSAPI.COM and SMSAPI.PL APIs. Since v3 it supports PSR-17 and PSR-18 HTTP clients, with a ready-made Curl adapter. Install via composer require smsapi/php-client.
Installation
composer require smsapi/php-client:^4.0.1
Pin to ^4.0.1 or later. The v4.0.1 patch fixes a PHP 8.5 curl_close() deprecation, a TypeError when curl_init() fails, and several HTTP client cURL issues — if you are on PHP 8.4/8.5 or run in environments where cURL can fail (containers with restricted networking), earlier 4.0.x builds can throw fatal errors instead of returning a clean failure.
Verify the transport
php -m | grep curl
php -v
The library talks to the API over cURL. Confirm the extension is enabled and that you have not disabled allow_url_fopen-style outbound HTTP in your runtime.
Basic Configuration
Create a config file (e.g., config/smsapi.php) with your API credentials:
return [
'api_key' => env('SMSAPI_KEY'),
'base_url' => env('SMSAPI_BASE_URL', 'https://api.smsapi.com'),
];
First Use Case: Sending a Text Message
use Smsapi\Client;
$client = new Client(config('smsapi.api_key'), config('smsapi.base_url'));
$response = $client->sendSms(
'from' => 'YourBrand',
'to' => '1234567890',
'text' => 'Hello from Laravel!'
);
Laravel Service Provider (Optional) Bind the client to the container for dependency injection:
// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->singleton(Client::class, function ($app) {
return new Client(config('smsapi.api_key'), config('smsapi.base_url'));
});
}
Sending Bulk SMS
$client->sendSms([
'from' => 'YourBrand',
'to' => ['1234567890', '0987654321'],
'text' => 'Bulk message to multiple recipients.'
]);
Handling Responses Check the response status and data:
if ($response->isSuccess()) {
$messageId = $response->getMessageId();
// Log or store $messageId for tracking.
} else {
$error = $response->getError();
Log::error("SMS failed: " . $error);
}
Laravel Notifications Integration Extend the notification channel with a custom driver:
// app/Notifications/SmsVerification.php
public function via($notifiable)
{
return ['sms'];
}
public function toSms($notifiable)
{
return [
'from' => 'YourBrand',
'to' => $notifiable->phone,
'text' => 'Your verification code: ' . $this->verificationCode,
];
}
Queueing SMS Jobs Dispatch a job for async sending:
// app/Jobs/SendSmsJob.php
use Smsapi\Client;
public function handle(Client $client)
{
$client->sendSms($this->messageData);
}
Transport Resilience (new in 4.0.1)
A failed curl_init() now surfaces as a normal error instead of a TypeError. Treat transport failures and API failures as distinct so you can retry the former safely:
try {
$response = $client->sendSms($payload);
} catch (\Throwable $e) {
// Transport/connection level — retry with backoff, don't mark as "sent".
throw $e;
}
if (! $response->isSuccess()) {
// API level rejection (bad number, no credits, throttling).
}
.env for sensitive data (e.g., SMSAPI_KEY).4.0.0 explicitly so a fresh composer update doesn't silently move you between cURL behaviours.Package Maintenance Status
PHP 8.5 cURL Deprecation
curl_close() handling no longer emits the PHP 8.5 deprecation. If you are on 8.5 and still see the deprecation, you are resolving an older version — check with composer show smsapi/php-client.set_error_handler / ErrorException), older versions will hard-fail on sends.cURL Init Failures
curl_init() threw a TypeError that bypassed typical \Exception catches. Catch \Throwable, not just \Exception, if you must support older versions.Error Handling
try-catch:
try {
$response = $client->sendSms(...);
} catch (\Throwable $e) {
Log::error("SMS API error: " . $e->getMessage());
}
API Key Exposure
.env and config/services.php.Character Limits
$client->sendSms([
'from' => 'YourBrand',
'to' => '1234567890',
'text' => chunk_split($longText, 153, "\n"), // Split into parts
]);
debug mode or enable HTTP logging:
\Smsapi\Client::setDebug(true); // If supported.
dd($response->getRawResponse());
^4.0.1:
composer show smsapi/php-client
Custom Response Handling
Extend the Response class to add domain-specific logic:
class CustomResponse extends \Smsapi\Response
{
public function isDelivered()
{
return $this->getStatus() === 'DELIVERED';
}
}
Middleware for SMS Add middleware to validate phone numbers or log metadata:
// app/Http/Middleware/SmsValidation.php
public function handle($request, Closure $next)
{
if ($request->input('to') && !preg_match('/^\d{10,15}$/', $request->input('to'))) {
abort(422, 'Invalid phone number.');
}
return $next($request);
}
Mocking for Tests Use Laravel's mocking to test SMS logic without hitting the API:
$mock = Mockery::mock(Client::class);
$mock->shouldReceive('sendSms')->once()->andReturn(new \Smsapi\Response(true));
$this->app->instance(Client::class, $mock);
How can I help you explore Laravel packages today?