Send SMS messages through the LabsMobile platform and the PHP library.
- Labsmobile API documentation can be found here.
- Send SMS messages.
- Get account credits
- Get prices by country
- Manage scheduled sendings
- HLR Request (Check mobile)
- A user account with LabsMobile. Click on the link to create an account here.
- PHP 8.2 through 8.5 (
>=8.2 <8.6) and Composer 2. - Guzzle 7 (installed automatically by Composer).
- CI targets PHP 8.2.35, 8.3.36, 8.4.27 and 8.5.12.
To install the labsmobile/sms-php library, it is recommended to use composer.
composer require labsmobile/sms-php:^2.0
"require": {
"labsmobile/sms-php": "^2.0"
}
Version 2.0.0 requires PHP 8.2 or later (up to PHP 8.5) and Guzzle 7.
Upgrade PHP and resolve any dependencies that require Guzzle 5 or 6 before upgrading.
Existing method names and two-argument client construction remain available.
Client operations now declare ResponseInterface return types; subclasses overriding
these methods must declare compatible return types.
Malformed JSON, scalar JSON, missing required API response codes and HTTP error responses
now raise RestException. Invalid JSON retains its JsonException as the previous exception.
Network failures continue to propagate as Guzzle exceptions. getStatus() returns the API
error code when available, or the HTTP status for HTTP/protocol errors; HLR uses HTTP status.
An optional third constructor argument accepts a Guzzle ClientInterface for custom
configuration or testing:
$http = new \GuzzleHttp\Client(['timeout' => 15]);
$client = new \Labsmobile\SmsPhp\LabsMobileClient('username', 'token', $http);composer update
composer testTests use simulated HTTP responses and do not need credentials or send SMS. PHPUnit 11.5 is used on PHP 8.2; Composer can select PHPUnit 12.5 on newer PHP versions. Resolve dependencies separately for each PHP runtime; a lock file generated on PHP 8.5 may contain development dependencies that cannot run on PHP 8.2.
To run the same checks in Docker (replace the build argument to test another version):
docker build --build-arg PHP_VERSION=8.5.12 -t sms-php-test .
docker run --rm sms-php-testSend SMS
Here is an example of using the library to send a SMS:
public $username = 'myusername';
public $token = 'mytoken';
public function testSms()
{
try {
$message = 'Test SMS';
$phone = ['34XXXXXXXXX'];
$labsMobileClient = new LabsMobileClient($this->username, $this->token);
$bodySms = new LabsMobileModelTextMessage($phone,$message);
$labsMobileClient = $labsMobileClient->sendSms($bodySms);
$body = $labsMobileClient->getBody();
$json = json_decode((string) $body);
self::assertSame('0',$json->code);
} catch (RestException $exception) {
self::assertSame('Error', $exception->getStatus() ." ". $exception->getMessage());
}
}Get account credits
Here is an example to learn credits for an existing account:
public $username = 'myusername';
public $token = 'mytoken';
public function testGetCredits()
{
try{
$labsMobileClient = new LabsMobileClient($this->username, $this->token);
$response = $labsMobileClient->getCredit();
$body = $response->getBody();
$json = json_decode((string) $body);
self::assertSame(0,$json->code);
} catch(RestException $exception) {
self::assertSame('Error', $exception->getStatus() ." ". $exception->getMessage());
}
}Manage scheduled sendings
Here is an example you can cancel or execute the scheduled sendings that are pending for execution:
public $username = 'myusername';
public $token = 'mytoken';
public function testScheduledSendings()
{
try {
$subid="XXXXXXXXXX";
$cmd="XXXX";
$labsMobileClient = new LabsMobileClient($this->username, $this->token);
$bodyScheduled = new LabsMobileModelScheduledSendings($subid, $cmd);
$labsMobileClient = $labsMobileClient->scheduledSendings($bodyScheduled);
$body = $labsMobileClient->getBody();
$json = json_decode((string) $body);
self::assertSame(0,$json->code);
} catch (RestException $exception) {
self::assertSame('Error', $exception->getStatus() ." ". $exception->getMessage());
}
}Get prices by country
Here is an example to know the credits that a single sending will take depending on the country of delivery:
public $username = 'myusername';
public $token = 'mytoken';
public function testCountryPrice()
{
try {
$countries = ["CO","ES"];
$labsMobileClient = new LabsMobileClient($this->username, $this->token);
$bodyContries = new LabsMobileModelCountryPrice($countries);
$labsMobileClient = $labsMobileClient->getpricesCountry($bodyContries);
$body = $labsMobileClient->getBody();
self::assertNotEmpty((string) $body);
} catch (RestException $exception) {
self::assertSame('Error', $exception->getStatus() ." ". $exception->getMessage());
}
}HLR Request
Here is an example queries the mobile phone status with the related information like current operator, format, active, ported information, subscription country, etc:
public $username = 'myusername';
public $token = 'mytoken';
public function testHlr()
{
try {
$numbers = [];//[34XXXXXXXX,34XXXXXXXX]
$labsMobileClient = new LabsMobileClient($this->username, $this->token);
$bodyHlr = new LabsMobileModelHlrRequest(json_encode($numbers));
$labsMobileClient = $labsMobileClient->hlrRequest($bodyHlr);
$body = $labsMobileClient->getBody();
$json = json_decode((string) $body);
self::assertSame('ok', $json->result);
} catch (RestException $exception) {
self::assertSame('Error', $exception->getStatus() ." ". $exception->getMessage());
}
}If you have questions, you can contact us through the support chat or through the support email support@labsmobile.com.