如何使用 Laravel 5.1 在 Twilio 中验证电话号码


How to validate phone number in Twilio using Laravel 5.1?

我正在使用Twilio Api向用户发送短信,如果用户的电话号码无效,则会给出致命错误。我想在发送消息之前验证电话号码。

目前我正在使用以下方法来验证它:

public function get_web_page($url) {
            $options = array(
                CURLOPT_RETURNTRANSFER => true,   // return web page
                CURLOPT_HEADER         => false,  // don't return headers
                CURLOPT_FOLLOWLOCATION => true,   // follow redirects
                CURLOPT_MAXREDIRS      => 10,     // stop after 10 redirects
                CURLOPT_ENCODING       => "",     // handle compressed
                CURLOPT_USERAGENT      => "test", // name of client
                CURLOPT_AUTOREFERER    => true,   // set referrer on redirect
                CURLOPT_CONNECTTIMEOUT => 120,    // time-out on connect
                CURLOPT_TIMEOUT        => 120,    // time-out on response
            ); 
            $session = curl_init($url);
            curl_setopt_array($session, $options);
            $content  = curl_exec($session);
            curl_close($session);
            return $content;
        }
$response = $this->get_web_page("https://credentials:removed@lookups.twilio.com/v1/PhoneNumbers/3234433584");
        $resArr = array();
        $resArr = json_decode($response)

它工作正常。

但我想使用 Twilio 的内置功能对其进行验证。我在互联网上搜索了很多,但没有任何帮助。

该文档是此类问题的绝佳资源。

Twilio SDK 具有针对该特定功能的方法:

我将直接从文档中复制和粘贴:

<?php
$client = new Services_Twilio('AC123', '123');
$response = $client->account->outgoing_caller_ids->create('+15554441234');
print $response->validation_code;

您可以通过尝试其在线查找功能来查看响应示例:https://www.twilio.com/lookup