Laravel 8 Zoom

 Kaynak: https://artisansweb.net/how-to-create-zoom-meetings-with-php-and-jwt/

https://stackoverflow.com/questions/62619661/how-can-i-use-php-packages-in-laravel

$JWT=new \Firebase\JWT\JWT;





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.

1
2
3
4
5
6
7
8
<?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.

1
2
3
4
5
6
7
8
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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.

Comments

Popular posts from this blog

Laravel Datatable