0

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:[email protected]/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.

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

3 Comments

hi thanks for answering, i have tried this, it gives following error instead of any response "Services_Twilio_RestException in Twilio.php line 297: The phone number you are attempting to call, +15554441234, is not valid."
@Insomania, that's not wrong. You need to catch the Services_Twilio_RestException when a number isn't valid. Look up the PHP manual on Exceptions
yo! man, you are great, i have learned a new thing today, thanks so much it worked :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.