Below is an end-to-end example of submitting a request for various methods using PHP programming language:
<?php
// Simple script to showcasing how to queue up and run multiple requests using the LEADTOOLS CloudServices.
// In order to run this script, the following changes will need to be added:
// 1) Place your Application ID in the $appId variable in the GeneratePostOptions and GeneratePostOptionsUpload methods
// 2) Place your Application Password in the $password variable in the GeneratePostOptions and GeneratePostOptionsUpload methods
// The script will perform the following operations in order:
// 1)Perform an UploadFile request to the LEADTOOLS CloudServices. A successfully made request will return a unique identifier.
// We will use this identifier to queue up further methods.
// 2)We will queue up an ExtractText method using the identifier that was returned from the UploadFile method.
// 3)After we verify the ExtractText method was queued, we will then queue up a Conversion method to convert the file to TIFF.
// 4)Once the Conversion request has been queued, we will make a call to the Run method. This will mark the file as ready to be processed.
// **Note** Once a file has been marked for processing, or has finished processing, no further requests can be queued or run on that file.
// 5)We will then query the services using the GUID -- if the file is finished processing, the body will contain all the
// request data.
// 6)We will take the json data that was returned, parse it, and display all the information that was returned.
$servicesBaseUrl = "https://azure.leadtools.com/api/";
//The first page in the file to mark for processing
$firstPage = 1;
//Sending a value of -1 will indicate to the services that the rest of the pages in the file should be processed.
$lastPage = -1;
//Enum corresponding to the output format for the file.
$fileFormat = 4;
//We will be uploading the file via a URl. Files can also be passed by adding a PostFile to the request. Only 1 file will be accepted per request.
//The services will use the following priority when determining what a request is trying to do GUID > URL > Request Body Content
$fileURL = 'http://demo.leadtools.com/images/cloud_samples/ocr1-4.tif';
$uploadUrl = "%sUploadFile?fileurl=%s";
$uploadUrl = sprintf($uploadUrl, $servicesBaseUrl, $fileURL);
/*
If uploading a file alongside the HTTP request:
$uploadUrl = '%sUploadFile';
$uploadUrl = sprintf($uploadUrl, $servicesBaseUrl);
*/
//Generate the CURL options to be used
$requestOptions = GeneratePostOptionsUpload($uploadUrl);
$request = curl_init();
curl_setopt_array($request, $requestOptions); //Set the request URL
if (!$guid = curl_exec($request)) {
if (curl_errno($request)) {
echo "There was an error queueing the request \n\r";
echo curl_error($request);
curl_close($request);
return false;
}
}
curl_close($request);
//The request completed successfully. Now that we have the GUID, we can use it to queue up requests for the file.
echo "Unique ID returned by the services: $guid \n\r";
$queryUrl = "%sQuery?id=%s";
$queryUrl = sprintf($queryUrl, $servicesBaseUrl, $guid);
$requestOptions = GeneratePostOptions($queryUrl);
while (true) { //Poll the services to determine if the request has finished processing.
$request = curl_init();
curl_setopt_array($request, $requestOptions); //Set the request URL
if (!$results = curl_exec($request)) {
if (curl_errno($request)) {
echo "There was an error queueing the request \n\r";
echo curl_error($request);
curl_close($request);
return false;
}
}
curl_close($request);
$decodedResponse = json_decode($results);
$fileStatus = $decodedResponse->{'FileStatus'};
if ($fileStatus != 123) {
//The file has finished verifying
break;
}
sleep(5); //If the file hasn't finished processing, we will wait 5 seconds before checking again.
}
$extractTextUrl = "%sRecognition/ExtractText?firstPage=%s&lastPage=%s&guid=%s";
$extractTextUrl = sprintf($extractTextUrl, $servicesBaseUrl, $firstPage, $lastPage, $guid);
$requestOptions = GeneratePostOptions($extractTextUrl);
if (!SendRequest($requestOptions))
exit;
echo "ExtractText queued successfully \n\r";
$conversionUrl = "%sConversion/Convert?firstPage=%s&lastPage=%s&format=%s&guid=%s";
$conversionUrl = sprintf($conversionUrl, $servicesBaseUrl, $firstPage, $lastPage, $fileFormat, $guid);
$requestOptions = GeneratePostOptions($conversionUrl);
if (!SendRequest($requestOptions))
exit;
echo "Conversion queued successfully \n\r";
$runUrl = "%sRun?id=%s";
$runUrl = sprintf($runUrl, $servicesBaseUrl, $guid);
$requestOptions = GeneratePostOptions($runUrl);
if (!SendRequest($requestOptions))
exit;
echo "File has successfully been marked to run \n\r";
$requestOptions = GeneratePostOptions($queryUrl);
while (true) { //Poll the services to determine if the request has finished processing.
$request = curl_init();
curl_setopt_array($request, $requestOptions); //Set the request URL
if (!$results = curl_exec($request)) {
if (curl_errno($request)) {
echo "There was an error queueing the request \n\r";
echo curl_error($request);
curl_close($request);
return false;
}
}
curl_close($request);
$decodedResponse = json_decode($results);
$fileStatus = $decodedResponse->{'FileStatus'};
if ($fileStatus != 100) {
//The file has finished processing
break;
}
sleep(5); //If the file hasn't finished processing, we will wait 5 seconds before checking again.
}
echo "File finished processing with file status: " . $fileStatus . "\n\r";
//The file did not process successfully. Refer to our documentation for the full list of File Statuses and their associated meanings.
if ($fileStatus != 200) {
exit;
}
echo "Results: \n\r";
//Decode and parse the JSON results.
$jsonArray = $decodedResponse->{'RequestData'};
foreach ($jsonArray as $serviceResults) {
$serviceType = $serviceResults->{'ServiceType'};
echo "Service Type: " . $serviceType . "\n\r";
if ($serviceType == "Conversion") {
//Parse the returned URLS
echo "URL List: \n\r";
foreach ($serviceResults->{'urls'} as $url) {
echo $url . "\n\r";
}
} else if ($serviceType == "Recognition") {
$recognitionType = $serviceResults->{'RecognitionType'};
echo "Recognition Type: " . $recognitionType . "\n\r";
if ($recognitionType == "ExtractText") {
echo "Recognition Results: " + $serviceResults->{'data'};
}
} else {
echo "Service Type not recognized: " . $serviceType;
exit;
}
}
function GeneratePostOptionsUpload($url)
{
//Function to generate the array of CURL options to be used when uploading a file.
//If we are uploading via a URL, we will need to add a Content-Length:0 header value.
//If we are uploading a file as post data, we do not need to include the Content-Length header.
$appId = "Replace with Application ID";
$password = "Replace with Application Password";
//Remove if uploading the file as post data.
$headers = array(
"Content-Length : 0"
);
/*
//To upload a file as form data:
$file = new CURLFile("path/to/file");
$imageData = array('image' => $file);
//**NOTE** - Only 1 file will be accepted per request.
*/
$postOptions = array(
CURLOPT_POST => 1,
CURLOPT_URL => $url,
CURLOPT_FRESH_CONNECT => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_USERPWD => "$appId:$password",
CURLOPT_FORBID_REUSE => 1,
//Uncomment if uploading as post data.
//CURLOPT_POSTFIELDS => $imageData,
//Comment out the CURLOPT_HTTPHEADER if uploading as post data.
CURLOPT_HTTPHEADER => $headers
);
return $postOptions;
}
function GeneratePostOptions($url)
{
$appId = "Replace with Application ID";
$password = "Replace with Application Password";
$headers = array(
"Content-Length : 0"
);
$postOptions = array(
CURLOPT_POST => 1,
CURLOPT_URL => $url,
CURLOPT_FRESH_CONNECT => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_USERPWD => "$appId:$password",
CURLOPT_FORBID_REUSE => 1,
CURLOPT_HTTPHEADER => $headers
);
return $postOptions;
}
function SendRequest($requestOptions)
{
$request = curl_init();
curl_setopt_array($request, $requestOptions);
if (!$results = curl_exec($request)) {
if (curl_errno($request)) {
echo "There was an error queueing the request \n\r";
echo curl_error($request);
curl_close($request);
return false;
}
}
$httpReponse = curl_getinfo($request, CURLINFO_HTTP_CODE);
curl_close($request);
if ($httpReponse != 200) {
//The file was not queued successfully.
echo "There was an error queueing the request \n\r";
return false;
}
return true;
}
?>