Now Hiring: Are you a driven and motivated 1st Line IT Support Engineer?

Dec. 11, 2020

How to Download Zoom Cloud Recordings by PHP Program ?

The Zoom API is the primary means for developers to access a collection of resources from Zoom. Apps can read and write to the resources and mirror some of the most popular features available in  such as creating a new meeting, creating, adding and removing users, viewing reports and dashboards on various usage, and so on using the Zoom API. Depending on your app’s use case, you can choose from Zoom's various APIs and implement the features accordingly.

This is how Zoom's marketplace define the usage of API's provided by Zoom. And indeed using those API's really helpful to reduce back-office operations for most of the companies.

For Cihangir Yoga / Istanbul  , we integrate their portals with Zoom thru API's and automate meeting creation for scheduled sessions, implement Zoom Web Client in class detail and when the session start users can watch the session thru this web client under the brand of Cihangir Yoga.

Our next challenge was to download cloud recordings to more cost effective storage and display these videos also on Web pages of the company. 

So, how we can automize the process without the need for manual intervention ? 

We implemented a batch job by PHP which checks the class sessions already ended for ready recordings. If recordings are available, job get the Download_URL of the recording by using RecordingGET API of Zoom.

{
    "id""0b0f6f38-da32-4bb2-857b-decc734df16f",
    "meeting_id""9tE+eJCMRg+DuDKGu+b+dQ==",
    "recording_start""2020-12-10T13:46:47Z",
    "recording_end""2020-12-10T15:03:40Z",
    "file_type""MP4",
    "file_size"218902740,
    "play_url""https://cihangiryoga.zoom.us/rec/play/2NNUNfmyPg0gAMsC1lvK9-uEqz_t4UWJh0Y6bfl6BWc-IjBYWLQV14aRgxr-FYm9n5c0ew16d4on3Xwe.Weu1Ne5vH1ZfMjEW",
    "download_url""https://cihangiryoga.zoom.us/rec/download/2NNUNfmyPg0gAMsC1lvK9-uEqz_t4UWJh0Y6bfl6BWc-IjBYWLQV14aRgxr-FYm9n5c0ew16d4on3Xwe.Weu1Ne5vH1ZfMjEW",
    "status""completed",
    "recording_type""shared_screen_with_speaker_view"
}

After that by using CURL GET request we can download the files to any location on our storage area. 

But there are some tricks here. You probably get many errors while try to download these media files. Don't worry, we will give you the answers... 

Now, here is the PHP code to send GET request :

$ch = curl_init();
header('Content-Disposition: attachment; filename="'.$recfile->getId().'.mp4"');

// set URL and other appropriate options
$fp = fopen('public/upload/zoomvideos/'.$recfile->getId().'.mp4', 'w+');
curl_setopt($ch, CURLOPT_URL, $recfile->getDownloadUrl().'?access_token='.$authorization);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 50);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/octet-stream',
'Content-Description: File Transfer',
]);
curl_exec($ch);
fclose($fp);

First of all all the recordings by default can only be downloaded by host of meeting only. So if you don't enable "Viewers Can Download" property on Zoom portal you will get the following error each time you try:

{“status”:false,“errorCode”:401,“errorMessage”:“Forbidded"}

So, programmatically you have to tell Zoom that you are either host or acting on behalf of host. So to do that you have to set "access_token" at each time when you send the request. 

curl_setopt($ch, CURLOPT_URL, $recfile->getDownloadUrl().'?access_token='.$authorization);

But be careful at that point. If you also set "Authorization" token at header part you will get the same error. You only need that access_token nothing more. These JWT token has to be created each time when the request send.  

If you send the request with the same token you use before you will get the following error, so be careful:

{"status":false,"errorCode":124,"errorMessage":"Forbidden"}

If you wonder how to generate JWT token, here is a function for that also:

use Firebase\JWT\JWT;
protected function generateJWT()
{
$token = [
'iss' => $this::ZOOM_API_KEY,
'exp' => time() + 60,
];

return JWT::encode($token, $this::ZOOM_API_SECRET);
}

By that method in a loop of batch job you can download as many as cloud recordings you need. 

I hope that helps you all guys. 

If you have any questions, write as comments...

Stay At Home, Stay Healthy !




3 Comments
Leave a Comment
captcha