Zoom API Integration with PHP

  • 30-12-2022
  • 1106
  • PHP
  • Haresh Chauhan

zoom API integration PHP in this tutorial we will integrate zoom API in our PHP application for creating, deleting, and accessing zoom meetings using the PHP application. zoom provided individual and grouping video call services for seeing virtually each other, video call service, meetings, distance education, etc, etc without engaging each other physically.

In this tutorial, we will integrate zoom API for creating, accessing, deleting, and zoom meetings using API, there for firstly we need to generate API credentials form the zoom account like ClientId and Securite API key.

For getting access to zoom meetings, we will generate a token, and that token we will store in the database, using that token we can access zoom meeting API by creating, accessing, attending, and deleting meetings using this token in the API as header param.

We will install composer using the below-provided command, this command will create a core dependency library in the application for the load autoload file from the vendor folder.

  • on Zoom App marketplace
  • Upon registration in the zoom platform create credentials and here you need to pass the redirect URL.
  • After that, you need to provide basic details
  • There will be an additional option like subscription and chat you can either enable.

Step 1. Install Composer

Install composer using the below-provided command, this command will create the guzzlehttp/guzzle class for the autoload file from the vendor. so go to your project root file in the command prompt and hit the below-given command.

composer require guzzlehttp/guzzle

Step 2. Add Details

Generate your API key credentials from your zoom account, register your zoom account if you don't have one, or else log in to your account and generate the API client id and secret id form the zoom web application.

config.php
<?php

require_once 'vendor/autoload.php';
require_once "db.php";
    
define('CLIENT_ID', 'YOUR_CLIENT_ID');
define('CLIENT_SECRET', 'YOUR_CLIENT_SECRET');
define('REDIRECT_URI', 'REDIRECT_URL_FOR_OAUTH');

Step 3. Index File

This is our root index file in the PHP project, this file will load from the root, In this file, we will add a link for zoom account authentication. once you click on the link you will be redirected to your zoom account, login into your zoom account. This will create an access token for the authentication and create zoom meeting request access.

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>

Step 4. Create Token Table

Create a token table in the PHP application, just copy this create table script and paste it into your SQL PHPMyAdmin panel and create a token table.

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

Step 5. Class Model

we will create a model file for the database connection after the creat token table in the database.

model.php
<?php
class DB {
    // Database credentials
    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;
              }
         }
    }
    
    // Check is table empty
    public function is_table_empty() {
         $result = $this->db->query("SELECT id FROM token");
         if($result->num_rows) {
              return false;
         }
    
         return true;
    }
    
    // Get access token
    public function get_access_token() {
         $sql = $this->db->query("SELECT access_token FROM token");
         $result = $sql->fetch_assoc();
         return json_decode($result['access_token']);
    }
    
    // Get referesh token
    public function get_refersh_token() {
         $result = $this->get_access_token();
         return $result->refresh_token;
    }
    
    // Update access 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)");
         }
    }
}

Step 6. Create Metting

we will create a file for the create meeting, in this file we will call the config file for the getting register API key details provided. created a create_meeting() function, we will call this function to create a meeting on the zoom site. in this function, we will add some basic zoom meetings and create details like meeting date time, meeting duration as well as meeting protection password, etc...

createMeeting.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 {
         // if you have userid of user than change it with me in url
         $response = $client->request('POST', '/v2/users/me/meetings', [
              "headers" => [
                   "Authorization" => "Bearer $accessToken"
              ],
              'json' => [
                   "topic" => "Integrate zoom APIs",
                   "type" => 2,                              
                   "start_time" => "2020-06-22T20:30:00",    // meeting start time
                   "duration" => "30",                       // 30 minutes
                   "password" => "123456"                    // meeting password
              ],
         ]);
    
         $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();

Step 7. Get Metting

For getting zoom meetings we will use this PHP file. by running this file we will get a zoom meeting link, by clicking in zoom meeting we will redirect to the zoom web portal for the attending meeting.

getMeeting.php
<?php

require_once 'config.php';
    
function get_meetings() {
    $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('GET', '/v2/users/me/meetings', [
              "headers" => [
                   "Authorization" => "Bearer $accessToken"
              ]
         ]);
    
         $data = json_encode(json_decode($response->getBody()),true);
         print_r($data);
    
    } 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();
         }
    }
}
    
get_meetings();

Step 7. Delete Metting

In case you wish to delete the zoom meeting, use this file. this function will help you to delete zoom created meetings in case change your meeting schedule. in this meeting delete API, we will just pass a token for delete the zoom meeting.

delete.php
<?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;
$meetingId = $_GET['meetingid'];  

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

Step 8. Callback Response

This is our callback handle function, this function will help us to handle callback responses from the zoom meeting web portal. use this callback method if you wish to handle a response and want to redirect the user after the response or meeting create.

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();
}

We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :