Jenz
Jenz

Reputation: 8369

Get telephone code from a country or country code - Twilio

Using twilio, is there a way to get the telephone code for a given country or country code?

Eg: Given country as United States, it should return 1 as the telephone code.

Thanks in advance.

Upvotes: 2

Views: 2718

Answers (1)

philnash
philnash

Reputation: 73029

Twilio developer evangelist here.

As WEBjuju pointed out, that answer has some good options. Notably, this is a JSON file of countries and codes and if you need to go from country name to two letter code, then this JSON will help.

If you are looking to extract this information from phone numbers, then Google's libphonenumber is the canonical resource and there is a PHP version of libphonenumber available.

You could also use Twilio's Lookup API to get information out of a phone number. With the Twilio PHP library, this would look like:

use Twilio\Rest\Client;

// Your Account Sid and Auth Token from twilio.com/console
$sid = "YOUR_ACCOUNT_SID";
$token = "YOUR_AUTH_TOKEN";

$client = new Client($sid, $token);

$number = $client->lookups->phoneNumbers("+15108675309")->fetch();

echo $number->countryCode; // result - CA, US, etc.

Let me know if this helps at all.

Upvotes: 2

Related Questions