NomanJaved
NomanJaved

Reputation: 1390

Twilio PHP Lookup API is returning no response if number is not correct?

I am trying to implement a Twilio Lookup API for validation of phone numbers are they correct or not.

What I have tried is a PHP Lookup API documentation

$number = "+15445453"; // Wrong Number
$number = "+18186794307"; // Correct Number

$twilio = new Client($this->session->userdata('account_sid'), $this->session->userdata('auth_token'));
$phone_number = $twilio->lookups->v1->phoneNumbers($number)->fetch(["type" => ["carrier"]]);

For the correct number, it is returning the response, whereas for the wrong number it's empty nothing response.

enter image description here

After digging some I found another solution link that is using curl and getter a response if a number is not correct.

$base_url = "https://lookups.twilio.com/v1/PhoneNumbers/" . $number;
$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>";

enter image description here enter image description here

What's the issue in PHP lookup API that I am missing to implement and causing no response if a number is not correct?

Upvotes: 2

Views: 344

Answers (1)

Jared Dunham
Jared Dunham

Reputation: 1527

It may be throwing an Exception.

See: https://www.twilio.com/docs/libraries/php/usage-guide#exceptions

I would assume you could do something like:

$twilio = new Client(
    $this->session->userdata('account_sid'), 
    $this->session->userdata('auth_token')
);

try {
    $phone_number = $twilio->lookups->v1->phoneNumbers($number)->fetch(["type" => ["carrier"]]);
} catch (TwilioException $e){
  echo $e->getCode();
}

Upvotes: 2

Related Questions