Search code examples
twiliotwilio-phptwilio-api

Make a call between two numbers not registered in twilio


There's some way of make a call between two of my users? I mean... I have a twilio acount with a registered number and I have to make a call to my client "Bill" so when he answer that, the call should be redirected to another client, that Bill choosed, let's say "Joe". So, Bill click's a button and he's phone rings, he answer that and start to call Joe. When some of them hangup, the all call should be over. Have someone ever made that? Help me! And I'm sorry about my bad english (oh yeah, I'm using php for that)


Solution

  • This is just something simple to get you going, you can also look at connecting to a conference room https://www.twilio.com/docs/api/twiml/conference

    You will need to use the Twilio PHP Helper Library (the "Services" folder from there) download from https://www.twilio.com/docs/libraries/php#install-via-zip-file

    Project structure

    /
    /Services
    /callBill.php
    /callJoe.php
    

    callBill.php

    <?php
        header("content-type: text/xml");
        echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
    ?>
    
    <Response>
        <!-- // calling Bill -->
    </Response>
    
    <?php
        // Include the Twilio PHP library
        require 'Services/Twilio.php';
    
        // Twilio REST API version
        $version = "2010-04-01";
    
        // Set our Account SID and AuthToken
        $sid = 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
        $token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
    
        // A phone number you have at Twilio
        $phonenumber = '5557779999';
    
        // The URL Twilio will request when the call is answered            
        $twilioRequestUrl = "http://somewebsite.xyz/callJoe.php";
    
        // Instantiate a new Twilio Rest Client
        $client = new Services_Twilio($sid, $token, $version);
    
        try {
    
            // Initiate a new outbound call
            $call = $client->account->calls->create(
                $phonenumber, // The number of the phone initiating the call
                '7779995555', // call Bill at 7779995555
                $twilioRequestUrl 
            );
            //echo 'Started call: ' . $call->sid;
        } catch (Exception $e) {
            //echo 'Error: ' . $e->getMessage();
        }
    
    ?>
    

    callJoe.php

    <?php
    
        header("content-type: text/xml");
        echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
    ?>
    
    <!-- call and connect Joe at 9995557777 with Bill's caller ID-->
    <Response>
        <Pause length="2"/>
        <Say>Please wait, I'm calling Joe.</Say>
        <Dial callerId="7779995555">9995557777</Dial>
    </Response>
    

    Request http://somewebsite.xyz/callBill.php with your browser or with a click on a button.