Twilio dynamic speech voice number?

I am using twlio REST API in my PHP application to make phone calls.

everything works fine as it should.

however, I would have to allow users to use their own phone number if they so choose.

For this, I don't know how I should work, because my current twilio url is a static url (PHP file with XML output) that has the caller id in it!

I can just check the numbers through the rest api and add them to my twilio account, but how can I use these numbers in my application dynamically and not add them manually to the voice url page?

this is my voice url page:

<?php
header('Content-type: text/xml');

// put a phone number you've verified with Twilio to use as a caller ID number
$callerId = "+44XXXXXXXX0";

// put your default Twilio Client name here, for when a phone number isn't given
$number   = "Michelle";

// get the phone number from the page request parameters, if given
if (isset($_REQUEST['PhoneNumber'])) {
    $number = htmlspecialchars($_REQUEST['PhoneNumber']);
}

// wrap the phone number or client name in the appropriate TwiML verb
// by checking if the number given has only digits and format symbols
if (preg_match("/^[\d\+\-\(\) ]+$/", $number)) {
    $numberOrClient = "<Number>" . $number . "</Number>";
} else {
    $numberOrClient = "<Client>" . $number . "</Client>";
}
?>

<Response>
    <Dial callerId="<?php echo $callerId ?>">
          <?php echo $numberOrClient ?>
    </Dial>
</Response> 

      

Any help would be appreciated.

+3


source to share


1 answer


REVISED

In order for the user to choose their own caller ID while making calls, we will assume that you have already provided your web interface in your web interface to provide their phone number so that you can verify it using the REST API .

Once verified, you can save your phone number to your user profile below $UserVerifiedCallerID

so you can easily pass this number to your client script.

var params = {
    "PhoneNumber": $("#number").val(),
    "VerifiedCallerID": "<?php echo $UserVerifiedCallerID; ?>"
};
Twilio.Device.connect(params);

      



When Twilio calls your PHP script to get TwiML to handle this call, Twilio will send a VerifiedCallerID

parameter that you can use to set $callerId

:

<?php
header('Content-type: text/xml');

// Grab the VerfiedCallerID from the request
$callerId = $_REQUEST['VerifiedCallerID'];

...

      

Now, when your user makes calls, their own Verified Caller ID will be displayed instead of the hardcoded number.

0


source







All Articles