Installation:
composer require chaboksms/php:1.0.5
Or add to composer.json:
"require": {
"chaboksms/php": "1.0.5"
}
Run composer update.
First Use Case: Initialize the API with your credentials and send an SMS:
use Chaboksms\ChaboksmsApi;
$api = new ChaboksmsApi('your_username', 'your_password');
$sms = $api->sms();
$response = $sms->send('09123456789', '5000123', 'Test message');
$json = json_decode($response);
echo $json->Value; // Outputs RecId or Error Number
Where to Look First:
ChaboksmsApi class methods for available services (sms, ticket, branch, users, contacts).SMS Sending:
sms()->send($to, $from, $text) for REST or SOAP.sms('soap')->send2($to, $from, $text, $isFlash, $udh) for UDH support.sms('soap')->sendSchedule($to, $from, $text, $isFlash, $scheduleDateTime, $period).Bulk SMS:
$branch = $api->branch();
$branch->add('MyBranch', 'owner_username');
$branch->addNumber(['09123456789', '09123456788'], 1);
$branch->sendBulk('5000123', 'Title', 'Message', 1, null, 1, 'normal', 0, 0, 0);
Asynchronous Operations:
$smsAsync = $api->sms('async');
$ticketAsync = $api->ticket('async');
$smsAsync->send(...)->execute();
$ticketAsync->add(...)->execute();
User Management:
$users = $api->users();
$users->add(['username' => 'new_user', 'password' => 'password123']);
$users->changeCredit(1000, 'Top-up', 'new_user', true);
Contact Management:
$contacts = $api->contacts();
$contacts->add(['mobileNumber' => '09123456789', 'name' => 'John Doe']);
$contacts->get(1, 'keyword', 0, 10);
try-catch blocks to handle exceptions gracefully.
try {
$response = $sms->send(...);
} catch (Exception $e) {
Log::error('SMS Error: ' . $e->getMessage());
return response()->json(['error' => 'Failed to send SMS'], 500);
}
$sms->getCredit() to avoid overages.Log::info('SMS Response', ['response' => $json]);
.env file and use the config helper or a service provider to inject them:
$api = new ChaboksmsApi(config('services.chaboksms.username'), config('services.chaboksms.password'));
Authentication:
SOAP vs REST:
$smsSoap->send(['09123456789', '09123456788'], ...)), while REST methods do not.Asynchronous Operations:
->execute(). Forgetting this will not trigger the API call.Character Encoding:
Credit Management:
$sms->getCredit()) before bulk operations to avoid failed sends.sendWithSpeech) consume more credit than standard SMS.Number Formats:
0 prefix (e.g., 09123456789), not +989123456789.Rate Limits:
Enable SOAP Debugging:
ChaboksmsApi class or use a SOAP client library like php-soap with debug options:
$client = new SoapClient($wsdl, ['trace' => 1]);
$client->__getLastRequest() and $client->__getLastResponse() for raw SOAP traffic.Log Raw Responses:
Log::debug('Raw Response', ['response' => $response]);
Validate Inputs:
Test in Sandbox:
Custom Request/Response Handling:
ChaboksmsApi class to add middleware for logging, retries, or request transformations:
class CustomChaboksmsApi extends ChaboksmsApi {
public function send($to, $from, $text) {
$text = $this->sanitizeText($text); // Custom logic
return parent::send($to, $from, $text);
}
}
Queue Async Operations:
dispatch(new SendSmsJob($to, $from, $text))->onQueue('sms');
Webhook Integration:
$sms->isDelivered($recId)) or use Chaboksms webhooks (if supported) to receive real-time updates.Caching:
$credit = Cache::remember('chaboksms_credit', 300, function() {
return $sms->getCredit();
});
Fallback Mechanisms:
Time Zones:
sendSchedule) use the server's time zone. Ensure your server's time zone matches expectations (e.g., config(['app.timezone' => 'Asia/Tehran'])).Base Numbers:
sendByBaseNumber, ensure the bodyId (shared service body) is correctly created via $smsSoap->sharedServiceBodyAdd().Async Execution Order:
recId) to track individual requests.SOAP WSDL:
$api = new ChaboksmsApi($username, $password, 'https://new-wsdl-url.com');
How can I help you explore Laravel packages today?