php

As an outcome of this guide, you will send a simple text message to your handset using Infobip SMS API and PHP.


Prerequisites

Infobip account

• PHP, version 7.2 or greater

Difficulty level

This guide assumes very basic knowledge of PHP and basic familiarity with APIs.

Summary of the steps

• Add Infobip API PHP Client to your project.
• Set up an InfobipConfiguration class and add authentication details.
• Create a SendSMSAPI class and provide details needed to send an SMS.

Add Infobip API PHP Client to your project

To use the Infobip API PHP Client in your PHP project, you’ll need to add it to your project composer.json file, either directly or with this convenient command-line shortcut.

composer require infobip/infobip-api-php-client

Create an InfobipConfiguration instance

You’ll use the instance to input your credentials and authenticate Infobip SMS API. Before running the code, don’t forget to replace the placeholders we set in the example below with your account values. You can find your API Key and API Base URL in your Infobip account or on the Infobip API landing page, once logged in.

<?php

const API_KEY_PREFIX = 'App';
const API_KEY = '<your-api-key>';
const URL_BASE_PATH = '<your-base-URL>';

$configuration = (new InfobipConfiguration())
    ->setHost(URL_BASE_PATH)
    ->setApiKeyPrefix('Authorization', API_KEY_PREFIX)
    ->setApiKey('Authorization', API_KEY);

$client = new GuzzleHttpClient();

Now that we’ve got the configuration done, the next step is sending a text message!

Send an SMS

To send a single SMS, we can configure the SendSMSAPI class to provide the required information, such as the destination and the content of the SMS message.

Key points:

• The destination address in the to field must be in the international format, e.g. 447415774332.
• For testing purposes, use the Infobip test sender InfoSMS.
• If using a free trial account, the recipient’s phone number must be the same phone number you’ve registered on signup.

use InfobipAPI{ SendSMSApi, SmsDestination, SmsTextualMessage, SmsAdvancedTextualRequest};

$sendSmsApi = new SendSMSApi($client, $configuration);
$destination = (new SmsDestination())->setTo('4412345678910');
$message = (new SmsTextualMessage())
    ->setFrom('InfoSMS')
    ->setText('This is a dummy SMS message sent using infobip-api-php-client')
    ->setDestinations([$destination]);
$request = (new SmsAdvancedTextualRequest())
    ->setMessages([$message]);

Once you run the code, you should get an SMS on your handset and see the following 200 OK response. You can check if the message was a success by heading over to your Infobip account and accessing Analyze > Logs.

{
  "bulkId": "2034072219640523072",
  "messages": [
    {
      "messageId": "41793026727",
      "status": {
        "description": "Message sent to next instance",
        "groupId": 1,
        "groupName": "PENDING",
        "id": 26,
        "name": "MESSAGE_ACCEPTED"
      },
      "to": "2250be2d4219-3af1-78856-aabe-1362af1edfd2"
    }
  ]
}

For troubleshooting and analytics, Use the auto-generated bulkId to fetch the entire bulk of messages, or the messageId to fetch one specific message.

BONUS: Keeping secrets safe

This example code had the API key written in the PHP source code, but that can lead to tokens being leaked to anyone who has access to Git. Consider using something like dotenv to keep your secret credentials out of Git.

Make a new file called .env in the root of your project with the following content:

API_KEY_PREFIX='App'
API_KEY='<your-api-key>'
URL_BASE_PATH='<your-base-url>'

Replace the < and > fully with the values you’ve been using to send SMS in PHP.

Some PHP frameworks like Laravel will use dotenv by default, but if you need to set this up in another environment, install dotenv with Composer.

composer require vlucas/phpdotenv

Then in your PHP code, load up .env in a bootstrap file somewhere and switch from using the constants we hardcoded to using the $_ENV super global variable.

$dotenv = DotenvDotenv::createImmutable(__DIR__);
$dotenv->load();

$configuration = (new InfobipConfiguration())
    ->setHost($_ENV['URL_BASE_PATH'])
    ->setApiKeyPrefix('Authorization', $_ENV['API_KEY_PREFIX'])
    ->setApiKey('Authorization', $_ENV['API_KEY']);

If you now put .env into .gitignore, you can avoid those credentials being committed to git, and potentially getting into the wrong hands.

echo '.env' >> .gitignore