websms/php-sdk
PHP SDK for the websms service. Minimal repository with basic project scaffolding and contributor info; intended as a starting point for integrating websms messaging features into PHP applications.
Installation Add the package via Composer:
composer require websms/php-sdk
Require the autoloader in your Laravel project (handled automatically by Composer).
First Use Case: Sending an SMS Initialize the client with your WebSms API credentials:
use Websms\Client;
$client = new Client([
'username' => env('WEBSMS_USERNAME'),
'password' => env('WEBSMS_PASSWORD'),
'sender' => env('WEBSMS_SENDER_ID', 'YourBrand')
]);
Store credentials in .env:
WEBSMS_USERNAME=your_username
WEBSMS_PASSWORD=your_password
WEBSMS_SENDER_ID=YourBrand
Send a Basic SMS
$response = $client->send([
'to' => '1234567890',
'message' => 'Hello from Laravel!'
]);
Check the response for success/failure:
if ($response->isSuccess()) {
Log::info("SMS sent: " . $response->getMessageId());
}
Bulk SMS Sending
Use a loop or collect() to send to multiple recipients:
$recipients = ['1234567890', '0987654321'];
foreach ($recipients as $number) {
$client->send(['to' => $number, 'message' => 'Your message']);
}
For async processing, dispatch a job:
SendSmsJob::dispatch($client, $number, $message);
Template-Based Messages Use WebSms templates (if supported) for dynamic content:
$client->send([
'to' => '1234567890',
'message' => 'Your code is {code}.',
'vars' => ['code' => '123456']
]);
Error Handling Wrap API calls in a try-catch:
try {
$response = $client->send([...]);
} catch (\Websms\Exception\WebsmsException $e) {
Log::error("SMS failed: " . $e->getMessage());
// Retry logic or notify admin
}
Laravel Service Provider Integration
Bind the client to the container in AppServiceProvider:
public function register()
{
$this->app->singleton('websms.client', function ($app) {
return new Client([
'username' => config('services.websms.username'),
'password' => config('services.websms.password'),
'sender' => config('services.websms.sender')
]);
});
}
Configure in config/services.php:
'websms' => [
'username' => env('WEBSMS_USERNAME'),
'password' => env('WEBSMS_PASSWORD'),
'sender' => env('WEBSMS_SENDER_ID')
],
Queueing SMS Jobs
Create a job (SendSmsJob):
use Illuminate\Bus\Queueable;
use Websms\Client;
class SendSmsJob implements ShouldQueue
{
use Queueable;
public function handle(Client $client)
{
$client->send([...]);
}
}
Deprecated Package
No Rate Limiting
$attempts = 0;
$maxAttempts = 3;
while ($attempts < $maxAttempts) {
try {
$response = $client->send([...]);
break;
} catch (\Websms\Exception\WebsmsException $e) {
$attempts++;
sleep(2 ** $attempts); // Exponential backoff
}
}
No Async Support
Limited Documentation
getBalance() or listMessages() are available.Sender ID Restrictions
'YourBrand').Enable Debug Mode
$client = new Client([...], [
'debug' => true // Logs raw API requests/responses
]);
Check HTTP Status Codes
Inspect $response->getStatusCode() for non-200 responses (e.g., 401 Unauthorized).
Validate Credentials Test with a dummy request:
$client->getBalance(); // Throws exception if credentials are invalid
Custom Responses
Extend the Response class to add helper methods:
class ExtendedResponse extends \Websms\Response
{
public function isDelivered()
{
return $this->getStatus() === 'delivered';
}
}
Add Retry Logic
Create a decorator for the Client class:
class RetryClientDecorator
{
public function send(array $params, int $retries = 3)
{
// Implement retry logic
}
}
Support for Webhooks
If WebSms supports webhooks, add a setWebhookUrl() method to the client:
$client->setWebhookUrl(config('services.websms.webhook_url'));
Mocking for Tests
Use Laravel’s Mockery to stub the client:
$mockClient = Mockery::mock(\Websms\Client::class);
$mockClient->shouldReceive('send')->andReturn(new \Websms\Response(200, ['id' => '123']));
How can I help you explore Laravel packages today?