How to Create a Meeting on Zoom using Zoom API and PHP

web design lagos
Share on facebook
Share on twitter
Share on whatsapp

Recently I was working on a client project where I needed to interact with the Zoom API. In the application, we implemented a lot of stuff using Zoom API like Accounts, Billing, Meetings, Groups, Reports, Rooms, etc. Though we did a lot of stuff using the Zoom API, this tutorial will focus on how to create a meeting using Zoom API and PHP.

As we all know, Zoom is a platform which is used for teleconferencing, telecommuting, distance education, etc. It is popular among people for online conferences, meetings, webinars, and other stuff.

Those who are looking for creating a meeting through Zoom API need to choose either OAuth or JWT for interacting with their API. OAuth and JWT(JSON web token) provide a high level of security to make interactions with the third party services. In this tutorial, we will use the OAuth process and communicate with the Zoom API through it.

Create an OAuth app on Zoom

Once you have your Zoom account, you need to create an OAuth app on Zoom using the below steps.

  • Register you app on Zoom APP Marketplace.
  • Upon registering an app, you will get your generated credentials. Here you need to pass Redirect URL for OAuth and Whitelist URL.
  • On the next step, enter the basic information about your app.
  • In the next page, you can optionally enable some additional features such as Event Subscriptions and Chat Subscriptions for your app.
  • Next you need to add scopes regarding you app. For example, you can add scope for Meeting.

If you are on localhost then use the ngrok and generate the local URL. In my case, I am using ngrok, and using the generated URL through ngrok my URL for OAuth redirection and Whitelist URL is as shown below.

If you are facing an issue with the creating an OAuth app please refer Zoom’s official documentation on Create an OAuth App.

Basic Setup and Configuration

So far, I didn’t find any PHP library which can be used to generate an access token and interact with the Zoom API. Doing some research I am able to manage it through the Guzzle library and REST API. That being said, let’s first install the Guzzle library using the command:

composer require guzzlehttp/guzzle

We are going to store the access token in the database for later use. The access token is valid for a short period of time. In our code, we will regenerate the access token in the background so that the user doesn’t need to do the authorization process again. Run the below SQL query to create a database table that holds access token.

CREATE TABLE `token` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `access_token` text NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

As we will require the token values from the database, we need to write a code for it. Create a file class-db.php and add the code below in it.

class-db.php

<?php
class DB {
    private $dbHost     = "DB_HOST";
    private $dbUsername = "DB_USERNAME";
    private $dbPassword = "DB_PASSWORD";
    private $dbName     = "DB_NAME";

    public function __construct(){
        if(!isset($this->db)){
            // Connect to the database
            $conn = new mysqli($this->dbHost, $this->dbUsername, $this->dbPassword, $this->dbName);
            if($conn->connect_error){
                die("Failed to connect with MySQL: " . $conn->connect_error);
            }else{
                $this->db = $conn;
            }
        }
    }
 
    public function is_table_empty() {
        $result = $this->db->query("SELECT id FROM token");
        if($result->num_rows) {
            return false;
        }
 
        return true;
    }
 
    public function get_access_token() {
        $sql = $this->db->query("SELECT access_token FROM token");
        $result = $sql->fetch_assoc();
        return json_decode($result['access_token']);
    }
 
    public function get_refersh_token() {
        $result = $this->get_access_token();
        return $result->refresh_token;
    }
 
    public function update_access_token($token) {
        if($this->is_table_empty()) {
            $this->db->query("INSERT INTO token(access_token) VALUES('$token')");
        } else {
            $this->db->query("UPDATE token SET access_token = '$token' WHERE id = (SELECT id FROM token)");
        }
    }
}

Make sure to replace the placeholders with your actual database credentials. Next, let’s generate an access token through the OAuth process.

Generate an Access Token

The user can create an access token for their account using the App credentials and OAuth process. Create a config.php file and store app credentials and redirect URL in this PHP file. Include the other environments like DB class and vendor library also.

config.php

<?php
require_once 'vendor/autoload.php';
require_once "class-db.php";

define('CLIENT_ID', 'YOUR_CLIENT_ID');
define('CLIENT_SECRET', 'YOUR_CLIENT_SECRET');
define('REDIRECT_URI', 'REDIRECT_URL_FOR_OAUTH');

Here, replace the placeholders with your app credentials and set the same redirect URL you given in the OAuth app. In my case, the redirect URL is https://f2448150.ngrok.io/zoom/callback.php . It means in the callback.php file, we have to write the code which calls a Zoom API, request an access token, and store it in our database.

callback.php

<?php
require_once 'config.php';
 
try {
    $client = new GuzzleHttp\Client(['base_uri' => 'https://zoom.us']);

    $response = $client->request('POST', '/oauth/token', [
        "headers" => [
            "Authorization" => "Basic ". base64_encode(CLIENT_ID.':'.CLIENT_SECRET)
        ],
        'form_params' => [
            "grant_type" => "authorization_code",
            "code" => $_GET['code'],
            "redirect_uri" => REDIRECT_URI
        ],
    ]);

    $token = json_decode($response->getBody()->getContents(), true);

    $db = new DB();

    if($db->is_table_empty()) {
        $db->update_access_token(json_encode($token));
        echo "Access token inserted successfully.";
    }
} catch(Exception $e) {
    echo $e->getMessage();
}

Now, let’s generate an authorized URL where a user can click and complete the authorization process. I am going to create this URL in the index.php file.

index,php

<?php
require_once 'config.php';

$url = "https://zoom.us/oauth/authorize?response_type=code&client_id=".CLIENT_ID."&redirect_uri=".REDIRECT_URI;
?>
 
<a href="<?php echo $url; ?>">Login with Zoom</a>

Run the above file on the browser, click on the URL, and complete the authorization process. On successful authentication, you should see a success message and access token would store in your ‘token’ table. If that works, we can go ahead and create a meeting with the Zoom API.

Create a Meeting on Zoom using Zoom API

Zoom is providing an endpoint for creating a meeting through API. You may read about it on their documentation. It asks to send a POST request to the endpoint along with the required parameters.

The API endpoint also requires an access token to be passed in the header. As I said earlier, the access token has a short life and we are going to regenerate it in the background without asking for the authentication process again.

I have created a file create-meeting.php file and send a POST request to the endpoint. I also handled the scenario of expired token and regenerating it.

create-meeting.php

<?php
require_once 'config.php';

function create_meeting() {
    $client = new GuzzleHttp\Client(['base_uri' => 'https://api.zoom.us']);

    $db = new DB();
    $arr_token = $db->get_access_token();
    $accessToken = $arr_token->access_token;

    try {
        $response = $client->request('POST', '/v2/users/me/meetings', [
            "headers" => [
                "Authorization" => "Bearer $accessToken"
            ],
            'json' => [
                "topic" => "Let's learn Laravel",
                "type" => 2,
                "start_time" => "2020-05-05T20: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;

    } catch(Exception $e) {
        if( 401 == $e->getCode() ) {
            $refresh_token = $db->get_refersh_token();

            $client = new GuzzleHttp\Client(['base_uri' => 'https://zoom.us']);
            $response = $client->request('POST', '/oauth/token', [
                "headers" => [
                    "Authorization" => "Basic ". base64_encode(CLIENT_ID.':'.CLIENT_SECRET)
                ],
                'form_params' => [
                    "grant_type" => "refresh_token",
                    "refresh_token" => $refresh_token
                ],
            ]);
            $db->update_access_token($response->getBody());

            create_meeting();
        } else {
            echo $e->getMessage();
        }
    }
}

create_meeting();

If you noticed the code, I have passed “2020-05-05T20:30:00” as a ‘start_time’. It means meeting time will be 05 May 2020, 08:30 PM. The user should pass the format for it as yyyy-MM-ddTHH:mm:ss. For the ‘type’ key I passed value ‘2’ which is for a Scheduled meeting. The user also needs to set a meeting password which I set to ‘123456’.

Delete a Meeting

The user can play with the API endpoints like list, update, delete a meeting. All you need to do is follow their guidelines on using specific endpoints. For example, you can delete a meeting by sending a DELETE request to the API endpoint. To this endpoint, you need to pass your meeting id as follows.

<?php
require_once 'config.php';

$client = new GuzzleHttp\Client(['base_uri' => 'https://api.zoom.us']);

$db = new DB();
$arr_token = $db->get_access_token();
$accessToken = $arr_token->access_token;

$response = $client->request('DELETE', '/v2/meetings/{meeting_id}', [
    "headers" => [
        "Authorization" => "Bearer $accessToken"
    ]
]);

I hope you got to know how to create a meeting using Zoom API and PHP. I would like to hear your thoughts or suggestions in the comment section below.

Leave a Comment

Your email address will not be published. Required fields are marked *