# New-be needs a PHP sample

**URL:** <https://devforum.zoom.us/t/new-be-needs-a-php-sample/4735>\
**Category:** API and Webhooks\
**Created:** [June 24, 2019, 1:39am UTC](https://devforum.zoom.us/t/new-be-needs-a-php-sample/4735 "2019-06-24T01:39:50Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![cfuller](https://avatars.discourse-cdn.com/v4/letter/c/b9bd4f/32.png) [@cfuller](https://devforum.zoom.us/u/cfuller)\
**Post date:** [June 24, 2019, 1:39am UTC](https://devforum.zoom.us/t/new-be-needs-a-php-sample/4735/1 "2019-06-24T01:39:51Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![ojus.zoom](https://sea2.discourse-cdn.com/flex016/user_avatar/devforum.zoom.us/ojus.zoom/32/734_2.png) [@ojus.zoom](https://devforum.zoom.us/u/ojus.zoom)\
**Post date:** [June 24, 2019, 9:08pm UTC](https://devforum.zoom.us/t/new-be-needs-a-php-sample/4735/2 "2019-06-24T21:08:00Z")

</div>

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](https://wordpress.org/plugins/video-conferencing-with-zoom-api/) 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](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

---

<div class="post-metadata">

**Author:** ![matt.schertz](https://avatars.discourse-cdn.com/v4/letter/m/a3d4f5/32.png) [@matt.schertz](https://devforum.zoom.us/u/matt.schertz)\
**Post date:** [June 25, 2019, 7:27pm UTC](https://devforum.zoom.us/t/new-be-needs-a-php-sample/4735/3 "2019-06-25T19:27:11Z")

</div>

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;
}
```

---

<div class="post-metadata">

**Author:** ![ojus.zoom](https://sea2.discourse-cdn.com/flex016/user_avatar/devforum.zoom.us/ojus.zoom/32/734_2.png) [@ojus.zoom](https://devforum.zoom.us/u/ojus.zoom)\
**Post date:** [June 26, 2019, 1:15am UTC](https://devforum.zoom.us/t/new-be-needs-a-php-sample/4735/4 "2019-06-26T01:15:50Z")

</div>

@matt.schertz,

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

---

<div class="post-metadata">

**Author:** ![cfuller](https://avatars.discourse-cdn.com/v4/letter/c/b9bd4f/32.png) [@cfuller](https://devforum.zoom.us/u/cfuller)\
**Post date:** [June 27, 2019, 1:55am UTC](https://devforum.zoom.us/t/new-be-needs-a-php-sample/4735/5 "2019-06-27T01:55:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cfuller](https://avatars.discourse-cdn.com/v4/letter/c/b9bd4f/32.png) [@cfuller](https://devforum.zoom.us/u/cfuller)\
**Post date:** [July 15, 2019, 3:00am UTC](https://devforum.zoom.us/t/new-be-needs-a-php-sample/4735/6 "2019-07-15T03:00:33Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![michael\_p.zoom](https://sea2.discourse-cdn.com/flex016/user_avatar/devforum.zoom.us/michael_p.zoom/32/18105_2.png) [@michael\_p.zoom](https://devforum.zoom.us/u/michael_p.zoom)\
**Post date:** [July 16, 2019, 3:21am UTC](https://devforum.zoom.us/t/new-be-needs-a-php-sample/4735/7 "2019-07-16T03:21:30Z")

</div>

Hi @cfuller,

> [@cfuller](#):
>
> 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.

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](https://marketplace.zoom.us/docs/guides/authorization/oauth/oauth-with-zoom#refresh-access-token)

---

<div class="post-metadata">

**Author:** ![cfuller](https://avatars.discourse-cdn.com/v4/letter/c/b9bd4f/32.png) [@cfuller](https://devforum.zoom.us/u/cfuller)\
**Post date:** [July 16, 2019, 3:33am UTC](https://devforum.zoom.us/t/new-be-needs-a-php-sample/4735/8 "2019-07-16T03:33:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![michael\_p.zoom](https://sea2.discourse-cdn.com/flex016/user_avatar/devforum.zoom.us/michael_p.zoom/32/18105_2.png) [@michael\_p.zoom](https://devforum.zoom.us/u/michael_p.zoom)\
**Post date:** [July 16, 2019, 3:43am UTC](https://devforum.zoom.us/t/new-be-needs-a-php-sample/4735/9 "2019-07-16T03:43:23Z")

</div>

@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

---

<div class="post-metadata">

**Author:** ![cfuller](https://avatars.discourse-cdn.com/v4/letter/c/b9bd4f/32.png) [@cfuller](https://devforum.zoom.us/u/cfuller)\
**Post date:** [August 3, 2019, 4:31am UTC](https://devforum.zoom.us/t/new-be-needs-a-php-sample/4735/10 "2019-08-03T04:31:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tommy](https://sea2.discourse-cdn.com/flex016/user_avatar/devforum.zoom.us/tommy/32/72769_2.png) [@tommy](https://devforum.zoom.us/u/tommy)\
**Post date:** [August 5, 2019, 3:52pm UTC](https://devforum.zoom.us/t/new-be-needs-a-php-sample/4735/11 "2019-08-05T15:52:53Z")

</div>

Hey @cfuller,

[Here is how to make a post request in PHP.](https://stackoverflow.com/questions/5647461/how-do-i-send-a-post-request-with-php)

Going by that example, I would try this to [refresh an access\_token](https://marketplace.zoom.us/docs/guides/authorization/oauth/oauth-with-zoom#refresh-access-token),

```php
$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

---

<div class="post-metadata">

**Author:** ![DeveloperBot](https://sea2.discourse-cdn.com/flex016/user_avatar/devforum.zoom.us/developerbot/32/12632_2.png) [@DeveloperBot](https://devforum.zoom.us/u/DeveloperBot)\
**Post date:** [August 21, 2020, 6:30pm UTC](https://devforum.zoom.us/t/new-be-needs-a-php-sample/4735/12 "2020-08-21T18:30:49Z")

</div>

Closed on August 21, 2020, 6:30pm UTC


