From 79fb87c61c7b6da9eeb492c31e68ccde3b0553c5 Mon Sep 17 00:00:00 2001 From: SushilMallRC Date: Mon, 5 Aug 2024 13:33:36 +0530 Subject: [PATCH] Remove username andd password from Demo files and added support of JWT --- demo/_credentialsSample.php | 14 +++--- demo/authData.php | 6 +-- demo/callRecording.php | 91 +++++++++++++++++++------------------ demo/doubleRefresh.php | 6 +-- demo/errorHandling.php | 8 ++-- demo/extensions.php | 8 ++-- demo/fax.php | 26 ++++++----- demo/mms.php | 24 +++++----- demo/ringout.php | 11 +++-- demo/sms.php | 11 +++-- demo/subscription.php | 18 ++++---- demo/webhook.php | 9 ++-- 12 files changed, 120 insertions(+), 112 deletions(-) diff --git a/demo/_credentialsSample.php b/demo/_credentialsSample.php index 73417af9..adef4c91 100644 --- a/demo/_credentialsSample.php +++ b/demo/_credentialsSample.php @@ -1,14 +1,12 @@ '18881112233', // your RingCentral account phone number - 'extension' => null, // or number - 'password' => 'yourPassword', - 'clientId' => 'yourClientId', + 'clientId' => 'yourClientId', 'clientSecret' => 'yourClientSecret', - 'server' => 'https://platform.ringcentral.com', // for production - https://platform.ringcentral.com - 'smsNumber' => '18882223344', // any of SMS-enabled numbers on your RingCentral account + 'server' => 'https://platform.ringcentral.com', // for production - https://platform.ringcentral.com + 'smsNumber' => '18882223344', // any of SMS-enabled numbers on your RingCentral account 'mobileNumber' => '16501112233', // your own mobile number to which script will send sms - 'dateFrom' => 'yyyy-mm-dd', // dateFrom - 'dateTo' => 'yyyy-mm-dd' // dateTo + 'dateFrom' => 'yyyy-mm-dd', // dateFrom + 'dateTo' => 'yyyy-mm-dd', // dateTo + "RC_JWT" => 'JWT' ); \ No newline at end of file diff --git a/demo/authData.php b/demo/authData.php index 09ff4a0c..bf815b83 100644 --- a/demo/authData.php +++ b/demo/authData.php @@ -1,12 +1,12 @@ getMessage() . PHP_EOL; - $auth = $platform->login($credentials['username'], $credentials['extension'], $credentials['password']); + $auth = $platform->login(["jwt" => $credentials['RC_JWT']]); print 'Authorized' . PHP_EOL; diff --git a/demo/callRecording.php b/demo/callRecording.php index d464deb4..b0daaafc 100644 --- a/demo/callRecording.php +++ b/demo/callRecording.php @@ -1,6 +1,6 @@ login($credentials['username'], $credentials['extension'], $credentials['password'], true); +$platform->login(["jwt" => $credentials['RC_JWT']]); // Find call log records with recordings $callLogRecords = $platform->get('/account/~/extension/~/call-log', array( - 'type' => 'Voice', - 'withRecording' => 'True', - 'dateFrom' => $credentials['dateFrom'], - 'dateTo' => $credentials['dateTo'])) - ->json()->records; + 'type' => 'Voice', + 'withRecording' => 'True', + 'dateFrom' => $credentials['dateFrom'], + 'dateTo' => $credentials['dateTo'] +) +) + ->json()->records; // Create a CSV file to log the records - $status = "Success"; - $dir = $credentials['dateFrom']; - $fname = "recordings_${dir}.csv"; - $fdir = "/Recordings/${dir}"; +$status = "Success"; +$dir = $credentials['dateFrom']; +$fname = "recordings_${dir}.csv"; +$fdir = "/Recordings/${dir}"; - if (is_dir($fdir) === false) - { - mkdir($fdir, 0777, true); - } +if (is_dir($fdir) === false) { + mkdir($fdir, 0777, true); +} - $file = fopen($fname,'w'); +$file = fopen($fname, 'w'); - $fileHeaders = array("RecordingID","ContentURI","Filename","DownloadStatus"); +$fileHeaders = array("RecordingID", "ContentURI", "Filename", "DownloadStatus"); - fputcsv($file, $fileHeaders); +fputcsv($file, $fileHeaders); - $fileContents = array(); +$fileContents = array(); - $timePerRecording = 6; +$timePerRecording = 6; - foreach ($callLogRecords as $i => $callLogRecord) { +foreach ($callLogRecords as $i => $callLogRecord) { - $id = $callLogRecord->recording->id; + $id = $callLogRecord->recording->id; - $uri = $callLogRecord->recording->contentUri; + $uri = $callLogRecord->recording->contentUri; - $apiResponse = $platform->get($callLogRecord->recording->contentUri); + $apiResponse = $platform->get($callLogRecord->recording->contentUri); - $ext = ($apiResponse->response()->getHeader('Content-Type')[0] == 'audio/mpeg') - ? 'mp3' : 'wav'; + $ext = ($apiResponse->response()->getHeader('Content-Type')[0] == 'audio/mpeg') + ? 'mp3' : 'wav'; - $start = microtime(true); + $start = microtime(true); - file_put_contents("${fdir}/recording_${id}.${ext}", $apiResponse->raw()); + file_put_contents("${fdir}/recording_${id}.${ext}", $apiResponse->raw()); - $filename = "recording_${id}.${ext}"; + $filename = "recording_${id}.${ext}"; - if(filesize("${fdir}/recording_${id}.${ext}") == 0) { - $status = "failure"; - } + if (filesize("${fdir}/recording_${id}.${ext}") == 0) { + $status = "failure"; + } - file_put_contents("${fdir}/recording_${id}.json", json_encode($callLogRecord)); + file_put_contents("${fdir}/recording_${id}.json", json_encode($callLogRecord)); - $end=microtime(true); + $end = microtime(true); - // Check if the recording completed wihtin 6 seconds. - $time = ($end*1000 - $start * 1000); - if($time < $timePerRecording) { - sleep($timePerRecording-$time); - } + // Check if the recording completed wihtin 6 seconds. + $time = ($end * 1000 - $start * 1000); + if ($time < $timePerRecording) { + sleep($timePerRecording - $time); + } - $fileContents = array($id, $uri, $filename, $status); - fputcsv($file, $fileContents); + $fileContents = array($id, $uri, $filename, $status); + fputcsv($file, $fileContents); - } +} - fclose($file); +fclose($file); -?> +?> \ No newline at end of file diff --git a/demo/doubleRefresh.php b/demo/doubleRefresh.php index 9932e63f..806fc57b 100644 --- a/demo/doubleRefresh.php +++ b/demo/doubleRefresh.php @@ -1,12 +1,12 @@ platform(); $platform2 = $rcsdk2->platform(); -$platform1->login($credentials['username'], $credentials['extension'], $credentials['password']); +$platform1->login(["jwt" => $credentials['RC_JWT']]); print 'Platform1 Authorized' . PHP_EOL; diff --git a/demo/errorHandling.php b/demo/errorHandling.php index 1d3bf6d4..da16ef85 100644 --- a/demo/errorHandling.php +++ b/demo/errorHandling.php @@ -1,6 +1,6 @@ login($credentials['username'], $credentials['extension'], $credentials['password']); +$platform->login(["jwt" => $credentials['RC_JWT']]); // Load something nonexistent @@ -26,7 +26,7 @@ } catch (ApiException $e) { - $message = $e->getMessage() . ' (from backend) at URL ' . (string)$e->apiResponse()->request()->getUri(); + $message = $e->getMessage() . ' (from backend) at URL ' . (string) $e->apiResponse()->request()->getUri(); print 'Expected HTTP Error: ' . $message . PHP_EOL; diff --git a/demo/extensions.php b/demo/extensions.php index 7f82f705..3525a0fd 100644 --- a/demo/extensions.php +++ b/demo/extensions.php @@ -1,12 +1,12 @@ login($credentials['username'], $credentials['extension'], $credentials['password']); +$platform->login(["jwt" => $credentials['RC_JWT']]); // Load extensions @@ -25,7 +25,7 @@ // Load presence $presences = $platform->get('/account/~/extension/' . $extensions[0]->id . ',' . $extensions[0]->id . '/presence') - ->multipart(); + ->multipart(); print 'Presence loaded ' . $extensions[0]->name . ' - ' . $presences[0]->json()->presenceStatus . ', ' . diff --git a/demo/fax.php b/demo/fax.php index 554052a9..acae5877 100644 --- a/demo/fax.php +++ b/demo/fax.php @@ -1,12 +1,12 @@ login($credentials['username'], $credentials['extension'], $credentials['password']); +$platform->login(["jwt" => $credentials['RC_JWT']]); // Send Fax $request = $rcsdk->createMultipartBuilder() - ->setBody(array( - 'to' => array( - array('phoneNumber' => $credentials['username']), - ), - 'faxResolution' => 'High', - )) - ->add('Plain Text', 'file.txt') - ->add(fopen('https://developers.ringcentral.com/assets/images/ico_case_crm.png', 'r')) - ->request('/account/~/extension/~/fax'); + ->setBody( + array( + 'to' => array( + array('phoneNumber' => $credentials['username']), + ), + 'faxResolution' => 'High', + ) + ) + ->add('Plain Text', 'file.txt') + ->add(fopen('https://developers.ringcentral.com/assets/images/ico_case_crm.png', 'r')) + ->request('/account/~/extension/~/fax'); //print $request->getBody() . PHP_EOL; diff --git a/demo/mms.php b/demo/mms.php index fca292ed..0aa488b4 100644 --- a/demo/mms.php +++ b/demo/mms.php @@ -1,12 +1,12 @@ login($credentials['username'], $credentials['extension'], $credentials['password']); +$platform->login(["jwt" => $credentials['RC_JWT']]); // Find SMS-enabled phone number that belongs to extension @@ -39,14 +39,16 @@ // Send Fax $request = $rcsdk->createMultipartBuilder() - ->setBody(array( - 'from' => array('phoneNumber' => $smsNumber), - 'to' => array( - array('phoneNumber' => $credentials['mobileNumber']), - ), - )) - ->add(fopen('https://developers.ringcentral.com/assets/images/ico_case_crm.png', 'r')) - ->request('/account/~/extension/~/sms'); + ->setBody( + array( + 'from' => array('phoneNumber' => $smsNumber), + 'to' => array( + array('phoneNumber' => $credentials['mobileNumber']), + ), + ) + ) + ->add(fopen('https://developers.ringcentral.com/assets/images/ico_case_crm.png', 'r')) + ->request('/account/~/extension/~/sms'); //print $request->getBody() . PHP_EOL; diff --git a/demo/ringout.php b/demo/ringout.php index b19ff282..438cc590 100644 --- a/demo/ringout.php +++ b/demo/ringout.php @@ -1,13 +1,13 @@ login($credentials['username'], $credentials['extension'], $credentials['password']); +$platform->login(["jwt" => $credentials['RC_JWT']]); // Make a call $response = $platform->post('/account/~/extension/~/ringout', array( 'from' => array('phoneNumber' => $credentials['fromPhoneNumber']), - 'to' => array('phoneNumber' => $credentials['toPhoneNumber']) -)); + 'to' => array('phoneNumber' => $credentials['toPhoneNumber']) +) +); $json = $response->json(); diff --git a/demo/sms.php b/demo/sms.php index 214a4884..adc4a655 100644 --- a/demo/sms.php +++ b/demo/sms.php @@ -1,13 +1,13 @@ login($credentials['username'], $credentials['extension'], $credentials['password']); +$platform->login(["jwt" => $credentials['RC_JWT']]); // Find SMS-enabled phone number that belongs to extension @@ -44,11 +44,12 @@ $response = $platform ->post('/account/~/extension/~/sms', array( 'from' => array('phoneNumber' => $smsNumber), - 'to' => array( + 'to' => array( array('phoneNumber' => $credentials['mobileNumber']), ), 'text' => 'Test from PHP', - )); + ) + ); print 'Sent SMS ' . $response->json()->uri . PHP_EOL; diff --git a/demo/subscription.php b/demo/subscription.php index 8e6c48f1..6d40265b 100644 --- a/demo/subscription.php +++ b/demo/subscription.php @@ -1,6 +1,6 @@ login($credentials['username'], $credentials['extension'], $credentials['password']); +$platform->login(["jwt" => $credentials['RC_JWT']]); // Subscription $subscription = $rcsdk->createSubscription('Pubnub'); -$subscription->addEvents(array( - '/account/~/extension/~/message-store', - '/account/~/extension/~/presence' -)); +$subscription->addEvents( + array( + '/account/~/extension/~/message-store', + '/account/~/extension/~/presence' + ) +); $subscription->setKeepPolling(true); diff --git a/demo/webhook.php b/demo/webhook.php index b56a93be..eb9486ac 100644 --- a/demo/webhook.php +++ b/demo/webhook.php @@ -1,13 +1,13 @@ login($credentials['username'], $credentials['extension'], $credentials['password']); +$platform->login(["jwt" => $credentials['RC_JWT']]); // Find SMS-enabled phone number that belongs to extension @@ -28,6 +28,7 @@ 'transportType' => 'WebHook', 'address' => $credentials['webhookUri'] ) -)); +) +); print 'Webhook Subscription ' . $response->json() . PHP_EOL;