Phone ID - Tutorial: Cleanse a number with Telesign PHP SDK

📘

NOTE:

This tutorial applies only to self-service accounts, not full-service accounts.

This tutorial walks you step-by-step through how to write code that makes a request to Telesign Phone ID using a PHP SDK to cleanse a phone number. Phone number cleansing corrects common formatting issues in submitted phone numbers. Skip to the end of this page to see the full sample code.

This code will take your test phone number, add an extra digit to make it invalid, and send it to Phone ID to cleanse. It then takes the cleansed phone number from the response and prints it to the console.

Before you begin

Make sure you have the following ready:

  • Authentication credentials - Your Telesign Customer ID and API Key. If you don't have these already, see How do I find my Customer ID and API Key?
  • Phone number - The phone number you want to cleanse.
  • Composer: This package manager isn't required to use the SDK, but it is required for this tutorial.

📘

NOTE:

This tutorial uses the following:

  • MacOS
  • PHP v8.4.2
  • Composer v2.8.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 here on GitHub incorporating these details:

    • Use phoneidtest as the project directory.
    • 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 for your project in the Terminal.

  2. Create a new file in the "src" directory called phone_id_cleanse_number.php and open it in your editor.

touch src/phone_id_cleanse_number.php

Cleanse a number with Phone ID

  1. Follow the steps below or copy the sample code into the file and update it accordingly.
  2. Add a script opening tag.
<?php
  1. Add the imports after the script opening tag. The telesign\sdk\phoneid\PhoneIdClient import pulls selected functionality from the Telesign Self-service SDK.
require __DIR__ . "/../vendor/autoload.php";


use telesign\sdk\phoneid\PhoneIdClient;

📘

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.

  1. Define variables to store your authentication credentials.
$customer_id = getenv('CUSTOMER_ID') ?? 'FFFFFFFF-EEEE-DDDD-1234-AB1234567890';
$api_key = getenv('API_KEY') ?? 'ABC12345yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw==';
  1. Define a variable to hold the phone number you want to cleanse. Use the complete number, including the country code. This should be a string with no spaces or special characters. For example: 16505551212.
$phone_number = getenv('PHONE_NUMBER') ?? '11234567890';

📘

NOTE:

In your production integration, pull the phone number from your recipient database instead of hardcoding it.

  1. This code adds an extra digit to the number to make it invalid.
$extra_digit = "0";
$incorrect_phone_number = "$phone_number$extra_digit";
  1. Instantiate a PhoneIdClient object and pass it your customer ID and your API key.
$data = new PhoneIdClient($customer_id, $api_key);
  1. Use Phone ID to check and cleanse the incorrect phone number you created earlier.
$response = $data->phoneid($incorrect_phone_number);
  1. Use two print statements to check the results of the cleansing procedure on the incorrect phone number. The first print statement explains what the cleansed phone number is and displays the JSON response for the cleansed number. The second print statement shows the original phone number and displays the JSON response for the original phone number.
if ($response->ok) {
  printf("Cleansed phone number has country code %s and phone number is %s.",
  $response->json['numbering']['cleansing']['call']['country_code'],
  $response->json['numbering']['cleansing']['call']['phone_number']);
  printf("Original phone number was %s.",
  $response->json['numbering']['original']['complete_phone_number']);
}
  1. If everything works, you send an incorrect phone number to Phone ID for cleansing, and you get back a response containing your cleansed phone number and the original phone number.

Test your integration

  1. Switch from your editor to the terminal and run:
php src/phone_id_cleanse_number.php

Sample code

<?php
require __DIR__ . "/../../vendor/autoload.php";
use telesign\sdk\phoneid\PhoneIdClient;
$customer_id = getenv('CUSTOMER_ID') ?? 'FFFFFFFF-EEEE-DDDD-1234-AB1234567890';
$api_key = getenv('API_KEY') ?? 'ABC12345yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw==';
$phone_number = getenv('PHONE_NUMBER') ?? '11234567890';
$extra_digit = "0";
$incorrect_phone_number = "$phone_number$extra_digit";
$data = new PhoneIdClient($customer_id, $api_key);
$response = $data->phoneid($incorrect_phone_number);
if ($response->ok) {
  printf("Cleansed phone number has country code %s and phone number is %s.",
  $response->json['numbering']['cleansing']['call']['country_code'],
  $response->json['numbering']['cleansing']['call']['phone_number']);
  printf("Original phone number was %s.",
  $response->json['numbering']['original']['complete_phone_number']);
}