我正在尝试实现 Twilio Lookup API 来验证电话号码是否正确。
我尝试的是PHP 查找API文档
$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"]]);
对于正确的号码,它返回响应,而对于错误的号码,它是空的,没有任何响应。
在挖掘了一些之后,我发现了另一个解决方案链接,即使用curl并在数字不正确时获取响应。
$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>";
PHP 查找 API 中我缺少实现并且在数字不正确时导致没有响应的问题是什么?
它可能会抛出
Exception
。
请参阅:https://www.twilio.com/docs/libraries/php/usage-guide#exceptions
我假设你可以这样做:
$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();
}