twillio phone number lookup sdk5x

158 views Asked by At

Twillio returns a object which has allot of private properties when looking up a phone number. Right below is the code i use

function isValidNumber($number) {
    try{
        $obj = $this->client->lookups->phoneNumbers($number)->fetch();
        return $obj;
    }catch(Twilio\Exceptions\RestException $e){
        return false;
    }
}

This is a fragment of result that i'm getting

object(Twilio\Rest\Lookups\V1\PhoneNumberInstance)#23 (4) {
  ["properties":protected]=>
  array(6) {
    ["callerName"]=>
    NULL
    ["countryCode"]=>
    string(2) "LK"
    ["phoneNumber"]=>
    string(12) "+94xxxxxxxx"
    ["nationalFormat"]=>
    string(12) "xxxxxxxxxx"
    ["carrier"]=>
    NULL
    ["addOns"]=>
    NULL
  }
  ["solution":protected]=>
  array(1) {
    ["phoneNumber"]=>
    string(11) "94xxxxxxxxx"
  }
}

is there a elegant way to get the phoneNumber key out ? Also the try catch doesn't seem to help either, as soon as an invalid entry goes in, exceptions are all over the place. I'm using the SDK 5x

1

There are 1 answers

1
bro_php On BEST ANSWER

If anyone is stuck due to lack of examples and not that much helpful documentation, I came up with something really simple. This is not elegant at all but for now gets the job done

file_get_contents('https://'.$sid.':'.$auth_token.'@lookups.twilio.com/v1/PhoneNumbers/'.urlencode($number));