1. Skip to Navigation
  2. Skip to Content
our blog

Finding the Carrier of a Cell Phone Number in PHP

Updated: 6/21/2012

Edit: It has been pointed out by a few readers that CloudVox is no longer online. While this post still contains valuable information, we will have to find another free provider to replace CloudVox.

Often web apps need to send SMS messages to their users (for example: balance alerts, reminders, notifications). To send these text messages from within your app, you basically have 2 options:

1) Pay Twilio and Tropo for the use of their APIs, for roughly a penny per message.

2) Send an email directly to their carrier’s mail-to-SMS gateway – a free service most carriers provide, which will translate your email into an SMS sent to their subscribers’ phone

Twilio and Tropo are about as easy as it gets to integrate (REST-like APIs that return JSON/POX), but you pay for every single message and that can add up quickly. So #2 (mail-to-SMS gateways) starts to look pretty good.

But there’s some problems with mail-to-SMS gateways:

1) Each carrier has their own format. For example, T-Mobile’s gateway is number@tmomail.net (for the US), AT&T’s is number@txt.att.net (for most of their customers), and Verizon is number@vtext.net (unless you used to be with Alltel – then its number@message.alltel.com). A big list of mail-to-SMS-gateways is available on Wikipedia.

Bonus:some carriers want the international code prepended (the 1 in front of the number, for the US), some carriers don’t. (e.g., T-Mobile does, AT&T doesn’t)

2) Some carriers require authentication (Sprint) or don’t have a mail-to-SMS gateway at all (Google Voice).

3) If the user ports their number to a different carrier you’ll have to detect it and react accordingly.

So you’ll have to figure out what carrier they’re using in order to direct the email and meet whatever requirements – which usually means asking the user to choose their carrier from a list.

There is a better way! What if we could detect the carrier automatically?

It turns out you can. The lovely folks at CloudVox made an API that does exactly that – you give it a phone number, and it tells you the carrier and if its a cell-phone or landline. Its 100% free, with no API keys/credentials required.

Its hosted at digits.cloudvox.com, along with example calls/responses (full docs are here). It speaks JSON over HTTP.

This gives us a much better user experience:

1) User inputs their number, no carrier dropdown needed

2) We use CloudVox’s API to determine their carrier

3) Try to send them an SMS, and have them confirm ownership in the normal way (i.e., enter a confirmation code)

This will cover most people, and is free outside of the development time. If there was an error (i.e., we couldn’t find the number with CloudVox’s API, or we did but the carrier doesn’t support mail-to-SMS), we can fall back to Twilio or Tropo. We’re still paying a few cents here and there, but the default case is free.

Here’s some example code in PHP, for querying CloudVox’s API:

<?php
	/* Finds the carrier for the given phone number using CloudVox's API. Returns the carrier name as a string on success, or false if the carrier couldn't be determined */
	function findCarrier($phoneNumber)
	{
		$carrier = false; // will contain the carriers name as a string if we are able to look it up
 
		// attempt to lookup the number with the CloudVox API
		$requestUrl = 'http://digits.cloudvox.com/' . $phoneNumber . '.json';
		$apiResponse = file_get_contents($requestUrl); // Note: needs code to handle the error case of file_get_contents
 
		if ($apiResponse)
		{
			$responseJson = json_decode($apiResponse);
			if (isset($responseJson->allocation) && isset($responseJson->allocation->carrier_name))
			{
				$carrier = $responseJson->allocation->carrier_name;			
			}
		}
		return $carrier; // will be false or the name of the carrier
	}
 
	if (isset($_GET['phoneNumber']))
	{
		// user has submitted a form via GET, containing a field called phoneNumber
 
		// collect the phoneNumber and remove any non-numeric chars
		$phoneNumber = preg_replace('#[^0-9]#','',strip_tags($_GET['phoneNumber'])); 
		$carrier = findCarrier($phoneNumber);
 
		if ($carrier)
		{
			if (stripos($carrier, 'verizon') !== FALSE)
			{
				sendWithVerizon($phoneNumber);
			}
			else if (stripos($carrier, 'at&t') !== FALSE)
			{
				sendWithATT($phoneNumber);
			}
			else
			{
				// other carrier. you'd really want to fall back to Twilio/Tropo here, and/or log the carrier so you can add it later
				echo "Unknown Carrier: {$carrier}";
			}
		}
		else
		{
			// send SMS via Twilio or Tropo instead
		}
	}
	else
	{
		// show the form
		echo '<form>' . 
			 '<label for="phoneNumber">Phone Number</label>' .
			 '<input type="text" name="phoneNumber" />' .
			 '</form>';
	}
?>

8 Responses to Finding the Carrier of a Cell Phone Number in PHP

  1. Devan H Crow says:

    Thanks!!! This works perfectly.

  2. Alec says:

    The info at http://digits.cloudvox.com seems to be outdated. I’ve tried using it on my number, it said my phone was part of Spring. I actually migrated (ported) my number to another provider in 2003.

  3. Although I haven’t used Cloudvox.com myself, what I have found is that cell numbers were issued to carriers in blocks and that most of the cell number to carrier are based on that block and doesn’t take into consideration numbers that are ported.

    I have actually come across a very cost effective service (http://www.data24-7.com/) that if you are doing any quantity of such look ups that this service works very well. The costs are a flat $12.00 month account fee and $0.006 (less than a penny) per transaction. The best product is textat which is primarily domestic. I had ported a number from AT&T to VerizonWireless about a year ago and they had the correct information.

  4. Pingback: PHP – how to find out your phone carrier « xfiles petron

  5. quasidynamic says:

    Looks great, but evidently they are out of business Since Nov 2011??

    :(

  6. AGreenhill says:

    CloudVox is dead… their page comes back as saying it’s been down since Nov 2011.

  7. Selona says:

    As of 11/30, cloudvox has been taken down.

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Current ye@r *