GoToMeeting, GoToWebinar PHP API Class

February 11th | By | Back to Blog

GoToMeeting and GoToWebinar are two great services provided by Citrix for holding online meetings and webinars. The two popular platforms have released their API to enable web applications to integrate with their services.

Here is a PHP class we recently wrote for project that required such an integration.

<?php
class CitrixAPI {

	public $_organizerKey;
	public $_accessToken;
	
	public function __construct ($_accessToken = null, $_organizerKey = null) {
		$this->_accessToken = $_accessToken;
		$this->_organizerKey = $_organizerKey;
	}
	
	public function getOAuthToken ($_apiKey = null, $_callbackUrl = null) {
		if (isset($_GET['authorize']) && (int)$_GET['authorize'] == 1) {
			header('location:https://api.citrixonline.com/oauth/authorize?client_id='. $_apiKey .'&redirect_uri=' . $_callbackUrl);
			exit();
		}
		
		if (isset($_GET['code'])) {
			$url = 'https://api.citrixonline.com/oauth/access_token?grant_type=authorization_code&code='. $_GET['code'] .'&client_id='. $_apiKey;
			return $this->makeApiRequest($url);
		}
	}

	/**
	 * @name getAttendeesByOrganizer
	 * @desc GoToMeeting API
	 */
	
	public function getAttendeesByOrganizer () {
		$url  = 'https://api.citrixonline.com/G2M/rest/organizers/'. $this->_organizerKey .'/attendees';
		$url .= '?startDate='. date('c');
		$url .= '?endDate='. date('c', strtotime("-7 Days"));
		
		return $this->makeApiRequest($url, 'GET', array(), $this->getJsonHeaders());
	}
	
	/**
	 * @name getFutureMeetings
	 * @desc GoToMeeting API
	 */
	
	public function getFutureMeetings () {
		$url  = 'https://api.citrixonline.com/G2M/rest/meetings?scheduled=true';
		return $this->makeApiRequest($url, 'GET', array(), $this->getJsonHeaders());
	}

	/**
	 * @name getUpcomingWebinars
	 * @desc GoToWebinar API
	 */
	public function getUpcomingWebinars () {
		$url  = 'https://api.citrixonline.com/G2W/rest/organizers/'. $this->_organizerKey .'/upcomingWebinars';
		return $this->makeApiRequest($url, 'GET', array(), $this->getJsonHeaders());
	}
	
	/**
	 * @name getUpcomingWebinars
	 * @desc GoToWebinar API
	 */
	public function getPastWebinars () {
		$url  = 'https://api.citrixonline.com/G2W/rest/organizers/'. $this->_organizerKey .'/historicalWebinars';		
		return $this->makeApiRequest($url, 'GET', array(), $this->getJsonHeaders());
	}
	
	/**
	 * @name getWebinarAttendees
	 * @desc GoToWebinar API
	 */

	public function getWebinarAttendees ($webinarKey) {
		$url  = 'https://api.citrixonline.com/G2W/rest/organizers/'. $this->_organizerKey .'/webinars/'. $webinarKey .'/attendees';
		return $this->makeApiRequest($url, 'GET', array(), $this->getJsonHeaders());
	}
	
	public function getWebinarRegistrants ($webinarKey) {
		$url  = 'https://api.citrixonline.com/G2W/rest/organizers/'. $this->_organizerKey .'/webinars/'. $webinarKey .'/registrants';
		return $this->makeApiRequest($url, 'GET', array(), $this->getJsonHeaders());
	}
	
	public function getWebinar ($webinarKey) {
		$url  = 'https://api.citrixonline.com/G2W/rest/organizers/'. $this->_organizerKey .'/webinars/'. $webinarKey;
		return $this->makeApiRequest($url, 'GET', array(), $this->getJsonHeaders());
	}
	/**	  
	 * @param String $url
	 * @param String $requestType
	 * @param Array $postData
	 * @param Array $headers
	 */
	
	public function makeApiRequest ($url = null, $requestType = 'GET', $postData = array(), $headers = array()) {

	    $ch = curl_init();
	    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
	    curl_setopt($ch, CURLOPT_URL, $url);
	    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	
	    
	    if ($requestType == 'POST') {
	        curl_setopt($ch, CURLOPT_POST, 1);
	        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);  
	    }

	    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
	    $data = curl_exec($ch);

	    $validResponseCodes = array(200, 201, 409);
	    $responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 

	    if (curl_errno($ch)) {
	    	curl_close($ch);
	        return curl_error($ch); 
	    } 
	    elseif (!in_array($responseCode, $validResponseCodes)) {
	        if ($this->isJson($data)) {
	            $data = json_decode($data);
	        }  	        		
	    }
	    
	    curl_close($ch);
	    return $data;	  
	}
	
	public function getJsonHeaders () {
		return array(
			"HTTP/1.1",
			"Content-type: application/json",
			"Accept: application/json",
			"Authorization: OAuth oauth_token=". $this->_accessToken
		);
	}
	
	public function isJson ($string) {
	    $isJson = 0;
	    $decodedString = json_decode($string);
	    
	    if (is_array($decodedString) || is_object($decodedString)) {
	        $isJson = 1;    
	    }
	    
	    return $isJson;
	}
}

The Code Explained

In order to get started you will need to open a developer account on Citrix and create an application. When creating your application, you will need to specify which API would you like to use, GoToMeeting or GoToWebinar, this class uses common methods from both APIs and can be used for both.

Next, you will need to retrieve your oAuth token to use the API, if your application does not require authentication you will need to get the token once and store it, it should be valid for a year. In any other case, you will need to store the token in your session.

In order to get your token, just initialize the class and use the getOAuthToken method, like so:

<?php
$citrix = new CitrixAPI();
$oauth = $citrix->getOAuthToken([your-api-key], [callback-url]);

var_dump($oauth);
?>

The [callback-url] should be the same URL your sending the request from and the API key is provided in your Citrix developer account, on your application screen. This code will redirect you to Citrix for validation, redirect you back to your [callback-url] and eventually dump the data on your screen, which you can then store manually or handle in a session.

In order to initialize the code go to the URL where this code is setup and navigate to ?authorize=1 in order to initialize the authentication process.

Available Methods

Here is the list of available methods:

  • getAttendeesByOrganizer
  • getFutureMeetings
  • getUpcomingWebinars
  • getPastWebinars
  • getWebinarAttendees
  • getWebinarRegistrants
  • getWebinar

To create more methods you can review GoToMeeting or GoToWebinar APIs. Was this helpful? let us know or share with others.


  • michaelphipps

    Thanks for writing this up! I just wish I’d found your article 3 hours ago. Wasted such a lot of time with the Citrix-GoToWebinar-PHP-Library on git-hub because it doesn’t return the correct Organizer Key. Your code works. Thanks again!

  • http://www.facebook.com/liad.guez Liad Guez

    Thanks, this is the exact reason I wrote it, hope it helps.

  • Paul Das

    Hello Liad – How excellent is this. Many thanks. I have managed to get as far as getting the G2M to my code that I have registered but how do I get the attendees of the meeting?

    include “YourClasses.php”;

    $citrix = new CitrixAPI();

    $result= $citrix->getFutureMeetings();

    What am I missing?

  • Paul Das

    I am getting this error object(stdClass)#2 (2) { ["int_err_code"]=> string(12) “InvalidToken” ["msg"]=> string(20) “Invalid token passed” }

  • Stranet

    Great Work!!
    I’m working on integration of GotoWebinar in my site, do you know if it’s possible to SCHEDULE a webinar via api?

    Thank you a lot.

  • conflictxinside

    Any chance you can show how to use any of these methods to output anything? Where do we enter our organizer key and access token once we get them?