smsapi/php-client
PHP client library for SMSAPI, providing a simple way to send SMS and manage messaging features from PHP applications. Suitable for integrating SMS notifications and related services into Laravel or custom PHP projects.
Installation
composer require smsapi/php-client
(Note: Since the package is archived, verify compatibility with your PHP version and Laravel setup.)
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 MustBeVerified notification channel:
// app/Notifications/SmsVerification.php
use Smsapi\Client;
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);
}
.env for sensitive data (e.g., SMSAPI_KEY).Archived Package Risks
Error Handling
try-catch:
try {
$response = $client->sendSms(...);
} catch (\Exception $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());
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?