Positive ID

An example Integration Service API in PHP

View Raw View Documentation
<?php

/**
* Check the metadata object contains valid data according to your needs.
* 
* @param object $metadata the object to validate
* @param array $image the image array
* 
* @return bool TRUE if metadata object is valid. FALSE if metadata object is invalid
*/

function validate($metadata, $image){
	return true;
}

/**
* Helper function
* Get an error object
* 
* @param string $context an error message description
* @param string $status an HTTP status reason. Potential values are 'Bad Request', 'Unauthorized'...
* @param int $code an HTTP status code. Potential values are 400, 404, 401...
* 
* @return string an error response as json string
*/

function echoErrorMessage($context, $status, $code){
	$errorObj = new StdClass();
	$errorObj->context = $context;
	$errorObj->status = $status;
	$errorObj->code = $code;

	// START: Example purposes
	echo "Error:\n";
	print_r($errorObj);
	// END: Example purposes
	
	header("Content-Type: application/json; charset=utf-8");
	header("HTTP/2.0 " . $code . " " . $status, true, $code);
	echo json_encode($errorObj);
}

/**
* Helper function -- A function where you could call third party software 
* in order to gather specific information on a subject.
* 
* @param object $metadata The subject's metadata
* @param array $image The subject's image array or null
* 
* @return object
*/

function callSystems($metadata, $image){
	$responseObject = new StdClass();

	$responseObject->result = 1;
	$responseObject->resultReason = "OK";
	$responseObject->moreInfo = "https://positiveidcheck.com"; // a link containing information on regards to the result.
	$responseObject->source = "Positive ID"; // an optional field containing the name of the source where the result comes from.

	return $responseObject;
}

/**
* Helper function -- Drives the request validation and processing.
* 
* @param string $metadata The subject's metadata json string
* @param array $image The subject's image array or null
* 
* @return string a json string response
*/

function processRequest($metadata, $image){
	$objMetadata = json_encode($metadata, false){

	// START: Example purposes
	echo "Input:\n";
	print_r($objMetadata);
	// END: Example purposes

	// Validate posted data goes according your minimum requirements
	$isValid = validate($objMetadata, $image);
	if($isValid){
		// $metadata, or both $image and $metadata are valid.
		// Here you could call external systems to gather information and return an output
		$systemResponse = callSystems($objMetadata, $image);
		
		// START: Example purposes
		echo "\nResponse:\n";
		print_r($systemResponse);
		// END: Example purposes

		header("Content-Type: application/json; charset=utf-8");
		header("HTTP/2.0 200 OK", true, 200);
		echo json_encode($systemResponse);
	} else {
		echoErrorMessage("Received data isn't valid", "Bad Request", 200);
	}
}

if ($_SERVER['REQUEST_METHOD'] !== 'POST'){
	echoErrorMessage("This method is not allowed", "Method Not Allowed", 405);
	return;
}

// Get posted metadata object

/* 
$_POST['metadata'] = { 
	"identifier": "string", // null if couldn't get it
	"name": "string", // null if couldn't get it
	"state": "string", // null if couldn't get it
	"country": "string", // null if couldn't get it
	"birthDate": epoch1970seconds, // seconds since 1970 or -1 if I couldn't get it
	"issueDate": epoch1970seconds, // seconds since 1970 or -1 if I couldn't get it
	"expirationDate": epoch1970seconds, // seconds since 1970 or -1 if I couldn't get it
	"sex": "M" // M, F, X, U
}
*/
/*
* IMPORTANT: Harcoded metadata object is for the purpose of this example only. 
* Real metadata comes from the $_POST['metadata'] variable.
*/
$metadata = $_POST["metadata"];
$metadata = '{"identifier":"ABC1234", "name":"John Doe", "state":"NY", "country":"USA", "birthDate":654753185, "issueDate":1222833185, "expirationDate":1349063585, "sex":"M"}';

// Get posted image bytes (OPTIONAL)
/* 
[ 'name', 'type', 'size', 'tmp_name', 'error' ]
See https://www.php.net/manual/en/features.file-upload.post-method.php
*/

$image = $_FILES['image']; // Image is an optional

// Start processing the request
processRequest($metadata, $image);

?>

Input Example

stdClass Object
(
	[identifier] => ABC1234
	[name] => John Doe
	[state] => NY
	[country] => USA
	[birthDate] => 654753185
	[issueDate] => 1222833185
	[expirationDate] => 1349063585
	[sex] => M
)
	

Output Example

stdClass Object
(
	[result] => 1
	[resultReason] => OK
	[moreInfo] => https://positiveidcheck.com
	[source] => Positive ID
)