Create Online Meeting in Laravel with Zoom API

Featured Programming

The COVID-19 outbreak has unprecedentedly impacted countries around the world particularly during the last six-odd months. As the corona virus (COVID 19) pandemic continues to spread, the world is changing the way it previously used to embrace.

During the last six months the internet traffic has tremendously increased. Demand has skyrocketed for several online services. Video calls have replaced direct meetings with management, clients, colleagues, and families alike. More and more people are inclined towards using video-conferencing services like Zoom, GotoMeeting, Cisco WebEx, and Microsoft Teams, to name a few.

Zoom is, by far the most popular communication platform for video conferences, chats, webinars and online classes.

The Zoom API provides the developers with the access mechanism of the collection of zoom resources from Zoom. Apps can provide you with features like creating a new meeting, creating, adding and removing users, viewing reports and dashboards and many more – using Zoom API.

Zoom API has two options for authentication: OAuth and JWT (JSON Web Token). JWT is recommended for server-to-server API calls. So if you want the user to perform an operation on his own account from his own client, you should use user operations with OAuth. If you want your server to open a Zoom meeting and wait for users to enter, and record the meeting on the server, there’s no “Zoom client” involved in these operations, then JWT authentication is the only choice.

JSON Web Tokens are the long text strings which are passed on every request to verify the authorization of the server. They contain a combination of secrets from the API and payload data in the form of a JSON object.

As a short brief on how JWT works – you, as a client will use your API key and secret, and other parameters like the expiration time of the token, to generate a signed token which you’ll send to the server. This step is performed on the client, using one of the JWT client libraries. The server will use your API secret to decode your JWT and respond with another token, which can be used to perform API calls, until it expires. You call the API endpoint you need with the token you received.

Before we delve into real coding we must perform the following as a pre-requisite:

  • Head to https://zoom.us and sign up. You can sign up for a free account or if you already have a Zoom account then sign in.
  • Create a JWT App in order to get API Key & Secret. Use this link for the detailed information on how to create a Web SDK App for your web application.

Up till now it is assumed that you have created a JWT App and got the API key and token to use in your Laravel application. Time to roll up our sleeves and start some coding to create a Zoom meeting using Zoom API and providing above JWT token.

.env File

A .env file is an application-level configuration file in Laravel architecture. It contains settings in the form of kay-value pairs. These settings can then be used from anywhere in the application code like: env(‘key’)

First of all we have to add a couple of settings in the .env file like below.

ZOOM_HOST_USERID=<ValidZoomUserId>
ZOOM_JWT_TOKEN=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOm51bGwsImlzcyI6InJ5bWo1T1ZwUkVDYktmal95enU3aEEiLCJleHAiOjE2MTcxOTE0MDAsImlhdCI6MTYwMTYzNDQ2MX0.Nu67kWMAZWvieWpO45GKmzPGAT7IaX76UWk7m114H64

Note that the above JWT token is not a real one, it is just an example, albeit your actual token should look like this.

Zoom Helper Class

Let’s dive into some real code to get the things done for us. Let’s start from a Zoom helper class. This class undertakes the necessary code which is required to create a zoom meeting using Zoom API. The helper class is intended to be a generic one so that it can be placed and invoked from any Laravel project.

<?php

namespace App\Helpers;
use Illuminate\Http\Request;

class ZoomHelper
{
	public static function CreateZoomMeeting(string $Topic, string $StartTime, $Duration)
	{		
		// Modify the Start Time format a bit, YYYY-MM-DD HH:MM:SS to YYYY-MM-DDTHH:MM:SS
		$StartTime = str_replace(" ", "T", $StartTime);

		//Get Host Id and JWT Token from .env 		
		$ZOOM_Host_UserId = env('ZOOM_HOST_USERID');	
		$ZOOM_JWT_Token = env('ZOOM_JWT_TOKEN');
		$authorization = "Bearer " .$ZOOM_JWT_Token;	
		 
		try {
			$arr = array(
				"userid" => $ZOOM_Host_UserId,		
				"topic" => $Topic,			//Topic of the meeting.		
				"start_time" => $StartTime,		//e.g, "2020-10-21T10:30:00"
				"duration" => $Duration,
				"authorization" => $authorization	
			);

			$dataRet = self::createMeeting($arr);
			return $dataRet;

		} catch (Exception $e) {
			echo "error";
		}
	}

	public static function createMeeting($arr)
	{
		$curl = curl_init();
		$data = [
			"topic" => $arr["topic"],
			"type" => 2,
			"start_time" => $arr["start_time"],
			"duration" => 	$arr["duration"],	//meeting duration in minutes.
			"password" => "12349876",
			"settings" => [
				"waiting_room" => true,
				"host_video" => true,
				"join_before_host" => true	
			]
		];
		curl_setopt_array($curl, array(
			CURLOPT_URL => "https://api.zoom.us/v2/users/" . $arr["userid"] . "/meetings",
			CURLOPT_POSTFIELDS => json_encode($data),
			CURLOPT_RETURNTRANSFER => true,
			CURLOPT_ENCODING => "",
			CURLOPT_MAXREDIRS => 10,
			CURLOPT_TIMEOUT => 30,
			CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
			CURLOPT_CUSTOMREQUEST => "POST",
			CURLOPT_HTTPHEADER => array(
				"User-Agent: Zoom-Jwt-Request",
				"authorization: " . $arr["authorization"],
				"content-type: application/json"
			),
		));
		$response = curl_exec($curl);
		$data = json_decode($response);
		$err = curl_error($curl);
		curl_close($curl);
		if ($err) {
			echo "cURL Error #:" . $err;
		} else {
			return $data;
		}
	}
}

?>

Not to mention that the above code can be modified to customize your requirements. You can do this by changing the parameter values like “Type” or “Password” or adding more parameters. Refer to Zoom API documentation to comprehend these parameters and the purpose of each one.

And here is the simple Controller class to create a Zoom meeting through the Zoom Helper class we just created above.

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Helpers\ZoomHelper;
use Illuminate\Http\Request;

class ZoomSessionController extends Controller
{
	public function createZoomSession(Request $request)
	{
		/* Create Zoom Meeting. This method is supposed to be invoked by a simple form post, in which user has to provide meeting title, time and duration.  */

		$dataRet = ZoomHelper::CreateZoomMeeting($request->ZoomSessionTitle, $request->ZoomSessionTime, intval($request->ZoomSessionDuration));
		return redirect()->back();
	}
}

Leave a Reply

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