Phone ID - Tutorial: Check phone type to block VoIP 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 identify voice over internet protocol (VoIP) numbers, so you can block them. Skip to the end of this page to see the full sample code.
Why block VoIP numbers?
VOIP numbers are cheap, easy to buy in bulk, and can easily be used by a fraudster to sign up for an account that requires a phone number as part of registration. After signup, they may abuse promotions and bonuses, spam other users, create fake likes, comments, or product reviews, sell fake/bulk accounts, and engage in other similar fraudulent activities. Blocking VOIP numbers can be a key part of securing your account registration process.
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 get information about. If you are a self-service customer and have not added money to your account yet, you need to first get the phone number verified by Telesign by adding it to your list of Test Numbers.
- 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 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 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
phone_id_check_type.php
and open it in your editor.
touch src/phone_id_check_type.php
Check phone type
- 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\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.
- 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 get information for and a variable to store the phone type, represented by a numerical identifier for a VoIP phone. For phone number, 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';
$phone_type_voip = "5";
NOTE:
In your production integration, pull the phone number from your recipient database instead of hardcoding it.
- Instantiate a PhoneIdClient object and pass it your customer ID and your API key.
$data = new PhoneIdClient($customer_id, $api_key);
- Use Phone ID to check your phone number and store the results of your check in
response
.
$response = $data->phoneid($phone_number);
- Check
phone_type
. If it is "VOIP", print a message saying that the number is a VoIP phone. Otherwise, print a message saying it is not a VoIP phone.
if ($response->ok) {
if ($response->json['phone_type']['code'] == $phone_type_voip) {
echo "Phone number $phone_number is a VoIP phone.";
}
else {
echo "Phone number $phone_number is not a VoIP phone.";
}
}
NOTE:
In a production integration, you would instead implement logic here to block the VoIP number.
- If everything works, it should submit the phone number you specified to a check by Phone ID. Depending on whether the phone type is VoIP or not, the appropriate message is displayed.
Test your integration
Switch from your editor to the terminal and run:
php src/phone_id_check_type.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';
$phone_type_voip = "5";
$data = new PhoneIdClient($customer_id, $api_key);
$response = $data->phoneid($phone_number);
if ($response->ok) {
if ($response->json['phone_type']['code'] == $phone_type_voip) {
echo "Phone number $phone_number is a VoIP phone.";
}
else {
echo "Phone number $phone_number is not a VoIP phone.";
}
}
Updated 15 days ago