<?php
// Web service configuration for SOAP endpoint
$wsdlUrl = 'https://api.thinktel.ca/soap.svc?singlewsdl';
$ucontrolUsername = 'username';
$ucontrolPassword = 'password';

// Single SOAP client for life of task
$client = new SoapClient($wsdlUrl, array('login' => $ucontrolUsername, 'password' => $ucontrolPassword, 'trace' => 1));

try
{
    // If you are choosing the SIP Trunk interactively, then first get the list of SIP Trunks from your account.
    $listSipTrunksResponse = $client->ListSipTrunks();
    $sipTrunks = $listSipTrunksResponse->ListSipTrunksResult->TerseNumber;

    // Choose specific SIP Trunk. For example purposes, we will assume the choice was SIP Trunk 9999990088.
    foreach ($sipTrunks as $st)
    {
        if ($st->Number === 9999990088)
        {
            $sipTrunk = $st;
            break;
        }
    }
    $sipTrunkNumber = $sipTrunk->Number;

    // If you are choosing the rate center interactively, then first get the list of rate centers with enough available inventory to fulfill the order.
    $listRateCentersResponse = $client->ListRateCenters();
    $rateCenters = array_filter($listRateCentersResponse->ListRateCentersResult->TerseRateCenter, function($rc)
    {
        return ($rc->Available >= 3);
    });

    // Choose specific rate center. For example purposes, we will assume the choice was "Toronto, ON".
    foreach ($rateCenters as $rc)
    {
        if ($rc->Name === 'Toronto, ON')
        {
            $rateCenter = $rc;
            break;
        }
    }
    $rateCenterName = $rateCenter->Name;

    // Order the next available 3 DIDs from chosen rate center.
    $addSipTrunkRateCenterDidsResponse = $client->AddSipTrunkRateCenterDids(array('number' => $sipTrunkNumber, 'ratecenters' => array(array(
        'RateCenterName' => $rateCenterName,
        'Quantity' => 3,
    ))));

    // Check response for what 3 DIDs were added, or for any exceptions
    foreach ($addSipTrunkRateCenterDidsResponse->AddSipTrunkRateCenterDidsResult->NumberResponse as $result)
    {
        if ($result->Reply == 'Accepted')
        {
            echo "Added DID {$result->Number} to SIP Trunk {$sipTrunkNumber}.", PHP_EOL;
            
            // Provide 911 information for recently added DID.
            $addV911Response = $client->AddV911(array('v911' => array(
                'Number' => $result->Number,
                // If providing a business name, leave FirstName blank and provide the business name as LastName.
                'FirstName' => '',
                'LastName' => 'Distributel',
                'SuiteNumber' => '801',
                'StreetNumber' => '3300',
                'StreetName' => 'Bloor St W',
                // Emergency address requires the specific municipal city name, not the general city name.
                'City' => 'Etobicoke',
                'ProvinceState' => 'ON',
                'PostalZip' => 'M8X2X2',
                // Optional information for emergency personal to access the physical location.
                // For non-municipal addresses, this where you could provide latitude and longitude coordinates.
                'OtherInfo' => 'Elevators require keycard access, see building security desk first.'
            )));
            if ($addV911Response->AddV911Result->Reply == 'Accepted')
            {
                echo "Added V911 information to telephone number {$addV911Response->AddV911Result->Number}.", PHP_EOL;
            }
            else
            {
                echo "V911 information for telephone number {$addV911Response->AddV911Result->Number} was not accepted: {$addV911Response->AddV911Result->Message}", PHP_EOL;
            }

            // Set additional settings for recently ordered DID, such as update the Label
            $updateSipTrunkDidV2Response = $client->UpdateSipTrunkDidV2(array('siptrunk' => $sipTrunkNumber, 'line' => array(
                'Number' => $result->Number,
                'LabelSpecified' => true,
                'Label' => 'Drop-in Desk',
                'AdditionalLabelSpecified' => false,
                'TranslatedNumberSpecified' => false,
                'UnavailableCallForwardingSpecified' => false
            )));
            if ($updateSipTrunkDidV2Response->UpdateSipTrunkDidV2Result->Reply == 'Accepted')
            {
                echo "Updated label for DID {$updateSipTrunkDidV2Response->UpdateSipTrunkDidV2Result->Number}.", PHP_EOL;
            }
            else
            {
                echo "Update for DID {$updateSipTrunkDidV2Response->UpdateSipTrunkDidV2Result->Number} was not accepted: {$updateSipTrunkDidV2Response->UpdateSipTrunkDidV2Result->Message}", PHP_EOL;
            }
        }
        else
        {
            echo "Order not accepted: {$result->Message}", PHP_EOL;
        }
    }
}
catch (SoapFault $fault)
{
    echo "Last SOAP Request: {$client->__getLastRequest()}", PHP_EOL;
    trigger_error("SOAP Fault: (faultcode: {$fault->faultcode}, faultstring: {$fault->faultstring})", E_USER_ERROR);
}
?>