gehlotparesh
gehlotparesh

Reputation: 142

Twilio basic and call forwarding

I created a test account on Twilio and they gave me a number.

Now I want to forward call coming to that given(given by Twilio) to any other number. I am able to do this from Twilio's website.

But, I want to make this happen through my application, where:

  1. on one side, there is my number and
  2. one other side, there is a textbox, in which I will give the number, on which the calls will be forwarded and
  3. a save button, which will save the changes, after pressing which, whenever someone calls on the number given by Twilio, that incoming call will be forwarded to the number specified in textbox.

Upvotes: 0

Views: 1089

Answers (3)

gehlotparesh
gehlotparesh

Reputation: 142

$success_flag = false;
$phone_number_array = "";
$phone_number_array = json_decode($_POST['phone_number_array'], true);
$phone_number_array=array_map('trim',$phone_number_array);

$forward_number_array = "";
$forward_number_array = json_decode($_POST['forward_number_array'], true);
$forward_number_array=array_map('trim',$forward_number_array);  
$arrResponse = $forward_number_array;

try {
    for ($counter=0; $counter < count($phone_number_array); $counter++) { 

        foreach ($client->account->incoming_phone_numbers->getIterator(0, 50, array(
                "PhoneNumber" => $phone_number_array[$counter]
            )) as $number
        ) {
            $voice_url = "http://twimlets.com/forward?PhoneNumber=" . $forward_number_array[$counter];
                $number->update(array(
                "VoiceUrl" => $voice_url,
            ));
            $success_flag = true;

        }
    }       
} catch (Exception $e) {
    $success_flag = false;
    $error = "\n\nError in forward numbers : " . $e;
    file_put_contents("debug_file.txt", print_r($error, true), FILE_APPEND);

}

if($success_flag==false){
    $response = "no records found";
    echo $response;
}else{
    $response = "Changes saved successfully";
    echo $response;
}   

Upvotes: 0

ecorvo
ecorvo

Reputation: 3629

You can actually achieve this using TwiML which is plain XML. Just point the voice URL of your twilio number to an endpoint on your app that outputs this:

<?php
header("content-type: text/xml");
echo '<?xml version="1.0" encoding="UTF-8"?>';
$forward_to="";
if($_REQUEST['To'] == $number_a){
    $forward_to ='forward  number'; //this is already defined by your users, so it much be stored somewhere...
}elseif($_REQUEST['''] == $number_b){
    $forward_to ='forward  number'; //this is already defined by your users, so it much be stored somewhere...
}
?>

<Response>
    <Dial>
        <Number><?php echo $forward_to; ?></Number>
    </Dial>
</Response>

So when a call comes in you check where is the call coming in from, number a or number b. Then if it is from a you get the forward number for a (ie. c) and if it is for b you get the forward number for b (ie. d).

Upvotes: 2

philnash
philnash

Reputation: 73075

Twilio developer evangelist here.

If you're completely new to Twilio then I suggest you take a look through our PHP quickstart for voice. The tutorials there will get you off the ground working with Twilio and then you'll be able to customise your application the way you want.

If you then come across some problems, you can post here at StackOverflow with the code you've tried and people here will try to help.

Upvotes: 0

Related Questions