How to Merge Text Files in PHP

Cloudmersive
2 min readMay 8, 2024

--

Whether we have code, data, or prose stored in our plain text files, we can easily merge those text files with a quick low-code API call.

We can structure our text merging API call using PHP code examples provided below, and we can carry out this operation up to 800 times per month by authorizing our requests with a free Cloudmersive API key.

We can install the PHP client with composer by executing the below command from our command line:

composer require cloudmersive/cloudmersive_document_convert_api_client

And after that, we can copy the below code examples to call the text file merging function (we can copy our free API key in the $config snippet at the top):

<?php
require_once(__DIR__ . '/vendor/autoload.php');

// Configure API key authorization: Apikey
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');



$apiInstance = new Swagger\Client\Api\MergeDocumentApi(


new GuzzleHttp\Client(),
$config
);
$input_file1 = "/path/to/inputfile"; // \SplFileObject | First input file to perform the operation on.
$input_file2 = "/path/to/inputfile"; // \SplFileObject | Second input file to perform the operation on.
$input_file3 = "/path/to/inputfile"; // \SplFileObject | Third input file to perform the operation on.
$input_file4 = "/path/to/inputfile"; // \SplFileObject | Fourth input file to perform the operation on.
$input_file5 = "/path/to/inputfile"; // \SplFileObject | Fifth input file to perform the operation on.
$input_file6 = "/path/to/inputfile"; // \SplFileObject | Sixth input file to perform the operation on.
$input_file7 = "/path/to/inputfile"; // \SplFileObject | Seventh input file to perform the operation on.
$input_file8 = "/path/to/inputfile"; // \SplFileObject | Eighth input file to perform the operation on.
$input_file9 = "/path/to/inputfile"; // \SplFileObject | Ninth input file to perform the operation on.
$input_file10 = "/path/to/inputfile"; // \SplFileObject | Tenth input file to perform the operation on.

try {
$result = $apiInstance->mergeDocumentTxtMulti($input_file1, $input_file2, $input_file3, $input_file4, $input_file5, $input_file6, $input_file7, $input_file8, $input_file9, $input_file10);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling MergeDocumentApi->mergeDocumentTxtMulti: ', $e->getMessage(), PHP_EOL;
}
?>

Now we can supply our text file paths in each $input_file variable, and we’re good to go!

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet