Conference call using twilio php

75 Views Asked by At

I using @twilio/voice-react-native-sdk in Mobile App side and php for server side. My aim is first clinetA start the call to clinetB then during the call clinetA or clinetB wants to add people in call using phone number. I am able to make the call between clinetA and clinetB.And also able to call phone number during call. My issue is my code is creating two calls. First clinetA to clinetB and second clinetA to phone number.I want all these or more in one conference call. I am new in twilio and PhP.I have some idea that I am creating normal call first time and then creating conference during the call using phone number. First time app will send the participant as string then will add participant as phone number.I am not sure how i should do here to make conference call between clinetA to ClinetB then adding same call a phone number.

if (!is_numeric($participantPhoneNumber)) participant is string clinetA to clinetB  
  <?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require __DIR__ . '/twilio-php-main/src/Twilio/autoload.php';

use Twilio\TwiML\VoiceResponse;
use Twilio\Rest\Client;

$accountSid = 'xxxxxxxxx';
$authToken = 'xxxxxxxxx';

$response = new VoiceResponse();
$callerNumber = '+447401120267';
$callerId = isset($_POST["callerId"]) ? $_POST["callerId"] : "";
$typeOfCall = isset($_POST["typeOfCall"]) ? $_POST["typeOfCall"] : "audio";
$minimumMinutes = isset($_POST["minimumMinutes"]) ? $_POST["minimumMinutes"] : "60";
$language = isset($_POST["language"]) ? $_POST["language"] : "Arabic";
$speciality = isset($_POST["speciality"]) ? $_POST["speciality"] : "Social Services";
$from = isset($_POST["from"]) ? $_POST["from"] : "";
$to = isset($_POST["to"]) ? $_POST["to"] : "";
$participantPhoneNumber = isset($_POST["participant"]) ? $_POST["participant"] : "";

if (!isset($to) || empty($to)) {
    $response->say('Hello');
} else {
    if (!is_numeric($participantPhoneNumber)) {
        // Initial call between clients
         
        $dial = $response->dial();
        $client = $dial->client($to); 
        $client->identity($to);
        $client->parameter(['name' => 'typeOfCall', 'value' => $typeOfCall]);
        $client->parameter(['name' => 'minimumMinutes', 'value' => $minimumMinutes]);
        $client->parameter(['name' => 'language', 'value' => $language]);
        $client->parameter(['name' => 'speciality', 'value' => $speciality]);
    } else {
        $callSid = isset($_POST["callSid"]) ? $_POST["callSid"] : "";
        // Conference call
        $conferenceOptions = [
            'startConferenceOnEnter' => true,
            'endConferenceOnExit' => true,
        ];
     
        $conferenceName = $to; // Replace with a unique conference name
        $conferenceDial = $response->dial();
        $conferenceDial->conference($conferenceName, $conferenceOptions);
        // Add participant to the conference
        if (isset($participantPhoneNumber) && !empty($participantPhoneNumber)) {
            $client = new Client($accountSid, $authToken);
            try {
                $participant = $client->conferences($conferenceName)->participants->create(
                    $callerNumber,
                    $participantPhoneNumber,
                    ["beep" => false]
                );
            } catch (Twilio\Exceptions\TwilioException $e) {
                echo $e->getMessage();
            }
        }
    }
}

header('Content-Type: text/xml');
print $response;
?>

I am able to make the call between clinetA and clinetB.And also able to call phone number during call. My issue is my code is creating two calls. First clinetA to clinetB and second clinetA to phone number.I want all these or more in one conference call.

1

There are 1 best solutions below

0
On

As a starting point, I'd suggest not making a Voicecall between the first and second caller. Instead, start off with creating a Conference.

The first caller will initiate the Conference using Conference Twiml. Twilio provides this PHP code for a Simple Conference.

The second caller can either be added to the Conference using Twiml (esentially reuse the same Twiml as the first call) or using the REST Api to create a Conference Participant.