Below is an end-to-end example of submitting a Merge request using PHP programming language:



<?php
// Simple script to showcasing how to use the Merge API in the LEADTOOLS Cloud Services.
// 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) Upload 2 files to the LEADTOOLS Cloud Services using the forMerge flag.
//   2) Make a call to the Merge end-point to combine the two files.
//   3) Parse the JSON output from the LEADTOOLS Cloud Services

$servicesBaseUrl = "https://azure.leadtools.com/api/";

// 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 Merge Api requires you to upload your files with the forMerge parameter set.  A file that has been uploaded and tagged as forMerge is not compatible with other LEADTOOLS Cloud Services API functions.

$tiffFileURL = "http://demo.leadtools.com/images/cloud_samples/ocr1-4.tif";
$firstFileId = UploadFileForMerge($servicesBaseUrl,$tiffFileURL);
if(!$firstFileId){
    echo "Error sending the upload request for file: $tiffFileURL \n\r";
    return;
}
echo "First File ID: $firstFileId \n\r";
if(!CheckFileVerification($servicesBaseUrl, $firstFileId))
    return;

$pdfFileUrl = "https://demo.leadtools.com/images/pdf/leadtools.pdf";
$secondFileId = UploadFileForMerge($servicesBaseUrl, $pdfFileUrl);
if(!$secondFileId){
    echo "Error sending the upload request for file: $pdfFileUrl";
    return;
}
echo "Second File ID: $secondFileId \n\r";
if(!CheckFileVerification($servicesBaseUrl, $secondFileId))
    return;

// We now need to create the JSON body response for the Merge Api Command
$data = [];

$firstFileData = array(
    "firstPage" => 1,
    "lastPage" => -1,
    "fileId" => $firstFileId
);
$secondFileData = array(
    "pages" => [5,1,2,3,4],
    "fileId" => $secondFileId
);
array_push($data, $firstFileData, $secondFileData);

$fileFormat = FormatsEnum::Pdf;
$mergeUrl = "%sConversion/Merge?format=%s";
$mergeUrl = sprintf($mergeUrl, $servicesBaseUrl, $fileFormat);
$mergeOptions = GeneratePostOptionsMerge($mergeUrl, $data);

$r = curl_init();
curl_setopt_array($r, $mergeOptions); //Set the request URL

if (!$id = curl_exec($r)) {
    echo "There was an error sending the merge request \n\r";
    echo $id;
    exit;
}
curl_close($r); //Close the request


echo "Querying Results...\n\r";
$baseQueryURL = '%sQuery?id=%s';
$formattedQueryURL = sprintf($baseQueryURL, $servicesBaseUrl, $firstFileId);
$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 UploadFileForMerge($servicesBaseUrl, $fileUrl)
{
    $uploadUrl = "%sUploadFile?forMerge=true&fileUrl=%s";
    $uploadUrl = sprintf($uploadUrl, $servicesBaseUrl, $fileUrl);
    $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 uploading the file\n\r";
            echo curl_error($request);
            curl_close($request);
            return false;
        }
    }
    curl_close($request);

    return $guid;
}

function GeneratePostOptionsMerge($url, $jsonData)
{
    $options = GeneratePostOptionsUpload($url);
    //remove 0 content-length header
    unset($options['CURLOPT_HTTPHEADER']);

    $encodedData = json_encode($jsonData);
    $options[CURLOPT_POSTFIELDS] = $encodedData;
    $options[CURLOPT_HTTPHEADER] = array(
        'Content-Type: application/json',
        'Content-Length: ' . strlen($encodedData)
    );
    return $options;
}

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.

    $options = GeneratePostOptions($url);
    /*
        //To upload a file as form data:
        $file = new CURLFile("path/to/file");
        $imageData = array('image' => $file);
        $options[CURLOPT_POSTFIELDS] = $imageData;
        unset($options['CURLOPT_HTTPHEADER']);
        //**NOTE** - Only 1 file will be accepted per request.
    */
    return $options;
}

function GeneratePostOptions($url)
{
    $appId    = "unittestid";
    $password = "A_K|}y-.P8*Uw7iP2S.KzqmL";

    $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 CheckFileVerification($servicesBaseUrl,$id)
{
    $queryUrl = "%sQuery?id=%s";
    $queryUrl = sprintf($queryUrl, $servicesBaseUrl, $id);
    $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) {
            if($fileStatus != 122){
                echo "The file has failed the verification process with File Status $fileStatus";
                return false;
            }

            return true;
        }
        sleep(5); //If the file hasn't finished processing, we will wait 5 seconds before checking again.
    }
}

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;
}

?>