How to get a group by id calling the Prot-On Rest API with PHP

Get Group by id Demo Controller


<?php

namespace ProtOn\Demos\Controllers;

use ProtOn\Demos\ApiGroupIdDemo;

class DemosController extends Controller {
	
	/**
	 * Represents a GET request to /demos/group-id-demo
	 * Your view could consist in a form that posts it fields to itself.
	 * Post action would be controlled by "postApiGroupIdDemo()" method.
	 */
	public function getApiGroupIdDemo(){
	
		return "Your app view";
	
	}
	
	/**
	 * Represents a POST request to /demos/api-group-id-demo
	 * This method capture the values sended in a form,
	 * make the call to the API and return the result to the
	 * "getApiGroupIdResult($response)" method.
	 */
	public function postApiGroupIdDemo(){
	
		$demo = new ApiGroupIdDemo();
	
		$client_id =     $_POST['client_id'];
		$client_secret = $_POST['client_secret'];
		$groupId =       $_POST['groupId'];
	
		/*
		 * This is an example. You have to get your stored token from session,
		 * database or by calling your own oauth authentication service.
		 * Please check http://developers.prot-on.com/php-examples/oauth
		 * if you want to know how to implement an OAuth authentication system
		 */
		$access_token = $_SESSION['access_token'];
	
		try{
			if($access_token!=NULL){
				$response = $demo->getGroupById($access_token, $groupId);
			} else {
				throw new \Exception('Invalid authentication');
			}
		} catch (\Exception $e){
			$response['error'] = 400;
			$response['error_description'] = $e->getMessage();
		}
	
		return self::getApiGroupIdResult($response);
	
	}
	
	/**
	 * Represents a GET request to /demos/api-group-id-result
	 * Receives the result of the call and show the view.
	 */
	public function getApiGroupIdResult($response){
	
		return "Your app result";
	
	}
	
}
?>
			
			

BearerPest is a wrapper class that adds the Bearer authentication to Pest class, which is an external library in this example that allows to create an OAuth client.

You can get Pest class from its Github project, here, or use your own.

BearerPest Wrapper Code

<?php
namespace ProtOn\Utils;

use ProtOn\Utils\Pest;

class BearerPest extends Pest {

	protected $bearerHeader;
	
	public function setupAuth($user, $pass, $auth = 'basic'){
		if ($auth == 'bearer') {
			$this->bearerHeader = 'Authorization: Bearer ' . $user;
		} else {
			parent::setupAuth($user, $pass, $auth);
		}
	}
	
	public function prepData($data) {
		if (is_array ( $data )) {
			$multipart = false;
			
			foreach ( $data as $item ) {
				if (is_string ( $item ) && strncmp ( $item, "@", 1 ) == 0 && is_file ( substr ( $item, 1 ) )) {
					$multipart = true;
					break;
				} elseif ($item instanceof \CURLFile) {
					$multipart = true;
					break;
				}
			}
			
			return ($multipart) ? $data : http_build_query ( $data );
		} else {
			return $data;
		}
	}
	
	protected function prepHeaders($headers) {
		$headers = parent::prepHeaders($headers);
		if (!empty($this->bearerHeader)) {
			$headers[] = $this->bearerHeader;
		}
		return $headers;
	}

}

?>
			

Get Group by id Demo Service

			
<?php

namespace ProtOn\Demos;

use ProtOn\Utils\BearerPest;

class ApiGroupIdDemo {
	
	const PROTON_REST_API = 'https://dnd.prot-on.com/rest-api/api';
	
	public function getGroupById($token, $groupId){
		
		$response = NULL;
		
		$service_url = '/groups/'.$groupId.'/';
		
		$httpHeaders = array('Content-Type' => 'text/html; charset=utf-8');
		$data = array();
		
		$pest = new BearerPest(self::PROTON_REST_API);
		$pest->setupAuth($token, '', 'bearer');
		try {
			$body = $pest->get($service_url, $data, $httpHeaders);
			$response = $body;
		} catch (Pest_Exception $e) {
			$response = $e->getMessage();
		}
		
		return $response;
		
	}
	
}
?>