Below is an end-to-end example of submitting a ConvertRedact request using PHP programming language:
<?php
// Simple script to make and process the results of a ConvertRedact request to 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 method
// 2) Place your Application Password in the $password variable in the GeneratePostOptions method
//
// The script will perform the following operations in order:
// 1)Perform a ConvertRedact request to the LEADTOOLS CloudServices. A successfully made request will return a unique identifier.
// 2)We will then query the services using the GUID -- if the file is finished processing, the body will contain all the
// request data.
// 3)We will take the json data that was returned, parse it, and display all the information that was returned.
abstract class FormatsEnum
{
const Png = 1;
const Jpeg = 2;
const Tiff = 3;
const Pdf = 4;
const Pdfa = 5;
const PdfImage = 6;
const PdfImageOverText = 7;
const PdfaImageOverText = 8;
const Docx = 9;
const DocxFramed = 10;
const Rtf = 11;
const RtfFramed = 12;
const Txt = 13;
const TxtFramed = 14;
}
$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. For the purposes of this script, we will be converting to tif.
$fileFormat = FormatsEnum::Tiff;
$pattern = '(LEAD)';
//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 = 'https://demo.leadtools.com/images/pdf/leadtools.pdf';
$baseConverionURL = '%sConversion/ConvertRedact?firstPage=%s&lastPage=%s&format=%s&fileurl=%s&pattern=%s';
$formattedConversionURL = sprintf($baseConverionURL, $servicesBaseUrl, $firstPage, $lastPage, $fileFormat, $fileURL, $pattern);
/*
If uploading a file alongside the HTTP request:
$baseConverionURL = '%sConversion/ConvertRedact?firstPage=%s&lastPage=%s&format=%s&pattern=%s';
$formattedConversionURL = sprintf($baseConverionURL, $servicesBaseUrl, $firstPage, $lastPage, $fileFormat, $pattern));
*/
//Generate the CURL options to be used
$conversionRequestOptions = GeneratePostOptions($formattedConversionURL);
$request = curl_init();
curl_setopt_array($request, $conversionRequestOptions); //Set the request URL
if (!$guid = curl_exec($request)) {
echo "There was an error processing the request. \n\r";
echo $guid;
exit;
}
curl_close($request); //Close the request
echo "Unique ID returned by the services: $guid \n\r";
echo "Querying Results...\n\r";
$baseQueryURL = '%sQuery?id=%s';
$formattedQueryURL = sprintf($baseQueryURL, $servicesBaseUrl, $guid);
$queryRequestOptions = GeneratePostOptions($formattedQueryURL);
while (true) { //Poll the services to determine if the request has finished processing.
$request = curl_init();
curl_setopt_array($request, $queryRequestOptions); //Set the request URL
if (!$results = curl_exec($request)) {
echo "There was an error querying the services \n\r";
echo $results;
curl_close($request);
exit;
}
curl_close($request);
$decodedResponse = json_decode($results);
$fileStatus = $decodedResponse->{'FileStatus'};
if ($fileStatus != 100 && $fileStatus != 123) {
//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) {
echo "Service Type: " . $serviceResults->{'ServiceType'} . "\n\r";
echo "URL List: \n\r";
foreach ($serviceResults->{'urls'} as $url) {
echo $url . "\n\r";
}
}
function GeneratePostOptions($url)
{
//Function to generate the array of CURL options to be used.
//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;
}
?>