0

I am extremely new to twilio, I created a test account on twilio, 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

I want to achieve this functionality through PHP

But am totally unknown to twilio.

Hoping for help.

Any help will be greatly appreciated.

Thanks in advance for helping.

gehlotparesh
  • 142
  • 3
  • 13

3 Answers3

2

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).

ecorvo
  • 3,559
  • 4
  • 24
  • 35
  • Thanks ecorvo for your reply, but I have multiple purchased numbers, in the opposite to them, there will be text boxes, in which user will be giving respective numbers, on which user wants to forward call. So how above xml will distinguish that for which number, to whom the call needs to be forwarded – gehlotparesh Sep 14 '15 at 08:48
  • So do you want to do the routing as the call comes in or do you want have it pre set? – ecorvo Sep 14 '15 at 09:52
  • I want it when call comes, it should be forwarded but didn't know the exact way how to do it – gehlotparesh Sep 14 '15 at 10:53
  • I would imagine you would have this pre defined prior to the call coming in for example if calling this number forward to this number etc... But if you want to do it on the fly it is a bit more complicated. You would need to place the caller on hold while alert someone that a call is coming in then at this point that person would manually forward the call. Is this what you want? – ecorvo Sep 14 '15 at 11:24
  • I think, what you are calling predefined, is the only thing I want. I want it in below way: There are two numbers(a,b) with no forwarding for now. But, through my interface, user enters data as below, for a=>c b=>d So, when any calls "a", call should be forwarded on "c" and "b", call should be forwarded on "d" This is the thing that is needed by me. – gehlotparesh Sep 14 '15 at 12:37
  • I saw updated answer, but it's not as per the requirement am requesting, I will make you understand the whole scenario, consider there is a table, which consist of headers as "Purchased Numbers" and "Forward To", below headers, there's a row consisting of Purchased Number "a" under "Purchased Numbers" and a "Textbox" under "Forward To", in which user enters a number to which users wants to forward call for Purchased Number "a". Similarly there's another row having Purchased number "b" and a "Textbox", in which user will enter "Forward To" number for "b" And so on At the end of the table.... – gehlotparesh Sep 15 '15 at 07:25
  • At the end of the table.... There will be a button, which will allow the user to save the changes made. For example, if in the textbox opposite to purchased number "a", users writes "c", simultaneously, in the textbox opposite to purchased number "b", users writes "d" And then user press "Save" button, then, at any point of time, if call is made to "a", it should be forwarded to "c", similarly, if call is made to "b", it should be forwarded to "d" Did you get the whole concept? – gehlotparesh Sep 15 '15 at 07:29
  • I get it. and my answer should work fine. All you have to do is fetch the data from the table at the time of an incoming call and put it in $forward_to. I assume you know how to do the table part. My answer only covers the Twilio part. So basically what we are doing is pointing the incoming call URL in the twilio number to an endpoint that contains the code on the answer. So when a call comes in, we lookup on the stored table using the "To" param from twilio to see what number is being called. – ecorvo Sep 15 '15 at 14:00
  • Once we identify the number the table will tell us where to forward the call to, so at that point we put this into the variable "forward_to" and then output the XML to let twilio know what to do. This is all server-side, not what your users will see. Does it makes sense now? – ecorvo Sep 15 '15 at 14:00
  • Hey Ecorvo, first of all, I would like to thank you for your immense support, without you support, it would not have possible for me to achieve this functionality. However, It's done it different way, without any involvement of any XML file. I kept my answer below, please do see the answer. Once again, I would like to heartily thank you for your immense support, thank you very much. – gehlotparesh Sep 16 '15 at 08:37
  • First of all Im glad you found a solution. But I would like you to know that although that seems like an easy solution, it might not scale well. This works well for a couple numbers, but if your applications grows to lets say 100 numbers, this would be fairly slow. Essentially you are looping thru all numbers in your account and changing the voice URL to a twimlet. The XML I showed you is the equivalent of the twimlet but on your server, instead of twilio. Therefore no looping needed. – ecorvo Sep 16 '15 at 10:07
  • see Call forwarding with TwiML https://www.twilio.com/help/faq/voice/how-can-i-set-up-call-forwarding and https://www.twilio.com/docs/api/twiml/dial – ecorvo Sep 16 '15 at 10:08
  • Hey Ecorvo, thank you for your suggestion, I really appreciate the same, and its really useful. Once again thank you so much for all the help that you did from the base level to the success level. Thank you very much. – gehlotparesh Sep 18 '15 at 07:46
0

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.

philnash
  • 70,667
  • 10
  • 60
  • 88
  • Thanks for the reply I am through with getting 1. available numbers 2. currently owned or purchased numbers But am unable to find api which can give current balance of respective user So is there any api to get current balance of respective user? Your help is and will really be appreciated – gehlotparesh Sep 11 '15 at 12:36
  • @PJBrunet I'm sorry you feel that way. We're always looking to improve our documentation and tutorials, so if you have the time I'd really appreciate if you could share with me which tutorials you have been having trouble with and we can work specifically to improve them. My email address is philnash@twilio.com. – philnash May 04 '19 at 06:55
  • @philnash Just look at the top answer here. No need to install anything, I pasted it into nano and had my new phone number working in a few minutes. Whereas your website wants me to install a lot of bloated garbage that I don't need: Composer, Packagist, Laravel, PHP Helper Library, yuck! I just needed a quick solution. I'm busy and I don't have time for all that. Personally, I love your curl documentation and I looked there first, but just making/receiving calls I did not find a curl solution on your website. Here I found solutions to make/receive calls in a few minutes without all the fuss. – PJ Brunet May 05 '19 at 01:14
  • @PJBrunet appreciate that, our tutorials are very much setup to help people who are working with full applications and would likely have Composer or even a full framework like Laravel that they want to integrate Twilio with. Those tutorials become a jumping off point for more fully featured applications and integrations. If you're looking for simple call forwarding then I'd suggest looking into [TwiML Bins](https://support.twilio.com/hc/en-us/articles/223179908-Setting-Up-Call-Forwarding) which are hosted on Twilio and only return TwiML. No need for even PHP then. – philnash May 05 '19 at 04:07
  • @philnash Yes, unfortunately many people are short-sighted and over-engineer "full applications" to be more complex than necessary, even when the solution is just a header + a string of text. (I learned that lesson 30+ years ago with Pascal & C libraries.) Just two lines of code I can paste into a "full application" very easily. Same with curl, these are one-liners I'm using in my backend systems to SMS texts, photos, emojis, reply to texts, etc. Thankfully, Twilio is easy to use and I plan to use it more extensively with my clients and in all my business projects. – PJ Brunet May 05 '19 at 20:24
  • I'm glad it's working out for you :) I definitely agree that some people can over-engineer things, but we definitely have people who are already working within frameworks who want to see how Twilio can be used within it, so we try to cater for that too. Do let me know if there's anything I can do to help as you do start to use Twilio more though. – philnash May 07 '19 at 14:21
0
$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;
}   
gehlotparesh
  • 142
  • 3
  • 13