Send and receive SMS using SmsDev.com.br
Require this package with Composer in the root directory of your project.
composer require enricodias/smsdev
Create a new instance with your API key:
$SmsDev = new \enricodias\SmsDev('API_KEY');
If the API key is not provided, the library will look for the environment variable
SMSDEV_API_KEY
Set any date format to be used in all date methods:
$SmsDev->setDateFormat('Y-m-d H:i:s'); // default is 'U', timestamp
$SmsDev->send(5511988881111, 'SMS Message'); // returns true if the API accepts the message
var_dump($SmsDev->getResult()); // Returns the raw API response.
The country code optional. The default is 55 (Brazil).
If you have the package giggsey/libphonenumber-for-php installed, it will be used to validate numbers locally. You can disable this feature with the method setNumberValidation
before sending:
$SmsDev->setNumberValidation(false); // disables phone number validation
Note that SmsDev will charge you for messages sent to invalid numbers.
Get unread messages in a specific date interval:
$SmsDev->setDateFormat('Y-m-d');
$SmsDev->setFilter()
->isUnread()
->dateBetween('2018-01-19', '2019-01-19')
->fetch();
Search for a specific message id:
$SmsDev->setFilter()
->byId(2515974)
->fetch();
After fetching the messages you can either access the raw API response using getResult()
or use the function parsedMessages()
to get a simplified array:
$SmsDev->setDateFormat('U'); // timestamp
$messages = $SmsDev->parsedMessages();
var_dump($messages);
/*
array(1) {
['date'] => '1529418914'
['number'] => '5511988887777'
['message'] => 'Message'
}
*/
Dates are converted to the format specified in setDateFormat()
.
The following filters are equivalent:
$SmsDev->setFilter()
->dateBetween('2018-01-19', '2019-01-19')
->fetch();
$SmsDev->setFilter()
->dateBetween('2018-01-19', '')
->dateTo('2019-01-19')
->fetch();
$SmsDev->setFilter()
->dateBetween('', '2019-01-19')
->dateFrom('2018-01-19')
->fetch();
$SmsDev->setFilter()
->dateFrom('2018-01-19')
->dateTo('2019-01-19')
->fetch();
The API uses the timezone America/Sao_Paulo. Using another timezone in your application will force you to convert dates locally in order to get correct values.
Ex: if you are using UTC-4 and receive a new message, it will look like the message came from the future because America/Sao_Paulo is UTC-3.
This class solves this problem by automatically correcting dates both in search filters and in parsed messages. Only the dates in raw API responses are not converted.
- Check the status of sent messages.
- Send multiple SMS messages.