sparqapi/api/application/libraries/Twlio.php
2020-09-22 01:50:14 +05:30

187 lines
4.6 KiB
PHP

<?php defined('BASEPATH') OR exit('No direct script access allowed');
include_once APPPATH.'/vendor/autoload.php';
use Twilio\Rest\Client;
use Twilio\Jwt\AccessToken;
use Twilio\Jwt\Grants\VideoGrant;
class Twlio
{
function getServerInfo($print = false,$json = false)
{
$twilio = new Client(TWLIO_SID, TWLIO_AUTH_TOKEN);
$token = $twilio->tokens
->create();
$response_data = array();
$response_data['username'] = $token->username;
$response_data['ice_servers'] = $token->iceServers;
$response_data['date_updated'] = $token->dateUpdated;
$response_data['ttl'] = $token->ttl;
$response_data['date_created'] = $token->dateCreated;
$response_data['password'] = $token->password;
$response_data['accountSid'] = $token->accountSid;
if($print)
{
$json ? print_r(json_encode($response_data,true)) : print_r($response_data);
}
else
{
return json_encode($response_data,true);
}
}
function getAPIKey($print = false,$json = false)
{
$twilio = new Client(TWLIO_SID, TWLIO_AUTH_TOKEN);
$new_key = $twilio->newKeys->create();
return array('apikey' => $new_key->sid,'apisecret' => $new_key->secret);
// print_r($new_key->dateCreated);
// print_r($new_key->secret);
// print_r($new_key->friendyName);
// try
// {
// if(is_object($new_key) && property_exists($new_key, 'secret'))
// {
// return $new_key->secret;
// }
// else
// {
// throw new Exception("Couldn't get API Key from Twlio", 1);
// }
// }
// catch(Exception $e)
// {
// // print_r($e->getMessage());
// $data['dataStatus'] = false;
// $data['status'] = REST_Controller::HTTP_INTERNAL_SERVER_ERROR;
// $data['msg'] = $e->getMessage();
// $this->response($data,REST_Controller::HTTP_OK);
// }
}
function getVideoAccessJWTToken($apiKey,$apiSecret,$roomName,$officerName)
{
// Required for all Twilio access tokens
$twilioAccountSid = TWLIO_SID;
$twilioApiKey = $apiKey;
$twilioApiSecret = $apiSecret;
// Required for Video grant
$roomName = $roomName;
// An identifier for your app - can be anything you'd like
$identity = $officerName;
// Create access token, which we will serialize and send to the client
$token = new AccessToken(
$twilioAccountSid,
$twilioApiKey,
$twilioApiSecret,
3600,
$identity
);
// Create Video grant
$videoGrant = new VideoGrant();
$videoGrant->setRoom($roomName);
// Add grant to token
$token->addGrant($videoGrant);
// render token to string
return $token->toJWT();
}
function listOfRoomsByRoomName($roomName)
{
echo $roomName;
$sid = TWLIO_SID;
$token = TWLIO_AUTH_TOKEN;
$twilio = new Client($sid, $token);
$rooms = $twilio->video->v1->rooms
->read(["uniqueName" => $roomName,"status" => "completed"], 20);
// print_r($rooms);die()
foreach ($rooms as $record) {
print($record);
}
}
function getRoomDataByRoomID($roomID)
{
//echo $roomName;
$sid = TWLIO_SID;
$token = TWLIO_AUTH_TOKEN;
$twilio = new Client($sid, $token);
//$rooms = $twilio->video->v1->rooms
// ->read(["uniqueName" => $roomName], 20);
// print_r($rooms);
$room = $twilio->video->v1->rooms($roomID)
->fetch();
print($room->uniqueName);
print($room->accountSid);
print($room->type);
print($room->status);
print($room->status);
// foreach ($rooms as $record) {
// print($record->sid);
// }
}
// 'sid
// 'status
// 'dateCreated
// 'dateUpdated
// 'accountSid
// 'enableTurn
// 'uniqueName
// 'statusCallback
// 'statusCallbackMethod
// 'endTime
// 'duration
// 'type
// 'maxParticipants
// 'recordParticipantsOnConnect
// 'videoCodecs
// 'mediaRegion
// 'url
// 'links
function startComposistion($roomID)
{
$apiKeySid = TWILIO_API_KEY;
$apiKeySecret = TWILIO_API_KEY_SECRET;
$client = new Client($apiKeySid, $apiKeySecret);
$composition = $client->video->compositions->create($roomID, [
'audioSources' => '*',
'videoLayout' => array(
'grid' => array (
'video_sources' => array('*')
)
),
// 'statusCallback' => 'http://my.server.org/callbacks',
'format' => 'mp4'
]);
print($composition);
echo $composition->sid;
}
}
?>