How to Validate Phone Numbers in PHP with the Twilio Lookup API
Time to read: 3 minutes
Twilio Lookup is a phonebook REST API that you can use to check whether a number exists, format international numbers to local standards, determine whether a phone can receive text messages, and even discover information about the carrier associated with that phone number.
In this post, we’re going to learn how to deal with valid and invalid numbers using the Twilio PHP library. Whether you need to look up customer numbers in your production Laravel app or just have a basic script you want to run to check numbers in a local database, this code should get you started with validating phone numbers.
Getting started
We’ll need a few things before we start building:
- PHP – I am using version 5.5 for this post
- Composer – This will help us manage our dependencies
- The Twilio PHP Library – Open your terminal, navigate to the folder you want your code to live in and enter the following to install it with Composer:
- A Twilio Account – Sign up for free
- Grab your Account SID and Auth Token from the Twilio dashboard as seen in this image:
Looking up valid phone numbers
Check out the Lookup page if you want to play around with the API and see what type of data a request will return. Try entering a phone number and take a look at the JSON response object.
Here’s a quick code sample to do a basic phone number lookup. Create a file named lookup.php
and add this code to it:
Open up your terminal and navigate to the directory containing lookup.php
and run this command:
This basic functionality is free, but you can get more information by doing a carrier lookup. Carrier lookups are commonly used to determine if a number is capable of receiving SMS/MMS messages. These cost $0.005 but a free trial account has $20 in credit to play with.
Carrier lookups require an extra parameter.
Looking up invalid phone numbers
In this “online phonebook” the phone numbers serve as a unique ID. When you try to look up a phone number that does not exist you will get a 404 response.
Head over to the Lookup homepage again to see this in action when you check a number that doesn’t exist:
Let’s create a function that takes a phone number, returns true if the number exists and false if it doesn’t. We’ll do this by trying to do a carrier lookup and checking to see if an exception was raised with a 404 error code.
Replace the code in lookup.php
with the following:
Try running this code by using the following command in your terminal, and don’t forget to insert your Account SID and Auth Token:
You now have a function you could use anywhere in your code for checking phone numbers.
Looking Ahead
So now you know how to use the REST API phone book that is Twilio Lookup. You can also check out other resources to learn how to use Lookup in other languages:
- Lookup phone numbers in Python
- Lookup phone numbers in Ruby.
- API documentation.
Feel free to reach out if you have any questions or comments or just want to show off the cool stuff you’ve built.
- Email: sagnew@twilio.com
- Twitter: @Sagnewshreds
- Github: Sagnew
- Twitch (streaming live code): Sagnewshreds
Related Posts
Related Resources
Twilio Docs
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
Resource Center
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Ahoy
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.