SMS - Tutorial: Send an SMS in PHP using the Telesign SDK

This tutorial teaches you how to use the Telesign PHP SDK to send an SMS. The complete sample code for this tutorial is available on GitHub here.

Before you begin

Make sure you have the following before you start:

  • Authentication credentials: Your Customer ID and API Key. If you need help finding these items, go to the support article How do I find my Customer ID and API Key.
  • Testing device: A mobile phone on which you can receive SMS.
  • Composer - This package manager isn't required to use this SDK, but it is required to use this tutorial.

📘

NOTE:

This tutorial uses the following:

  • MacOS
  • PHP v8.2.4
  • Composer v2.4.4

Please modify accordingly if your developer environment differs from these details.

Set up your project

  1. Follow the Telesign Self-service PHP SDK install instructions on GitHub here, incorporating the following details:

    • Use sms as the project directory and package name.
    • Use project as the Package Type.
    • Use no for defining both your require and require-dev dependencies interactively.
    • Use the default directory src/ for PSR-4 autoload mapping.

    You should end up in the top-level directory ("sms") for your project in the Terminal.

  2. Create a new directory for your project and then enter it. If you plan to create multiple PHP projects that use Telesign, we recommend that you group them within a telesign_integrations directory.

    cd telesign_integrations
    mkdir sms
    cd sms
    
  3. Create a new file in the "src" directory called "send_sms.php"

    touch src/send_sms.php
    

Create code to send an SMS

  1. Open the file "src/send_sms.php".

  2. Add a script opening tag.

    <?php
    
  3. Add the imports below. The telesign\sdk\messaging\MessagingClient import pulls selected functionality from the Telesign self-service SDK.

    require __DIR__ . "/../vendor/autoload.php";
    use telesign\sdk\messaging\MessagingClient;
    

    📘

    NOTE:

    The require line may need to be modified depending on the location of "autoload.php" in your file system. By default, Composer generates this file in a "vendor" directory at the top-level of your project, as indicated by the path shown in the example above.

  4. Define variables to store your authentication credentials. For testing purposes, you can just overwrite the default value with your credentials or use environment variables.

    $customer_id = getenv('CUSTOMER_ID') ? getenv('CUSTOMER_ID') :'FFFFFFFF-EEEE-DDDD-1234-AB1234567890';
    $api_key = getenv('API_KEY') ? getenv('API_KEY') :'ABC12345yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw==';
    
  5. Define a variable to hold the recipient's phone number. For this tutorial, hardcode your testing device's phone number or pull it from an environment variable.

    $phone_number = getenv('PHONE_NUMBER') ? getenv('PHONE_NUMBER'):'11234567890';
    
  6. Define variables for the message text and the Telesign abbreviation describing the message type. For purposes of this tutorial, use the "ARN" abbreviation (alerts, reminders, and notifications).

    $message = "Your package has shipped! Follow your delivery at https://vero-finto.com/orders/3456";
    $message_type = "ARN";
    
  7. Instantiate a Telesign messaging client object with your authentication credentials.

    $messaging = new MessagingClient($customer_id, $api_key);
    
  8. Make the request and capture the response.

    $response = $messaging->message($phone_number, $message, $message_type);
    
  9. Display the response body in the console for debugging purposes. In your production code, you would likely remove this.

    print_r($response->json);
    
  10. Add the closing tag for the script.

    ?>
    

Sample code

The complete sample code for this tutorial can be found on GitHub.