Tập tành How to Create Zoom Meetings with PHP and JWT

29th Jun 2022
Table of contents

In the past, I have written an article that explains creating a Zoom Meeting with PHP and OAuth. One of our readers asked about creating a Zoom meeting using JWT(JSON Web Tokens) and PHP. Zoom API allows us to use both OAuth or JWT to deal with their APIs. The Zoom API requires the Bearer Token generated either through OAuth or JWT.

Note: JWT may only be used for internal applications and processes. All apps created for third-party usage must use OAuth app type.

When you want to build a Zoom app for internal use then the JWT approach is easier compared to OAuth.

Getting Started

For getting started, go to the Zoom Developer Dashboard and create a new app. Choose JWT as the app type and copy the Zoom API key and secret.

Zoom JWT App

A single JWT consists of three components: Header, Payload, and Signature with a . separating each. For example: aaaaa.bbbbb.ccccc

Several libraries are available that help to generate JWT. I will recommend using the firebase/php-jwt library. This library provides a much easier approach to create JWT. Install this library using the below command.

composer require firebase/php-jwt

Next, we have to send a POST request to the Zoom API to create a meeting. One can use cURL for this purpose. But, I personally like a Guzzle which provides much cleaner code than cURL. Another benefit of using Guzzle is you don’t need to have cURL extension enabled on the server.

Run the below command to install the Guzzle library.

composer require guzzlehttp/guzzle

Create Zoom Meetings with JWT and PHP

We have installed two packages to achieve our goal. Now, we need to include it’s environment in our PHP file. Also, we have copied Zoom API keys in the previous steps. Let’s define these keys as constants in PHP.

<?php
require_once 'vendor/autoload.php';
 
use \Firebase\JWT\JWT;
use GuzzleHttp\Client;
 
define('ZOOM_API_KEY', 'API_KEY_HERE');
define('ZOOM_SECRET_KEY', 'API_SECRET_HERE');

Next, to generate JWT using the firebase/php-jwt package our code will be as follows.

function getZoomAccessToken() {
    $key = ZOOM_SECRET_KEY;
    $payload = array(
        "iss" => ZOOM_API_KEY,
        'exp' => time() + 3600,
    );
    return JWT::encode($payload, $key);    
}

Finally, to create a Zoom meeting it needs to send POST request to the endpoint /v2/users/me/meetings with JWT as a Bearer Token.

function createZoomMeeting() {
    $client = new Client([
        // Base URI is used with relative requests
        'base_uri' => 'https://api.zoom.us',
    ]);
 
    $response = $client->request('POST', '/v2/users/me/meetings', [
        "headers" => [
            "Authorization" => "Bearer " . getZoomAccessToken()
        ],
        'json' => [
            "topic" => "Let's Learn WordPress",
            "type" => 2,
            "start_time" => "2021-01-30T20:30:00",
            "duration" => "30", // 30 mins
            "password" => "123456"
        ],
    ]);
 
    $data = json_decode($response->getBody());
    echo "Join URL: ". $data->join_url;
    echo "<br>";
    echo "Meeting Password: ". $data->password;
}
 
createZoomMeeting();

Update Zoom Meeting

It may happen that you want to update the meeting information like duration, date, or topic. In that case, you need to send a PATCH request along with the details to be updated. You can use the below code to update your Zoom meeting. For more information, refer to the documentation.

function updateZoomMeeting($meeting_id) {
    $client = new Client([
        // Base URI is used with relative requests
        'base_uri' => 'https://api.zoom.us',
    ]);
 
    $response = $client->request('PATCH', '/v2/meetings/'.$meeting_id, [
        "headers" => [
            "Authorization" => "Bearer " . getZoomAccessToken()
        ],
        'json' => [
            "topic" => "Let's Learn Laravel",
            "type" => 2,
            "start_time" => "2021-07-20T10:30:00",
            "duration" => "45", // 45 mins
            "password" => "123456"
        ],
    ]);
    if (204 == $response->getStatusCode()) {
        echo "Meeting is updated successfully.";
    }
}
 
updateZoomMeeting('MEETING_ID');

Upon successful execution, you get the 204 as HTTP status code.

List Zoom Meetings

In your application, you may want to list all Zoom meetings. Zoom provides an API through which we can get a list of all Zoom meetings. Using the code below, you can print the details of your Zoom meetings.

function list_meetings($next_page_token = '') {
    $client = new GuzzleHttp\Client(['base_uri' => 'https://api.zoom.us']);
 
    $arr_request = [
        "headers" => [
            "Authorization" => "Bearer ". getZoomAccessToken()
        ]
    ];
 
    if (!empty($next_page_token)) {
        $arr_request['query'] = ["next_page_token" => $next_page_token];
    }
 
    $response = $client->request('GET', '/v2/users/me/meetings', $arr_request);
     
    $data = json_decode($response->getBody());
 
    if ( !empty($data) ) {
        foreach ( $data->meetings as $d ) {
            $topic = $d->topic;
            $join_url = $d->join_url;
            echo "<h3>Topic: $topic</h3>";
            echo "Join URL: $join_url";
        }
 
        if ( !empty($data->next_page_token) ) {
            list_meetings($data->next_page_token);
        }
    }
}
 
list_meetings();

Here, I am printing a topic and URL of meetings. You may also print other information. Print the variable $data to get a list of available information.

By default, Zoom returns a list of 30 meetings in a single call. If you want to get the next set of records, use the next_page_token value as shown in the code.

Get Past Meeting Participants

For the past meetings, you can get a list of participants using the Zoom API. If you are on a paid account then you can use this specific API. This API requires to have paid account. If you try to call this API with a free account you would get an error.

$client = new GuzzleHttp\Client(['base_uri' => 'https://api.zoom.us']);
 
$response = $client->request('GET', '/v2/past_meetings/MEETING_ID/participants', [
    "headers" => [
        "Authorization" => "Bearer ". getZoomAccessToken()
    ]
]);
 
$data = json_decode($response->getBody());
if ( !empty($data) ) {
    foreach ( $data->participants as $p ) {
        $name = $p->name;
        $email = $p->user_email;
        echo "Name: $name";
        echo "Email: $email";
    }
}

Replace the placeholder MEETING_ID with the actual past meeting id. In the response, you will get the names and emails of participants.

Delete Zoom Meeting with JWT and PHP

In adding to create a meeting, Zoom also provides API endpoints like list, update, delete a meeting. The user has to follow their API documentation for using a specific endpoint. As an example, you can delete a meeting by sending a DELETE request to the Zoom API endpoint. You need to pass your meeting id to the endpoint as follows.

function deleteZoomMeeting($meeting_id) {
    $client = new Client([
        // Base URI is used with relative requests
        'base_uri' => 'https://api.zoom.us',
    ]);
 
    $response = $client->request("DELETE", "/v2/meetings/$meeting_id", [
        "headers" => [
            "Authorization" => "Bearer " . getZoomAccessToken()
        ]
    ]);
 
    if (204 == $response->getStatusCode()) {
        echo "Meeting deleted.";
    }
}
 
deleteZoomMeeting('MEETING_ID_HERE');

I hope you got to know about creating Zoom meetings with PHP and JWT. Please share your thoughts and suggestions in the comment section below.

Attach
Bạn thấy bài viết này như thế nào?
0 reactions

Add new comment

Image CAPTCHA
Enter the characters shown in the image.
Câu nói tâm đắc: “Điều tuyệt với nhất trong cuộc sống là làm được những việc mà người khác tin là không thể!”

Related Articles

Master list (in progress) of how to get parts of fields for use in Twig templates. I’m always having to look these up, so I thought I’d hash them out and write them down.

Litespeed Cache là plugin WordPress dùng để kết hợp với Web Server LiteSpeed nhằm tăng tốc website WordPress của bạn gấp nhiều lần

In this article, we are going to see how some tools & libraries will make people's lives easier during the development & code review process.

In this tutorial, you will learn how to improve the custom code, theme and module, and general code development by using the pre-commit hook on git

Trước khi tìm hiểu xem PHP Code Sniffer là gì thì các bạn cần phải nắm được coding convention là gì đã.