2
247

OVHcloud SMS API

Learn how to use the OVHcloud SMS API with PHP to send SMS messages.

IMPORTANT: This tutorial explains how to use the OVHcloud SMS API to send SMS messages in France. It uses services and features available in France that may not be available in other countries. OVHcloud SMS offers are currently (2026) available in France, the United Kingdom, Ireland, Spain, Italy, and Poland. Available services, pricing, API configuration, and sender settings may vary depending on the country associated with your OVHcloud account.

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.

98 / 160  -  1 SMS 7bit

 OVHcloud account → SMS account → API credentials → ovh.inc → sending an SMS in PHP

Create an account with OVHcloud.

Go to the OVHcloud SMS Pro page. Create an SMS account. NOTE: New customers in France can benefit from 20 free SMS credits to test the service.

From the dashboard of your OVHcloud customer account, click Telecom and then SMS. The SMS account name is displayed, e.g. sms-ce10000-1.

Sending SMS messages with the OVHcloud API in PHP

Install the OVHcloud API code at the root of the site with Composer:

$ cd /var/www/izendsms.com
$ composer require ovh/ovh

The command created the folder vendor and the files composer.json and composer.lock.

{
    "require": {
        "ovh/ovh": "^3.5"
    }
}

Edit the file .gitignore at the root of the site. Add composer.lock at the end of the file. NOTE: The folder vendor is already in the list of the files ignored by Git. If not, add it.

Git
  1. /izendsms.com
    1. .gitignore
    2. composer.json

Commit this version

$ git status
$ git add composer.json .gitignore
$ git commit -m'Requires ovh/ovh in composer.json'

Using the API requires 5 parameters:

$ovh_endpoint API region: ovh-eu
$ovh_application_key Identifies the application
$ovh_application_secret Authenticates the application
$ovh_consumer_key Authorizes the application to access the API
$ovh_service_name Name of the SMS account associated with the OVHcloud account

The parameters $ovh_application_key, $ovh_application_secret and $ovh_consumer_key are provided when creating the API credentials. The parameter $ovh_endpoint is set to ovh-eu for the OVHcloud API in Europe. The parameter $ovh_service_name identifies the SMS account associated with the OVHcloud account for which the API credentials were created.

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_consumer_key=false;
  10.  
  11. global $ovh_service_name;
  12.  
  13. $ovh_service_name=false;

Go to the page Create API Keys to create the credentials associated to the OVhcloud account with an active SMS account.

Authorize the following requests:

GET /sms/
GET /sms/*/jobs
POST /sms/*/jobs
GET /sms/*
DELETE /sms/*/jobs/*

Press Create.

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 SMS account to the parameter $ovh_service_name.

API Explorer

Create the file tests/testovhsms.php with the following 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_consumer_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_consumer_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.             print_r($r);
  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.             'senderForResponse' => true,
  88.             'message'           => $msgtext,
  89.             'receivers'         => array($telnum),
  90.             'validityPeriod'    => '1440',
  91.             'differedPeriod'    => '2880',
  92.         );
  93.  
  94.         try {
  95.             $r = $ovh->post('/sms/' . $ovh_service_name . '/jobs', $args);
  96.  
  97.             print_r($r);
  98.         }
  99.         catch (Exception $e) {
  100.             die($e->getMessage());
  101.         }
  102.         break;
  103.  
  104.     case 'pending';
  105.         try {
  106.             $r = $ovh->get('/sms/' . $ovh_service_name . '/jobs');
  107.  
  108.             print_r($r);
  109.         }
  110.         catch (Exception $e) {
  111.             die($e->getMessage());
  112.         }
  113.         break;
  114.  
  115.     case 'cancel';
  116.         try {
  117.             $r = $ovh->delete('/sms/' . $ovh_service_name . '/jobs/' . $smsid);
  118.         }
  119.         catch (Exception $e) {
  120.             die($e->getMessage());
  121.         }
  122.         break;
  123.  
  124.     default:
  125.         break;
  126. }

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 SMS account

$ php -f testovhsms.php name
Array
(
    [0] => sms-ce10000-1
)

Verify that the $ovh_service_name configuration parameter defined in the ovh.inc file refers to an SMS account returned in the list. 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
...
)

In the case of an access error, the program displays an HTTP 403 error:

Client error: `GET https://eu.api.ovh.com/1.0/sms/sms-ce10000-1/` resulted in a `403 Forbidden` response {"class":"Client::Forbidden","message":"This call has not been granted","httpCode":"403 Forbidden","errorCode":"NOT_GRAN (truncated...)
The credentials do not give access to a GET '/sms/*' request. Recreate the credentials with all the necessary access rights.

Send a SMS using the telephone number associated to the SMS account:

$ php -f testovhsms.php send +3360000000 'iZend - The web engine - http://www.izend.org'
Array
(
    [tag] => rzz3lb0jg98ekk2a
    [validReceivers] => Array
        (
            [0] => +33600000000
        )

    [creditsLeft] => 19
    [invalidReceivers] => Array
        (
        )

    [totalCreditsRemoved] => 1
    [ids] => Array
        (
            [0] => 12345678
        )
)

Note the number returned by the field [ids], e.g. 12345678..

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 OVHcloud 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 OVHcloud 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
  1. /izendsms.com
    1. includes
      1. ovh.inc
    2. tests
      1. testovhsms.php

Commit this version:

$ git status
$ git add includes/ovh.inc tests/testovhsms.php
$ git commit -m'Adds the SMS API by OVHcloud'

Editing a SMS

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].