sparqapi/api/application/libraries/Twlio.php
2020-09-15 23:54:11 +05:30

111 lines
2.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 $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($apiKeySecret,$roomName,$officerName)
{
// Required for all Twilio access tokens
$twilioAccountSid = TWLIO_SID;
$twilioApiKey = TWLIO_AUTH_TOKEN;
$twilioApiSecret = $apiKeySecret;
// 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();
}
}
?>