php - 如果数字不正确,Twilio PHP Lookup API 不会返回任何响应?

标签 php twilio twilio-api twilio-php

我正在尝试实现 Twilio Lookup API 来验证电话号码是否正确。

我尝试的是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"]]);

对于正确的数字,它返回响应,而对于错误的数字,它没有任何响应。

enter image description here

经过挖掘,我找到了另一个解决方案 link如果数字不正确,则使用curl 和getter 进行响应。

$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

我缺少实现的 PHP 查找 API 中存在什么问题,并且在数字不正确时导致没有响应?

最佳答案

它可能会抛出异常

参见: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();
}

关于php - 如果数字不正确,Twilio PHP Lookup API 不会返回任何响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73212409/

相关文章:

php - 如何从字符串中提取大写单词

php - Laravel 返回response()->json() 而不是只返回一个数组

php - Laravel 4.2 - 使用::firstOrCreate 并确定实际发生了什么?

database - 加密 4 位密码/密码 - 容易破解吗?

java - 使用 Java 的 Twilio 发起和终止 SIP URI

php - 计算值的出现次数并将其作为 value_name=>occurrences 对返回

ios - 如何在短信中发送链接以便自动加载预览?

azure - 使用 Azure Web 功能接收 Twilio SMS 消息

c# - 使用 C# 下载 Twilio 录音

Twilio 出站调用 - 关键数字