New-be needs a PHP sample

I have a basic-moderate level of PHP coding experience. I am trying to write a custom WordPress Function to be able to put a form on our WordPress site that uses the API to register people based on the data they provide on our site, as opposed to sending people to the Zoom site.

I am struggling with understanding the oAuth process. I signed up for an API access and have the oAuth ClientID and ClientSecret, but I don’t understand the process for refreshing the certificate. I also don’t have a lot of experience using cURL in PHP or using APIs. I really only need to use two different API calls, but I’ve spent most of the day trying to figure it out and haven’t been able to.

Does anyone have a sample PHP script I can use to replicate the authorization process? I usually work best with samples to work from, and Google Search are not showing me any PHP samples.

Thanks for any help you can provide!

Hi @cfuller,

Welcome to the Zoom Developer Community.

At this time, we unfortunately do not have a ready to use example for PHP or Wordpress, but you can consider implementing this plugin developed by 3rd party developers in your application. Please note that we will not be able to support this plugin.

To know about OAuth with Zoom, please visit: https://marketplace.zoom.us/docs/guides/authorization/oauth/oauth-with-zoom

If you have any specific questions related to OAuth, please let us know and we will continue to assist you.

Cheers,
Ojus

Here’s two functions that I use in PHP to get authorization and send a chatbot message.

function getAccessToken() {
	$clientID="xxxxxxxxxxxxxxxxxxxxxx";
    $clientSecret="xxxxxxxxxxxxxxxxxxxxxxxxxxx";
	$content = "grant_type=client_credentials&client_id=$clientID&client_secret=$clientSecret";
    $token_url="https://zoom.us/oauth/token";
	$curl = curl_init();
	curl_setopt_array($curl, array(
		CURLOPT_URL => $token_url,
		CURLOPT_SSL_VERIFYPEER => true,
		CURLOPT_RETURNTRANSFER => true,
		CURLOPT_POST => true,
		CURLOPT_POSTFIELDS => $content
	));
	$response = curl_exec($curl);
	curl_close($curl);

	if ($response === false) {
    $retVal["Status"]=0;
		$retVal="Failed to retrieve code";
	} elseif (json_decode($response)->error) {
		$retVal["Status"]=0;
    $retVal["Message"]="Error: $response";
	} else {
    $retVal["Status"]=1;
    $retVal["Token"]=json_decode($response)->access_token;
}

	return $retVal;
}

//Sends the message to Zoom
//Parameters:
//  $to - User or Channel to send to.  ex. xxxxxxxxxxxxxxxxxxxxxxxx@conference.xmpp.zoom.us
//  $content - object containing the head and body of the message
function sendMessage($to,$content) {
    $msg->robot_jid="xxxxxxxxxxxxxxxxxxxx";
    $msg->to_jid=$to;
    $msg->account_id="xxxxxxxxxxxxxxxxxxxxxxxxx";
    $msg->content=$content;
    $auth=getAccessToken();
    if (!$auth["Status"]) {
        $retVal["Status"]=9;
        $retVal["Message"]=$auth["Message"];
    } else {
        $url="https://api.zoom.us/v2/im/chat/messages";
        $token=$auth["Token"];
        $header = array("Authorization: Bearer $token","Content-Type: application/json","Content-Length: ".strlen(json_encode($msg)));
        $curl = curl_init();
	    curl_setopt_array($curl, array(
		    CURLOPT_URL => $url,
		    CURLOPT_HTTPHEADER => $header,
		    CURLOPT_SSL_VERIFYPEER => true,
		    CURLOPT_RETURNTRANSFER => true,
		    CURLOPT_POST => true,
		    CURLOPT_POSTFIELDS => json_encode($msg)
	    ));
        $response = curl_exec($curl);

        if ($response === false) {
            $retVal["Status"]=8;
		    $retVal["Message"]=curl_error($curl);
	    } elseif (json_decode($response)->error) {
		    $retVal["Status"]=7;
            $retVal["Message"]="Error: $response";
	    } else {
            $retVal["Status"]=1;
            $retVal["Message"]=$response;
        }
        curl_close($curl);
    }
    return $retVal;
}

@matt.schertz,

What errors or messages are you receiving when you implement this code?

Thank you both. I will try both solutions to see if I can get it to work.
Thanks again!

I have spent three days now trying to figure this out and don’t know what else to try. I can use your code above and get a token, but when I attempt to use the token, I get this error:

{“reason”:“Invalid client_id or client_secret”,“error”:“invalid_client”}

The CURL code I’m using to get that error is:

    $curl = curl_init();
	curl_setopt_array($curl, array(
		CURLOPT_URL => "https://api.zoom.us/v2/webinars/$id/registrants/questions",
		CURLOPT_RETURNTRANSFER => true,
		CURLOPT_ENCODING => "",
		CURLOPT_MAXREDIRS => 10,
		CURLOPT_TIMEOUT => 30,
		CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
		CURLOPT_CUSTOMREQUEST => "GET",
		CURLOPT_HTTPHEADER => array( "authorization: Bearer $token" ),
	));

(And $token was the result of the getAccessToken above)

When I go to the Developer Page (https://marketplace.zoom.us/docs/api-reference/zoom-api/webinars/webinarregistrantsquestionsget) and use that to develop the token, and then use that token string as $token instead, it works on my site. But then the token expires an hour later and it doesn’t work anymore, so I need something that will automatically get the the token.

In reading the documentation about the Oauth process that is required for the API calls I need to make it sounds like the user would need a Zoom account … but that wouldn’t work given that the use case is I want users to my site to be able to fill out a form to register for the webinar, so they woudln’t have a zoom account, I want it to use my account credentials to authenticate and log on, but I can’t do that everytime a user comes to my site and wants to register for a website.

The website is a wordpress site, so I’m trying to create custom PHP functions that will first use the " /webinars/{webinarId}/registrants/questions" API call to get the questions, and then dynamically make a form, and then once submitted I will process the form in a separate PHP handler so I’ll need to have that other call authenticated as well to then use the " /webinars/{webinarId}/panelists" call next.

This seems like such a simple use case, and I’m not sure what to try next.

Hi @cfuller,

Have you tried to use a refresh token, after the current token expires[1]?

1 - https://marketplace.zoom.us/docs/guides/authorization/oauth/oauth-with-zoom#refresh-access-token

I did. But the error message I had gotten back was that the token was expired. I’m not sure if there is a time limit between when a token is issued and when it can be renewed… There was about a day or so between when I tried.

@cfuller,

The refresh token is set to 15 years so I’m sure it hasn’t expired. Have you tried getting another access token then storing the new refresh token(from the returning payload) to use it an hour later after the new access token expires?

Thanks

How do I write the CURL statement in PHP to do the token refresh? The sample on the instructions page is written in Node, and I tried to use that as a model, but I can not get it to work.

Thanks!

Thanks

Hey @cfuller,

Here is how to make a post request in PHP.

Going by that example, I would try this to refresh an access_token,

$url = 'https://zoom.us/oauth/token?grant_type=refresh_token&refresh_token={{ REFRSH_TOKEN }}';

$options = array(
    'http' => array(
        'header'  => "Authorization: Basic {{ base64Encode(client_id:client_secret) }}",
        'method'  => 'POST'
    )
);
$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { /* Handle error */ }

var_dump($result);

Let me know if this helps!

Thanks,
Tommy