start copy from cimac web
This commit is contained in:
@@ -0,0 +1,155 @@
|
||||
<!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) {
|
||||
|
||||
|
||||
$url = $host;
|
||||
|
||||
$curl = curl_init($url);
|
||||
curl_setopt($curl, CURLOPT_URL, $url);
|
||||
curl_setopt($curl, CURLOPT_POST, true);
|
||||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
||||
|
||||
$headers = array(
|
||||
"Accept: application/json",
|
||||
"Authorization: Bearer ".$token,
|
||||
"Content-Type: application/json",
|
||||
);
|
||||
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
|
||||
|
||||
$data = <<<DATA
|
||||
{"fieldData":
|
||||
{
|
||||
"name": "cla",
|
||||
"phone" : "123456789"
|
||||
}
|
||||
}
|
||||
DATA;
|
||||
|
||||
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
|
||||
|
||||
//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 = 'https://a996438.fmphost.com/fmi/data/v1/databases/contacts/sessions';
|
||||
$username = 'Admin';
|
||||
$password = '1234';
|
||||
$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 = 'https://a996438.fmphost.com/fmi/data/v1/databases/contacts/layouts/FMcontacts/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 added to 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>
|
||||
<p style="color:red">Name: <span style="color:black"><?php echo "pierre" ?></span></p>
|
||||
<p style="color:red">Phone: <span style="color:black"><?php echo "394788483" ?></span></p>
|
||||
|
||||
<hr>
|
||||
<p>This is the result of the request to delete the token: <?php echo $token_deleted ?></p>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user