PHP code example of nextgenswitch / nextgenswitch-php

1. Go to this page and download the library: Download nextgenswitch/nextgenswitch-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nextgenswitch / nextgenswitch-php example snippets


use NextGenSwitch\Client;

$client = new Client(
    baseUrl: getenv('NEXTGENSWITCH_BASE_URL'),
    authorizationCode: getenv('NEXTGENSWITCH_AUTHORIZATION'),
    authorizationSecret: getenv('NEXTGENSWITCH_AUTHORIZATION_SECRET'),
);

use NextGenSwitch\VoiceResponse;

$voice = (new VoiceResponse())
    ->say('Welcome to NextGenSwitch.')
    ->gather(
        [
            'action' => 'https://example.com/gather',
            'method' => 'POST',
            'numDigits' => 1,
            'timeout' => 10,
        ],
        static fn ($gather) => $gather->say('Press one for sales.'),
    );

$result = $client->createCall(
    to: '2001',
    from: '1001',
    responseXml: $voice,
    statusCallback: 'https://example.com/call-status',
);

$call = $result->data();

$result = $client->createCall(
    to: '2001',
    from: '1001',
    responseUrl: 'https://example.com/call-flow.xml',
);

$updatedFlow = (new VoiceResponse())
    ->pause(2)
    ->say('Your call flow has been updated.')
    ->dial('1000');

$client->modifyCall('CALL-123', $updatedFlow);

$response = (new VoiceResponse())
    ->say('This is a test.', ['loop' => 2])
    ->play('https://example.com/audio.mp3', ['loop' => 1])
    ->record([
        'action' => 'https://example.com/recording',
        'method' => 'POST',
        'timeout' => 5,
        'finishOnKey' => '#',
        'beep' => true,
    ])
    ->hangup();

header('Content-Type: application/xml; charset=UTF-8');
echo $response->xml();

$response = (new VoiceResponse())->stream(
    url: 'wss://voice.example.com/session',
    parameters: [
        'session_id' => 'session-123',
        'tenant' => 'example',
    ],
    attributes: ['name' => 'assistant-stream'],
);

use NextGenSwitch\Webhook\GatherResult;

$gather = GatherResult::fromArray($_POST);

if ($gather->digits === '1') {
    echo (new VoiceResponse())->dial('1001')->xml();
} else {
    echo (new VoiceResponse())->say('No valid selection received.')->hangup()->xml();
}

use NextGenSwitch\Exception\ApiException;

try {
    $client->modifyCall('CALL-123', (new VoiceResponse())->hangup());
} catch (ApiException $error) {
    error_log($error->statusCode() . ': ' . $error->getMessage());
}
bash
composer 
bash
php examples/create-call.php
php examples/record-call.php
php examples/modify-call.php CALL-123