<!DOCTYPE html>
	<html>
		<head>
			<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
			<meta http-equiv="X-UA-Compatible" content="IE=edge">
			<meta charset="utf-8">
			<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
			<title>Data API</title>

		</head>
            
		<body>
            <?php

                // Use the below three lines to show errors on the page
				ini_set('display_errors', 1);
				ini_set('display_startup_errors', 1);
				error_reporting(E_ALL);


                // Function to get an authorization token which will be used to get data from the database - note, environment variables are set just before function is called
				function get_token($host,$username,$password,$payloadName) {

					$additionalHeaders = '';
					$ch = curl_init($host);
					curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', $additionalHeaders));
					curl_setopt($ch, CURLOPT_HEADER, 0);
					curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
					curl_setopt($ch, CURLOPT_TIMEOUT, 30);
					curl_setopt($ch, CURLOPT_POST, 1);
					curl_setopt($ch, CURLOPT_POSTFIELDS, $payloadName);
					curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
					$result = curl_exec($ch); // Execute the cURL statement
					curl_close($ch); // Close the cURL connection

					// Decode the resulting JSON
					$json_token = json_decode($result, true);

					// Extract just the token value from the JSON result
					$token_received = $json_token['response']['token'];
					
					// Return the token from this function
					return($token_received);

				};


                // Function to get data from the database using the token from the function above - note, environment variables are set just before function is called
				function get_data($host,$token,$payloadName) {
				
					$curl = curl_init($host);
					curl_setopt($curl, CURLOPT_URL, $host);
					curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
					
					$headers = array(
					   "Content-Type: application/json",
					   "Accept: */*",
					   "Authorization: Bearer ".$token,
					);
					curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
					//for debug only!
					curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
					curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
					
					$resp = curl_exec($curl);
					curl_close($curl);
					var_dump($resp);
					return json_decode($resp);
		            

				};


                // Function to delete the authorization token
                function delete_token($host) {

                    $additionalHeaders = '';
                    $payloadName = '';
                    $ch = curl_init($host);
                    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', $additionalHeaders));
                    curl_setopt($ch, CURLOPT_HEADER, 0);
                    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
                    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); // Specify the request method as DELETE
                    curl_setopt($ch, CURLOPT_POSTFIELDS, $payloadName);
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                    $result = curl_exec($ch); // Execute the cURL statement
                    curl_close($ch); // Close the cURL connection

                    // Return the result
                    return($result);

                };


				// Set up the environment variables to get a token
				$host = 'http://2.238.144.32:1895/fmi/data/v1/databases/claudio/sessions';
				$username = '';
				$password = '';
				$payloadName = '';

				// Call the get_token function and put the result into a variable for use in the get_data function
				$token = get_token($host,$username,$password,$payloadName);
				// To display the token on the webpage, enabled the line below
				//echo $token;


				// Now that we have a token, set up the environment variables and call the get_data function to get the data
				$host = 'http://2.238.144.32:1895/fmi/data/v1/databases/claudio/layouts/contatti/records';
				$payloadName = '{}';

				// Call the get_data function
				$request = get_data($host,$token,$payloadName);

				// To show the raw data on the webpage, enable the line below
				//var_dump($request);

				// Extract a single field of data into a variable - this may need adjustment based on the circumstances
				$data_retrieved = $request->response;


				// We're done with the token, so for security purposes, delete the token
				$host = 'https://a996438.fmphost.com/fmi/data/v1/databases/contacts/sessions/'.$token;

				// Call the delete function
				$token_deleted = delete_token($host);
				// To view the result of this call, enable the line below
				//echo($token_deleted);

            ?>
			<p>Data retrived from fileMaker database</p>
			<p style="color:red">This is the session token: <span style="color: black"><?php echo $token ?></span></p>
			<!-- Using html, show the various parts on the webpage -->
			<hr>
			<?php for ($i=0; $i < count($data_retrieved->data) ; $i++) :?>			
			<p style="color:red">Name: <span style="color:black"><?php echo $data_retrieved->data[$i]->fieldData->{'Name'} ?></span></p>
			<p style="color:red">Phone: <span style="color:black"><?php echo $data_retrieved->data[$i]->fieldData->{'phone'} ?></span></p>
			
			<hr>
			<?php endfor; ?>
			<p>This is the result of the request to delete the token: <?php echo $token_deleted ?></p>
		</body>
	</html>