Reputation: 121
I am trying to use the Twilio api, that is here: https://www.twilio.com/lookup
it says that if I call this:
curl -XGET "https://lookups.twilio.com/v1/PhoneNumbers/(405)%20555-1212?Type=carrier&Type=caller-name" -u "{AccountSid}:{AuthToken}"
it will respond with data like this:
{
"country_code": "US",
"phone_number": "+14055551212",
"national_format": "(405) 555-1212",
"url": "https://lookups.twilio.com/v1/PhoneNumber/+14055551212",
"caller_name": {
"caller_name": null,
"caller_type": null,
"error_code": null,
}, "carrier": {
"type": "mobile",
"error_code": null,
"mobile_network_code": null,
"mobile_country_code": "310",
"name": null
}
}
so in PHP how do I call that as a variable?
I tried this:
$res = curl -XGET "https://lookups.twilio.com/v1/PhoneNumbers/(405)%20555-1212?Type=carrier&Type=caller-name" -u "{AccountSid}:{AuthToken}";
But I get errors.
I tried to create a curl function like this:
function url_get_contents($_turl) {
if (!function_exists('curl_init')) {
die('CURL is not installed!');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $_turl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
if ($output === false) { die(curl_error($ch)); }
curl_close($ch);
return $output;
}
then call it like these:
$res = url_get_contents("https://lookups.twilio.com/v1/PhoneNumbers/4055551212?Type=carrier&Type=caller-name -u AccountSID:AccountAuth");
and
$res = url_get_contents("https://lookups.twilio.com/v1/PhoneNumbers/4055551212?Type=carrier&Type=caller-name");
but the first one does not work. the second one works but it says I did not pass a valid sid and auth...
So, is there a way to make this work without tons and tons of code?
Upvotes: 0
Views: 1798
Reputation: 1330
I just had to do this recently. It looks like you are trying to pass the Curl authentication parameters incorrectly. Give this function a try:
function lookup($number){
// Twilio Account Info
$acct_sid = "xxxxx";
$auth_token = "yyyyy";
// Fetch the Lookup data
$curl = curl_init("https://lookups.twilio.com/v1/PhoneNumbers/".$number);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, $acct_sid.":".$auth_token);
$response_data = curl_exec($curl);
curl_close($curl);
// Handle the Data
$lookup_data_array = json_decode($response_data,true); // Convert to array
print_r($lookup_data_array);die();
}
Then you just pass the number you want to lookup like this:
$this->lookup("650-319-8930");
You should then get a response like this:
Array
(
[caller_name] => Array
(
[caller_name] => CLOUDFLARE, INC
[caller_type] => BUSINESS
[error_code] =>
)
[country_code] => US
[phone_number] => +16503198930
[national_format] => (650) 319-8930
[carrier] => Array
(
[mobile_country_code] =>
[mobile_network_code] =>
[name] => Proximiti Mobility 2
[type] => voip
[error_code] =>
)
[url] => https://lookups.twilio.com/v1/PhoneNumbers/+16503198930?Type=carrier&Type=caller-name
)
Good Luck!
Upvotes: 1