17

I'm trying to use Twilio's Lookup API to get certain properties of a mobile number via PHP... with very little success:

    $twilioClient = new Lookups_Services_Twilio(Credential::TwilioSID, Credential::TwilioToken);
    $number = $twilioClient->phone_numbers->get($someNumber);

Note that this is the example code present within their 'Getting Started' page here.

By taking a look at $number in the debugger, I can confirm it is returning something:

enter image description here

The highlighted property of the object is simply recursive with no new information.

Attempting to evaluate $number->phone_number returns null. I have tried this with perhaps half a dozen completely valid numbers now and this is the only response I get.

Attempting to json_encode($number) returns false.

I have no idea why this is not working, but it'd be helpful if I could know what I'm doing wrong.

Community
  • 1
  • 1
marked-down
  • 9,958
  • 22
  • 87
  • 150

3 Answers3

11

I would have been also not successful with their code defined so i used CURL to grab their API methods and it worked like a charm for me, you can try following code to get you need

    $base_url           =       "https://lookups.twilio.com/v1/PhoneNumbers/+1XXXXXXXXXX";
    $ch             =       curl_init($base_url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$account_sid:$auth_token");

$response           =       curl_exec($ch);
$response           =       json_decode($response);

echo "<pre>";   print_r($response); echo "</pre>";

It will return you few parameters (country_code, national_format, carrier)

Naveed Metlo
  • 206
  • 1
  • 2
  • 10
5

I'm just gonna go ahead and assume the phone numbers you've tried are neither from the US, nor in international format.

From Twilio's Lookups Quickstart Tutorial:

You'll want to include the country code of the phone number that you would like formatted. If not included, the country code will default to the US.

So your lookup should probably look like:

$number = $twilioClient->phone_numbers->get($someNumber, array('CountryCode' => 'NZ'));

If the phone numbers are from the US, in international format, or if the above still does not work, try whether the lookup succeeds on Twilio's web interface (you'll need the international prefix there).

If it does, your software library might be broken or your Twilio account might have incorrect/broken access rights.

If the web lookup fails as well, you should contact Twilio and report the issue.

Siguza
  • 21,155
  • 6
  • 52
  • 89
  • I can confirm the numbers I'm using work with the Twilio web interface, and that my account is active (it shouldn't matter since the API call is free regardless). I'll check over my access rights again tonight... – marked-down Jul 20 '15 at 03:20
3

Now 9-6-2016 and they still haven't fixed their PHP library...

None the less here is what worked for me. If you want more information like caller name etc you have to enable this in your twilio dashboard first.

require 'includes/twilio/Services/Twilio.php';

// Your Account Sid and Auth Token from twilio.com/user/account
$sid = "YOUR-SID";
$token = "YOUR-TOKEN";
$client = new Lookups_Services_Twilio($sid, $token);


// Lookup
$phoneNumber = rawurlencode("(000) 000-0000");
$full_path = $client->phone_numbers->uri . "/$phoneNumber" . "?CountryCode=US&Type=carrier&Type=caller-name";
$number = new $client->phone_numbers->instance_name($client, $full_path);

echo "Caller name:" . $number->caller_name->caller_name;
echo "<br>";
echo "Caller type:" . $number->caller_name->caller_type;
echo "<br>";
echo "Carrier type:" . $number->carrier->type . "\r\n";
echo "<br>";
echo "Carrier name:" . $number->carrier->name;
echo "<br>";
echo "Phone number:" . $number->phone_number;
echo "<br>";
echo "Country code:" . $number->country_code;
Ron Smart
  • 31
  • 4