How to Validate Phone Numbers in PHP
Learn how to validate Phone Numbers with just a few lines of code.
Any time we get new contact information through a web form, we need to validate that information right away in our PHP web form handler.
Failing to validate contact information increases the chances that we’re wasting our own time obtaining unusable data. We won’t know we obtained unusable data until we attempt to contact that person later on, and by then, it might be too late to ask for their information again.
We don’t have to stop at simple data validation, however. With the right tool, we simultaneously validate contact information and parse useful information from the contact string. If we’re validating phone numbers, for example, we can automatically extract the country code and country name of the input number, and we can reiterate the phone number in national and international formats for ease of use later on.
In this tutorial, we’ll learn how to call an API that validates and parses phone number details automatically. We’ll use complementary PHP code examples to structure our API call.
Let’s get started!
Step 1: Install the Client SDK
To begin our tutorial, we’re first going to install the SDK with Composer. Let’s run the following command:
composer require cloudmersive/cloudmersive_validate_api_clientStep 2: Configure the API key
With installation complete, we can turn our attention to configuration and authorization. We’ll need to copy the following snippet and supply a Cloudmersive API key in the appropriate line:
<?php
require_once(__DIR__ . '/vendor/autoload.php');
// Configure API key authorization: Apikey
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');If we don’t have a Cloudmersive API key, we can get one easily by creating a free account on the Cloudmersive website. This will give us a free API key with a limit of 800 API calls per month and zero additional commitments.
Step 3: Create an Instance of the API and Call the Function
We can now instance the API and call the phone number validation function.
$apiInstance = new Swagger\Client\Api\PhoneNumberApi(
new GuzzleHttp\Client(),
$config
);
$value = new \Swagger\Client\Model\PhoneNumberValidateRequest(); // \Swagger\Client\Model\PhoneNumberValidateRequest | Phone number to validate in a PhoneNumberValidateRequest object. Try a phone number such as \"1.800.463.3339\", and either leave DefaultCountryCode blank or use \"US\".
try {
$result = $apiInstance->phoneNumberSyntaxOnly($value);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling PhoneNumberApi->phoneNumberSyntaxOnly: ', $e->getMessage(), PHP_EOL;
}
?>We pass input phone numbers and default country codes (if applicable) in a single object in our input request. This is demonstrated in the following example JSON request model:
{
"PhoneNumber": "string",
"DefaultCountryCode": "string"
}After we successfully make our request, we can expect the API to return information following the below example JSON response model:
{
"IsValid": true,
"Successful": true,
"PhoneNumberType": "string",
"E164Format": "string",
"InternationalFormat": "string",
"NationalFormat": "string",
"CountryCode": "string",
"CountryName": "string"
}Conclusion
Today, we learned how to validate phone numbers from text input using just a few lines of PHP code.
If you have any questions for us about using Cloudmersive data validation APIs in your web form handlers, please feel free to contact a member of our team!


