twilio dynamic voice url number?

953 views Asked by At

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

everything works fine as it should.

however, I would need to allow the users to use their own phone number if they want.

For this I don't know how I should proceed because my current twilio voice URL is an static URL (PHP file with XML output) which has the caller ID within it!

I can simply verify the numbers via rest api and add them to my twilio account but how would I then use those numbers in my application dynamically as opposed to adding 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.

1

There are 1 answers

2
imthepitts On

REVISED

To allow the user to choose their own Caller ID while making calls, we'll assume you've already provided a way in your web UI for them to specify their phone number so that you can verify it with the REST API.

Once verified, you could save their phone number to their user profile, represented below by $UserVerifiedCallerID, so that you can easily pass that number to your Client script.

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

When Twilio calls back to your PHP script to get the TwiML for handling that call, Twilio will send the VerifiedCallerID parameter, which 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 show instead of a hard-coded number.