signalads/php
PHP client for the SignalAds REST API to send SMS messages. Supports single and bulk sends, pattern-based SMS, and structured error handling via ApiException/HttpException. Install with Composer and authenticate using your API key from the SignalAds panel.
Install via Composer:
composer require signalads-co/php
Ensure vendor/autoload.php is included in your project (Laravel handles this automatically via composer.json).
Retrieve API Key: Fetch your key from the SignalAds Panel.
First Use Case: Send a single SMS in a Laravel controller:
use SignalAds\SignalAdsApi;
public function sendSms()
{
$api = new SignalAdsApi(config('services.signalads.key'));
$response = $api->Send(
config('services.signalads.sender_id'),
'09123456789',
'Hello from Laravel!'
);
return response()->json($response);
}
Add to config/services.php:
'signalads' => [
'key' => env('SIGNALADS_API_KEY'),
'sender_id' => env('SIGNALADS_SENDER_ID'),
],
Single SMS:
$api->Send($senderId, $recipient, $message);
Bulk SMS:
$api->SendGroup($senderId, ['09123456789', '09123456788'], $message);
Templated SMS:
$api->SendPattern($senderId, '12345', ['param1', 'param2'], $recipients);
pattern_id in a config file for reusability.Status Checks:
$api->Status($messageId, 10, 0, 4); // Get last 10 delivered messages
Service Provider: Bind the API client for dependency injection:
// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->singleton(SignalAdsApi::class, function ($app) {
return new SignalAdsApi(config('services.signalads.key'));
});
}
Queued Jobs: Dispatch SMS sends asynchronously:
use Illuminate\Support\Facades\Bus;
Bus::dispatch(new SendSmsJob($recipient, $message));
// app/Jobs/SendSmsJob.php
public function handle()
{
$api = app(SignalAdsApi::class);
$api->Send(config('services.signalads.sender_id'), $this->recipient, $this->message);
}
API Responses: Normalize responses for consistency:
$response = $api->Send(...);
if ($response['error']['message']) {
Log::error("SMS failed: " . $response['error']['message']);
return back()->with('error', 'Failed to send SMS');
}
API Key Security:
.env and config/services.php.try {
$api->Send(...);
} catch (ApiException $e) {
if ($e->getCode() === 429) { // Rate limited
sleep(2);
retry();
}
}
Recipient Validation:
if (!preg_match('/^09[0-9]{9}$/', $phone)) {
throw new \InvalidArgumentException('Invalid phone number');
}
Pattern IDs:
Status Codes:
PENDING (1) may persist for minutes. Avoid polling too frequently (e.g., use Laravel’s schedule for delayed checks).HTTP Exceptions:
$api = new SignalAdsApi($key, [
'debug' => true,
'handler' => new \GuzzleHttp\HandlerStack(),
]);
Logging:
try {
$response = $api->Send(...);
Log::info('SMS sent', ['response' => $response]);
} catch (Exception $e) {
Log::error('SMS failed', ['error' => $e->getMessage()]);
}
Custom Responses:
SignalAdsApi class to add methods for unsupported endpoints (e.g., GetBalance):
class ExtendedSignalAdsApi extends SignalAdsApi {
public function GetBalance() {
return $this->request('GET', '/balance');
}
}
Webhook Integration:
Status endpoint to build a Laravel route for real-time updates:
Route::post('/sms/webhook', function (Request $request) {
$api = new SignalAdsApi(config('services.signalads.key'));
$status = $api->Status($request->message_id);
// Process status updates (e.g., trigger events)
});
Testing:
$mockHandler = new \GuzzleHttp\Handler\MockHandler([
new \GuzzleHttp\Psr7\Response(200, [], json_encode(['data' => ['message_id' => 'test']]))
]);
$api = new SignalAdsApi($key, ['handler' => new \GuzzleHttp\HandlerStack($mockHandler)]);
How can I help you explore Laravel packages today?