OVH SMS API
To edit a SMS on-line while computing the size of the message and the number of SMS sent, see the page Editing a SMS.
Create an account at OVH.
Order 20 SMS for free on the page SMS Pro from OVH.
In your personal space at OVH, click on the tab Telecom.
In the menu, click on SMS. The name of the service is displayed, e.g. sms-ce10000-1
.
Click on the name of the service. Spend some time in the interface.
The page Envoyer des SMS avec l'API OVH en PHP gives all the explanations to start with the interface. All the functions of the SDK are described on the page OVH API.
Create your identifiers on the page https://api.ovh.com/createToken. Fill in the form and press Create keys. You obtain the application key (AK), the application secret key (AS) and the authorization token (CK), the configuration parameters of the API.
Create the file ovh.inc in the folder includes with the following content:
- global $ovh_endpoint;
- $ovh_endpoint='ovh-eu';
- global $ovh_application_key, $ovh_application_secret, $ovh_consummer_key;
- $ovh_application_key=false;
- $ovh_application_secret=false;
- $ovh_consummer_key=false;
- global $ovh_service_name;
- $ovh_service_name=false;
Assign the application key (AK) to the parameter $ovh_application_key
, the application secret key (AS) to the parameter $ovh_application_secret
and the authorization token (CK) to the parameter $ovh_consummer_key
.
Assign the name of the service to the parameter $ovh_service_name
.
Add the code of the OVH API at the root of the site with Composer:
$ composer require ovh/ovh
Create the file tests/testovhsms.php with the folowing content:
- define('ROOT_DIR', dirname(__FILE__));
- set_include_path(get_include_path() . PATH_SEPARATOR . ROOT_DIR . DIRECTORY_SEPARATOR . 'library');
- set_include_path(get_include_path() . PATH_SEPARATOR . ROOT_DIR . DIRECTORY_SEPARATOR . 'includes');
- define('USAGE', 'php -f %s name | info | send telnum msgtext | pending | cancel smsid');
- function abort($msg, $code=1) {
- echo $msg, PHP_EOL;
- exit($code);
- }
- function usage() {
- global $argv;
- abort(sprintf(USAGE, basename($argv[0])), 1);
- }
- require_once 'ovh.inc';
- if (! ($ovh_application_key and $ovh_application_secret and $ovh_consummer_key)) {
- abort('ovh.inc?');
- }
- require_once 'vendor/autoload.php';
- use \Ovh\Api;
- $ovh = new Api($ovh_application_key, $ovh_application_secret, $ovh_endpoint, $ovh_consummer_key);
- if (!($argc >= 2)) {
- usage();
- }
- $telnum=$msgtext=$smsid=false;
- $action=$argv[1];
- switch ($action) {
- case 'name';
- break;
- case 'info';
- break;
- case 'send';
- if (!($argc == 4)) {
- usage();
- }
- $telnum=$argv[2];
- $msgtext=$argv[3];
- break;
- case 'pending';
- break;
- case 'cancel';
- if (!($argc == 3)) {
- usage();
- }
- $smsid=$argv[2];
- break;
- default:
- usage();
- }
- switch ($action) {
- case 'name';
- try {
- $r = $ovh->get('/sms');
- echo $r[0], PHP_EOL;
- }
- catch (Exception $e) {
- die($e->getMessage());
- }
- break;
- case 'info';
- try {
- $r = $ovh->get('/sms/' . $ovh_service_name);
- print_r($r);
- }
- catch (Exception $e) {
- die($e->getMessage());
- }
- break;
- case 'send';
- $args = array(
- 'charset' => 'UTF-8',
- 'class' => 'phoneDisplay',
- 'coding' => '7bit',
- 'noStopClause' => false,
- 'priority' => 'high',
- // 'sender' => 'iZend',
- 'senderForResponse' => true,
- 'validityPeriod' => '1440',
- 'differedPeriod' => '2880',
- 'message' => $msgtext,
- 'receivers' => array($telnum),
- );
- try {
- $r = $ovh->post('/sms/' . $ovh_service_name . '/jobs', $args);
- print_r($r);
- }
- catch (Exception $e) {
- die($e->getMessage());
- }
- break;
- case 'pending';
- try {
- $r = $ovh->get('/sms/' . $ovh_service_name . '/jobs');
- print_r($r);
- }
- catch (Exception $e) {
- die($e->getMessage());
- }
- break;
- case 'cancel';
- try {
- $r = $ovh->delete('/sms/' . $ovh_service_name . '/jobs/' . $smsid);
- }
- catch (Exception $e) {
- die($e->getMessage());
- }
- break;
- default:
- break;
- }
Create a link to the file tests/testovhsms.php in the root folder of the site:
$ ln tests/testovhsms.php testovhsms.php
In the root folder of the site, try the script:
$ php -f testovhsms.php
php -f testovhsms.php name | info | send telnum msgtext | pending | cancel smsid
The program displays its usage.
Verify the name of the service configured by OVH:
$ php -f testovhsms.php name
sms-ce10000-1
Compare the returned value with the value of the configuration parameter $ovh_service_name
defined in the file ovh.inc.
IMPORTANT: The other commands of the script testovhsms.php use the parameter $ovh_service_name
.
Display the options of the service:
$ php -f testovhsms.php info
Array
(
...
[status] => enable
[creditsLeft] => 20
[name] => sms-ce10000-1
...
)
Send a SMS:
$ php -f testovhsms.php send +3360000000 'iZend - The web engine - http://www.izend.org'
Array
(
[totalCreditsRemoved] => 1
[invalidReceivers] => Array
(
)
[ids] => Array
(
[0] => 12345678
)
[validReceivers] => Array
(
[0] => +3360000000
)
)
Note the number returned by the field [ids]
.
IMPORTANT: The script testovhsms.php doesn't request that the SMS is sent immediately but programs a delay of 24 hours. By deleting the request in time, you can test the API without consuming your credit. If you wish to check the reception of the SMS, comment out the parameter validityPeriod
in the code.
Check in your client space at OVH that sending the SMS is scheduled.
Display the delayed tasks:
$ php -f testovhsms.php pending
Array
(
[0] => 12345678
)
Cancel sending the SMS:
$ php -f testovhsms.php cancel 12345678
Check in your client space at Ovh that you have recovered your SMS credit.
Git
Delete the link on the file tests/testovhsms.php in the root folder of the site:
$ rm testovhsms.php
Edit the file .gitignore at the root of the site and add the following line:
vendor
The folder vendor isn't included in the deposit.
- /izendsms.com
- .gitignore
- composer.json
- composer.lock
- includes
- ovh.inc
- tests
- testovhsms.php
Commit this version:
$ git status
$ git add .gitignore composer.json composer.lock includes/ovh.inc tests/testovhsms.php
$ git commit -m'Adds the SMS API by OVH'
Comments