phptwiliotwilio-phptwilio-click-to-call

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.

Solution

  • $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;
    }