简体   繁体   中英

How to validate phone number in Twilio using Laravel 5.1?

I am using Twilio Api to send sms to users, it gives fatal error if user's phone number is invalid. I want to validate the phone number before sending messages.

Currently I am using the following method to validate it:

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)

and it works fine.

But I want to validate it using Twilio's inbuilt functionality. I have searched a lot on internet, but nothing helped.

The documentation is a wonderful resource for questions like that.

The Twilio SDK has a method for that specific feature:

I'll copy and paste directly from the documentation:

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

You can see examples of responses by trying out their online lookup functionality: https://www.twilio.com/lookup

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM