2
167

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:

  1. global $ovh_endpoint;
  2.  
  3. $ovh_endpoint='ovh-eu';
  4.  
  5. global $ovh_application_key, $ovh_application_secret, $ovh_consummer_key;
  6.  
  7. $ovh_application_key=false;
  8. $ovh_application_secret=false;
  9. $ovh_consummer_key=false;
  10.  
  11. global $ovh_service_name;
  12.  
  13. $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:

  1. define('ROOT_DIR', dirname(__FILE__));
  2.  
  3. set_include_path(get_include_path() . PATH_SEPARATOR . ROOT_DIR . DIRECTORY_SEPARATOR . 'library');
  4. set_include_path(get_include_path() . PATH_SEPARATOR . ROOT_DIR . DIRECTORY_SEPARATOR . 'includes');
  5.  
  6. define('USAGE', 'php -f %s name | info | send telnum msgtext | pending | cancel smsid');
  7.  
  8. function abort($msg, $code=1) {
  9.     echo $msg, PHP_EOL;
  10.     exit($code);
  11. }
  12.  
  13. function usage() {
  14.     global $argv;
  15.  
  16.     abort(sprintf(USAGE, basename($argv[0])), 1);
  17. }
  18.  
  19. require_once 'ovh.inc';
  20.  
  21. if (! ($ovh_application_key and $ovh_application_secret and $ovh_consummer_key)) {
  22.     abort('ovh.inc?');
  23. }
  24.  
  25. require_once 'vendor/autoload.php';
  26. use \Ovh\Api;
  27.  
  28. $ovh = new Api($ovh_application_key, $ovh_application_secret, $ovh_endpoint, $ovh_consummer_key);
  29.  
  30. if (!($argc >= 2)) {
  31.     usage();
  32. }
  33.  
  34. $telnum=$msgtext=$smsid=false;
  35.  
  36. $action=$argv[1];
  37.  
  38. switch ($action) {
  39.     case 'name';
  40.         break;
  41.     case 'info';
  42.         break;
  43.     case 'send';
  44.         if (!($argc == 4)) {
  45.             usage();
  46.         }
  47.         $telnum=$argv[2];
  48.         $msgtext=$argv[3];
  49.         break;
  50.     case 'pending';
  51.         break;
  52.     case 'cancel';
  53.         if (!($argc == 3)) {
  54.             usage();
  55.         }
  56.         $smsid=$argv[2];
  57.         break;
  58.     default:
  59.         usage();
  60. }
  61.  
  62. switch ($action) {
  63.     case 'name';
  64.         try {
  65.             $r = $ovh->get('/sms');
  66.  
  67.             echo $r[0], PHP_EOL;
  68.         }
  69.         catch (Exception $e) {
  70.             die($e->getMessage());
  71.         }
  72.         break;
  73.  
  74.     case 'info';
  75.         try {
  76.             $r = $ovh->get('/sms/' . $ovh_service_name);
  77.  
  78.             print_r($r);
  79.         }
  80.         catch (Exception $e) {
  81.             die($e->getMessage());
  82.         }
  83.         break;
  84.  
  85.     case 'send';
  86.         $args = array(
  87.             'charset'           => 'UTF-8',
  88.             'class'             => 'phoneDisplay',
  89.             'coding'            => '7bit',
  90.             'noStopClause'      => false,
  91.             'priority'          => 'high',
  92. //          'sender'            => 'iZend',
  93.             'senderForResponse' => true,
  94.             'validityPeriod'    => '1440',
  95.             'differedPeriod'    => '2880',
  96.             'message'           => $msgtext,
  97.             'receivers'         => array($telnum),
  98.         );
  99.  
  100.         try {
  101.             $r = $ovh->post('/sms/' . $ovh_service_name . '/jobs', $args);
  102.  
  103.             print_r($r);
  104.         }
  105.         catch (Exception $e) {
  106.             die($e->getMessage());
  107.         }
  108.         break;
  109.  
  110.     case 'pending';
  111.         try {
  112.             $r = $ovh->get('/sms/' . $ovh_service_name . '/jobs');
  113.  
  114.             print_r($r);
  115.         }
  116.         catch (Exception $e) {
  117.             die($e->getMessage());
  118.         }
  119.         break;
  120.  
  121.     case 'cancel';
  122.         try {
  123.             $r = $ovh->delete('/sms/' . $ovh_service_name . '/jobs/' . $smsid);
  124.         }
  125.         catch (Exception $e) {
  126.             die($e->getMessage());
  127.         }
  128.         break;
  129.  
  130.     default:
  131.         break;
  132. }

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.

OVH API

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.

  1. /izendsms.com
    1. .gitignore
    2. composer.json
    3. composer.lock
    4. includes
      1. ovh.inc
    5. tests
      1. 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

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].