Using GET and POST when calling activities with HANO API services in PHP

This example will properly show you how to implement POST and GET calls to Hano API including the required parameters. Well you will have to have a valid API key, as I will comment out mine. But I assure you, this code is working at the time I am writing this article. I didn’t cover all available calls to HANO API, but this is even more than enough to get you going in the right direction. Current code actually retrieves different booking types, available booking times from the HANO service, and in the end using the acquired info, tries to create a customer and then book that customer for specific booking window frame.

Solution:

These are the configuration and helper function scripts that will help you communicate with HANO API

<?php
///////////////////////////////////////////////////////////////////////
// Begin configuration for Hano Api 
$configurationFor["apiKey"]='Put your own HANO API Key here!';

//test
$configurationFor["HanoServiceUrl"]='https://api.bestille.no/v1test/';
//live
//$configurationFor["HanoServiceUrl"]='https://api.bestille.no/v1/'; 

$configurationFor["resourceId"]='1'; //from https://api.bestille.no/v1/resource/list?apikey='Put your own HANO API Key here!'; 

function CallAPI( $url, $data=false)
{
     if ($data) $url = sprintf("%s?%s", $url, http_build_query($data));	 
	 //print_r($url);	 
	 $curl = curl_init($url);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
		//execute the session
		 $curl_response = curl_exec($curl);
		//finish off the session
			curl_close($curl);
		return $curl_response;
}  

function httpPost($url,$params)
{
  $fields_string =http_build_query($params); 
    $ch = curl_init($url); 
    //curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);  
    //curl_setopt($ch,CURLOPT_HEADER, false); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string); 
    $output=curl_exec($ch);
 
    curl_close($ch);
    return $output; 
}

?>

 

1. Get the HANO API Booking Types and put them in drop-down example

<?php
//fill Dropdown

	echo '<select class="floatLabel" id="BookingType">';
	echo '<option value="">Chose Type Of Booking</option>';
	
	$data["apiKey"]=$configurationFor["apiKey"];
	$response=json_decode(CallAPI($configurationFor["HanoServiceUrl"]."service/list",$data),true);
	foreach ($response as $row) {
	echo '<option value="'.$row['id'].'">'.$row['name'].'</option>';
	}
	
	echo '</select>';
?>

2. Get the HANO API Available Booking Times and put them in option radio button

<?php
//get available dates and put them in option box
   $data["apiKey"]=$configurationFor["apiKey"];

	$data["serviceId"]=3;  //from //2.3.1 List 
	$data["startDate"]="2017-01-11";
	$data["endDate"]="2017-01-11";
	$data["resourceId"]=$configurationFor["resourceId"]; //depends on HANO database configuration

	$response=json_decode(CallAPI($configurationFor["HanoServiceUrl"]."activity/search",$data),true);	
    
    //Display time 
	if(!empty($response['results'])){        
         foreach ($response['results'] as $row) {			 
    		echo '<input type="radio" value="'.$row['time'].'" name="time"/>'.substr($row['start'], -8,5).'-'.substr($row['end'], -8,5).'</br>';	}
    }else{
        echo 'No Booking time available';
    }
?>

3. Create HANO API Customer and then use the customer to book specific time

<?php
$regdata["name"]="John"; 
$regdata["surname"]="Smith"; 
$regdata["email"]="some@email.com";

//add bogus data depending on customer scheme in HANO API Config
$regdata["birthdate"]='2000-01-01';
$regdata["address1"]='No address';
$regdata["zip"]='0000';
$regdata["city"]='city';
$regdata["phone2"]='0000';


$response=json_decode(httpPost($configurationFor["HanoServiceUrl"]."customer/register?apiKey=".$configurationFor["apiKey"],$regdata),true);	   

if($response['success']=='true'){
   // now add the booking to that user
   
   $data["apiKey"]=$configurationFor["apiKey"];
	$data["serviceId"]="3";  //from //2.3.1 List
	$data["timeId"]="59208"; 
	$data["customerId"]=$response["customer"]["id"]; 
	$data["password"]=$response["customer"]["password"];

	//print_r($data);
	
	$responseBook=json_decode(CallAPI($configurationFor["HanoServiceUrl"]."activity/create",$data),true);

		//Display book result
		if($responseBook['success']=='true'){
		   echo '<h2 class="heading">Booking Success for:</h2>
				<div class="controls">
				  <input type="text" id="name" class="floatLabel" name="name" value="'.$regdata["name"].'">				  
				</div>
				<div class="controls">
				  <input type="tel" id="surname" class="floatLabel" name="surname" value="'.$regdata["surname"].'">				  
				</div>
				<div class="controls">
				  <input type="text" id="email" class="floatLabel" name="email" value="'.$regdata["email"].'">				  
				</div>					
				<div class="controls">
				  <input type="text" id="id" class="floatLabel" name="email" value="'.$responseBook["id"].'">				  
				</div>    ';						
		
		}else{
			echo '<span>Error Booking! </span>'.print_r($responseBook);
		}			
	
}else{		 
	echo '<span>Error Booking Customer </span>' .print_r($response);
}
?>

 

1. Output of Get the HANO API Booking Types and put them in drop-down example

<select class="floatLabel" id="BookingType">
	<option value="">Chose Type Of Booking</option>
	<option value="3">Konsultasjon Ny kunde</option>
	<option value="12">Mikrotatovering</option>
	<option value="8">Liten tatovering </option>
	<option value="9">Medium tatovering</option>
	<option value="11">Large tatovering</option>
	<option value="10">XL - Tatovering</option>
	<option value="13">PMU Bryn</option>
	<option value="14">ikke aktiv kunde</option>
	<option value="15">Sett inn navn her</option>
</select>

2. Output of Get the HANO API Available Booking Times and put them in option radio button

<input value="58956" name="time" type="radio">12:00-12:15<br>
<input value="59190" name="time" type="radio">15:15-15:30<br>
<input value="59208" name="time" type="radio">15:30-15:45<br>

3. Output of Create HANO API Customer and then use the customer to book specific time

<h2 class="heading">Booking Success for:</h2>
<div class="controls">
<input id="name" class="floatLabel" name="name" value="John" type="text">
</div>
<div class="controls">
<input id="surname" class="floatLabel" name="surname" value="Smith" type="tel">
</div>
<div class="controls">
<input id="email" class="floatLabel" name="email" value="some@email.com" type="text">
</div>					
<div class="controls">
<input id="id" class="floatLabel" name="email" value="34461" type="text">
</div>    

 

What you may be forgetting:

1.Don’t forget to include the API Key in the Hano Service URL when using POST function

2.Check your HANO API Customer Creation required parameters scheme, as some additional parameters may be required in your DB

3.Make sure you use Time ID when scheduling/booking that is not expired (duh… )

4. This example uses loosely the resource ID parameter (I don’t care to whom to book the time) Maybe you will have to additionally change this parameter to your convenience

This example was done with PHP Version 5.6.15  running on Apache/2.4.17 (Win32)

Leave a Reply

Your email address will not be published. Required fields are marked *