How to build an OAuth authentication system with PHP-CURL

OAuth Authentication Example using cURL

<?php
	$accion = $_GET['accion'];
	switch($accion){
		case "code":
			getCode();
		break;
		case "token":
			$code = $_GET['code'];
			getToken($code);
		break;
		case "call":
			$token = $_GET['token'];
			$serviceToCall = "a_service_to_call";
			call($token, $serviceToCall);
		break;
	}
	
	/**
	 * Function to get an authorization code from OAuth provider
	 */
	function getCode(){
		
		$url = "https://proton.prot-on.com/external/oauth/authorize";
		
		$params = array(
			"response_type" => "code",
			"client_id" => '$your_client_id',
			"redirect_uri" => '$your_redirect_uri',
			"grant_type" => "authorization_code");
		
		$postFields = "";
		foreach($params as $key => $value){
		   $postFields .= $key . '='.urlencode($value).'&';
		}
		$postFields = rtrim($postFields, '&');
		
		$ch = curl_init();
		
		// https handling
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_HEADER, true);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
		curl_setopt($ch, CURLOPT_POST, true);
		curl_setopt($ch, CURLOPT_HEADER,'Content-Type: application/x-www-form-urlencoded');
		curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
		      
		$result = curl_exec($ch);
		
	}
	
	/**
	 * Function to get an OAuth access token from OAuth provider
	 * @param  $code the oauth authorization code
	 */
	function getToken($code){
		
		$url = "https://proton.prot-on.com/external/oauth/token";
		
		$params = array(
			"client_id" => "your_client_id",
			"client_secret" => "your_client_secret",
			"code" => $code,
			"grant_type" => "authorization_code");
		
		$postFields = "";
		foreach($params as $key => $value){
		   $postFields .= $key . '='.urlencode($value).'&';
		}
		$postFields = rtrim($postFields, '&');
		
		$ch = curl_init();
		
		// https handling
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_HEADER, true);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
		curl_setopt($ch, CURLOPT_POST, true);
		curl_setopt($ch, CURLOPT_HEADER,'Content-Type: application/x-www-form-urlencoded');
		curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
		      
		$result = curl_exec($ch);
		echo '<pre>';var_dump(curl_getinfo($ch));echo '</pre>';
		echo '<pre>';var_dump($result);echo '</pre>';
	}
	
	/**
	 * Function to make a request to a Prot-On REST service
	 * @param  $userId the user id
	 * @param  $accessToken the OAuth access token
	 * @param  $serviceToCall a Prot-On REST service to call
	 */
	function call($accessToken, $serviceToCall){
	
		$url = "https://dnd.prot-on.com/rest-api/api/".$serviceToCall;     

		$ch = curl_init();
		
		// https handling
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_POST, true);
		
		curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
			'Content-Type: application/json',
			'Authorization: Bearer ' . $accessToken)                                                                       
		);
		
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);       
		$result = json_decode(curl_exec($ch));
		echo '<pre>'.$result.'</pre>';
	}
?>