Back to Blog
Language How-Tos

How to Forward Calls to Another Number in Twilio Using PHP

Build a PHP call forwarding webhook using TwiML Dial to route inbound Twilio calls to different numbers based on business rules.

DA
Danial A
Senior Twilio Consultant, Telphi Consulting
June 22, 2026
8 min read
Twilio
PHP
How-To
Code
How to Forward Calls to Another Number in Twilio Using PHP

This tutorial builds a PHP call forwarding system that routes inbound Twilio calls to different phone numbers based on the current time, day of week, or other configurable business rules. It targets PHP developers who need intelligent call routing without a traditional PBX, such as forwarding to a mobile number after hours or distributing calls across a small team. You will build a PHP webhook that checks business hours and returns a TwiML Dial verb targeting the appropriate forwarding destination with a no-answer fallback.

Prerequisites and Setup

Install the Twilio SDK with composer require twilio/sdk vlucas/phpdotenv and set your Twilio number's voice webhook to POST to your forward.php script in the Twilio Console. Define your forwarding numbers and business hours as PHP constants or environment variables at the top of the script so they can be updated without touching the routing logic. PHP's built-in date() function with a configured timezone handles the business hours check; set date_default_timezone_set('America/New_York') at the top of the script for consistent results regardless of where the server is hosted. A no-answer action URL on the Dial verb handles the case where the forwarded call goes unanswered.

composer require twilio/sdk vlucas/phpdotenv

# .env
TWILIO_PHONE_NUMBER=+15551234567
OFFICE_NUMBER=+15550001111
MOBILE_NUMBER=+15550002222

The Complete Code

The isBusinessHours() function uses PHP's date('H') and date('N') to check whether the current time falls within weekday business hours in the configured timezone. During business hours the Dial verb targets the office number with a 20-second timeout; outside hours it targets the mobile number with a brief after-hours announcement. The action attribute on the Dial verb points to no-answer.php, which handles the case where the forwarded call is not answered within the timeout. The no-answer.php script checks $_POST['DialCallStatus'] and plays a voicemail prompt if the status indicates the call was not answered.

<?php
// forward.php
require_once 'vendor/autoload.php';
use TwilioTwiMLVoiceResponse;

$dotenv = DotenvDotenv::createImmutable(__DIR__);
$dotenv->load();

date_default_timezone_set('America/New_York');

function isBusinessHours(): bool {
    $hour = (int) date('H');
    $day  = (int) date('N'); // 1 = Mon, 7 = Sun
    return $day >= 1 && $day <= 5 && $hour >= 9 && $hour < 18;
}

$from     = $_ENV['TWILIO_PHONE_NUMBER'];
$office   = $_ENV['OFFICE_NUMBER'];
$mobile   = $_ENV['MOBILE_NUMBER'];

$response = new VoiceResponse();

if (isBusinessHours()) {
    $dial = $response->dial(['timeout' => 20, 'action' => '/no-answer.php', 'callerId' => $from]);
    $dial->number($office);
} else {
    $response->say('Our office is closed. Forwarding you to our after-hours line.', ['voice' => 'alice']);
    $dial = $response->dial(['timeout' => 30, 'action' => '/no-answer.php', 'callerId' => $from]);
    $dial->number($mobile);
}

header('Content-Type: text/xml');
echo $response;
<?php
// no-answer.php
require_once 'vendor/autoload.php';
use TwilioTwiMLVoiceResponse;

$dialStatus = $_POST['DialCallStatus'] ?? '';
$response   = new VoiceResponse();

if (in_array($dialStatus, ['no-answer', 'busy', 'failed'], true)) {
    $response->say(
        'We are unable to take your call. Please leave a message after the tone.',
        ['voice' => 'alice']
    );
    $response->record([
        'timeout'             => 10,
        'transcribe'          => true,
        'transcribeCallback'  => '/transcription.php',
    ]);
}

$response->hangup();
header('Content-Type: text/xml');
echo $response;

Testing and Debugging

Call your Twilio number during configured business hours and verify the call forwards to the office number; call again outside business hours and confirm it routes to the mobile number with the after-hours announcement. Temporarily change the business hours condition to always return true or false to test each branch without waiting for the actual time of day. Test the no-answer fallback by having the destination phone reject the call and verifying the voicemail prompt plays. Check the Twilio Console Call Logs to view the exact TwiML fetched and the HTTP response returned at each step of the forwarding flow.

Taking It Further

Load the business hours configuration and agent numbers from a database so operations staff can update routing through an admin panel without editing PHP files. Add a holiday schedule stored in a database table and check it before the business hours logic so calls on public holidays route to mobile or voicemail even during normal working hours. Implement simultaneous ring by adding multiple number() calls inside the dial() block so the call rings all team members at once and connects to whoever answers first. Combine this forwarding logic with the IVR tutorial to let callers choose their department before being forwarded to the relevant team member's direct number.

Contact Telphi Consulting to design an intelligent PHP call routing system with CRM integration, analytics, and real-time reporting.

Share this article:
0 views

Ready to Transform Your Business Communications?

Get a free consultation with our VoIP experts and discover how we can help you save costs, improve efficiency, and scale your business.

Comments (0)

Join the discussion and share your thoughts (AI-moderated for quality)

Protected by AI moderation

Be the first to comment

No comments yet. Share your thoughts below.