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_decode($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", 400); } } 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); ?>