Voice Verify API - Tutorial: Send a voice message with verification code with Telesign PHP SDK
NOTE:
To add this product to your account, contact a Telesign expert. This product is available for full-service accounts only.
This tutorial teaches you how to use Telesign Voice Verify API with a PHP SDK, by walking you step-by-step through the creation of an integration where you send your own verification code to an end-user. Skip to the end of this page to see the full, completed sample code.
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 a call.
- 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
-
Follow the Telesign Full-service PHP SDK install instructions here on GitHub incorporating these details:
- Use
voiceverify
as the project directory. - Use
project
as the Package Type. - Use
no
for defining both yourrequire
andrequire-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.
- Use
-
Create a new file in the "src" directory called
send_voice.php
and open it in your editor.
touch src/send_voice.php
Send a voice message with a verification code
- Follow the steps below or copy the sample code into the file and update it accordingly.
- Add a script opening tag.
<?php
- Add the imports after the script opening tag. The
telesign\enterprise\sdk\verify\VerifyClient
import pulls selected functionality from the Telesign Full-service SDK.
require __DIR__ . "/../vendor/autoload.php";
use telesign\enterprise\sdk\verify\VerifyClient;
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.
- 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==';
- Define a variable to hold the phone number you want to send voice message to. 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.
- Generate a verification code.
$verify_code = randomWithNDigits(5);
- Create a VerifyClient object and pass it your
customer_id
andapi_key
.
$verify = new VerifyClient($customer_id, $api_key);
- Send a voice message containing the verification code you created. Store the results of your request in the
response
variable.
$response = $verify->voice($phone_number, [ "verify_code" => $verify_code ]);
- Have the end user input the verification code they received, then check it to see if it is valid. Print the results of the check.
echo "Please enter the verification code you were sent: ";
$user_entered_verify_code = trim(fgets(STDIN));
if ($verify_code == $user_entered_verify_code) {
echo "Your code is correct.";
}
else {
echo "Your code is incorrect.";
}
Test your integration
- Switch from your editor to the terminal and run:
php src/send_voice.php
If all goes well, you should send a voice message containing a verification code to the phone number you selected. If you are testing with your phone, you should be able to enter the verification code in response to a prompt from your terminal. The sample will then check your verification code to see if it matches the verification code that was sent and report on the results.
Send a Voice Message with Verification Code in a Different Language
This section explains how to send a custom voice message in a different language using Voice Verify API. A sample is provided here if you want to cut and paste. Note that the only difference between this code sample and the previous code sample in this tutorial is that you use the tts_message
(text-to-speech) parameter to enter a custom message, and the language
parameter to specify the language. For a list of language codes, refer to - Voice Verify API - Supported languages for text-to-speech.
<?php
require __DIR__ . "/../vendor/autoload.php";
use telesign\enterprise\sdk\verify\VerifyClient;
$customer_id = getenv('CUSTOMER_ID') ?? 'FFFFFFFF-EEEE-DDDD-1234-AB1234567890';
$api_key = getenv('API_KEY') ?? 'ABC12345yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw==';
$phone_number = getenv('PHONE_NUMBER') ?? '11234567890';
$language = "fr-FR";
$tts_message = 'Votre code de vérification Widgets \'n\' More est $$CODE$$.';
$verify = new VerifyClient($customer_id, $api_key);
$response = $verify->voice($phone_number, [ "language" => $language, "tts_message" => $tts_message ]);
Sample code
<?php
require __DIR__ . "/../vendor/autoload.php";
use telesign\enterprise\sdk\verify\VerifyClient;
use function telesign\sdk\util\randomWithNDigits;
$customer_id = getenv('CUSTOMER_ID') ?? 'FFFFFFFF-EEEE-DDDD-1234-AB1234567890';
$api_key = getenv('API_KEY') ?? 'ABC12345yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw==';
$phone_number = getenv('PHONE_NUMBER') ?? '11234567890';
$verify_code = randomWithNDigits(5);
$verify = new VerifyClient($customer_id, $api_key);
$response = $verify->voice($phone_number, [ "verify_code" => $verify_code ]);
echo "Please enter the verification code you were sent: ";
$user_entered_verify_code = trim(fgets(STDIN));
if ($verify_code == $user_entered_verify_code) {
echo "Your code is correct.";
}
else {
echo "Your code is incorrect.";
}
Updated about 5 hours ago