How to Upload a Single Binary File to pdfRest with PHP
In this tutorial, we'll use PHP and the Upload Files API Tool to upload one raw binary file to pdfRest. The complete Upload a Single Binary to pdfRest repository sample stays visible while we explain the request fields and response behavior a PHP application must adapt safely.
Why Upload a Single Binary File to pdfRest with PHP?
Uploading a source once can simplify a multi-step workflow that references the same document repeatedly. The returned resource ID allows compatible API Tools to use the managed file without another byte transfer.
A PHP invoice service might stage a large statement before applying several operations, such as compression and encryption. Keeping the ID in the job record separates the initial file transfer from each later processing call.
This example uses raw binary upload rather than multipart form data. The file bytes become the HTTP body, Content-Type is application/octet-stream, and content-filename supplies the resource name that multipart metadata would ordinarily carry.
PHP Code Example
require 'vendor/autoload.php'; // Require the autoload file to load Guzzle HTTP client.
use GuzzleHttp\Client; // Import the Guzzle HTTP client namespace.
use GuzzleHttp\Psr7\Request; // Import the PSR-7 Request class.
use GuzzleHttp\Psr7\Utils; // Import the PSR-7 Utils class for working with streams.
// By default, we use the US-based API service. This is the primary endpoint for global use.
$apiUrl = "https://api.pdfrest.com";
/* For GDPR compliance and enhanced performance for European users, you can switch to the EU-based service by uncommenting the URL below.
* For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work
*/
//$apiUrl = "https://eu-api.pdfrest.com";
$client = new Client(['http_errors' => false]);
$headers = [
'api-key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
'content-filename' => 'filename.pdf',
'Content-Type' => 'application/octet-stream'
];
$body = file_get_contents('/path/to/file');
$request = new Request('POST', $apiUrl.'/upload', $headers, $body);
$res = $client->sendAsync($request)->wait();
echo $res->getBody();
Source for Upload a Single Binary to pdfRest: View the PHP sample on GitHub.
Breaking Down the Code
Load Guzzle and its stream helpers
require 'vendor/autoload.php'; // Require the autoload file to load Guzzle HTTP client. use GuzzleHttp\Client; // Import the Guzzle HTTP client namespace. use GuzzleHttp\Psr7\Request; // Import the PSR-7 Request class. use GuzzleHttp\Psr7\Utils; // Import the PSR-7 Utils class for working with streams.
Guzzle supplies the HTTP client and PSR-7 request objects used throughout the Upload a Single Binary to pdfRest example. For this PHP Upload a Single Binary to pdfRest flow, Utils::tryFopen creates readable multipart streams without loading every uploaded byte into one string.
Select the pdfRest service region
// By default, we use the US-based API service. This is the primary endpoint for global use. $apiUrl = "https://api.pdfrest.com"; /* For GDPR compliance and enhanced performance for European users, you can switch to the EU-based service by uncommenting the URL below. * For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work */ //$apiUrl = "https://eu-api.pdfrest.com"; $client = new Client(['http_errors' => false]); $headers = [
The active $apiUrl selects the US service for Upload a Single Binary to pdfRest; the commented assignment shows the EU alternative. Keep the PHP uploads, request IDs, and generated resources for Upload a Single Binary to pdfRest on that same regional host.
Build the raw binary upload
$headers = [
'api-key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
'content-filename' => 'filename.pdf',
'Content-Type' => 'application/octet-stream'
];
$body = file_get_contents('/path/to/file');
$request = new Request('POST', $apiUrl.'/upload', $headers, $body);
$res = $client->sendAsync($request)->wait();
echo $res->getBody();
This PHP raw-upload request places file bytes directly in the HTTP body. Because this PHP Upload a Single Binary to pdfRest call has no multipart file part, content-filename supplies the resource name.
Send the body and inspect the response
];
$body = file_get_contents('/path/to/file');
$request = new Request('POST', $apiUrl.'/upload', $headers, $body);
$res = $client->sendAsync($request)->wait();
echo $res->getBody();
The final Upload a Single Binary to pdfRest handling in PHP exposes the HTTP result and JSON body. Before using a Upload a Single Binary to pdfRest output or request ID, production PHP code should verify the status and retain the identifier required by the next step.
Beyond the Tutorial
You can now stage one file with a raw binary PHP request and carry its managed resource ID into compatible pdfRest operations.
Use a meaningful filename, verify the returned files entry, and retain its ID only as long as the workflow needs it. Resource storage is temporary, so download durable results or complete dependent calls before retention expires.
Try the Upload a Single Binary to pdfRest workflow with a representative file in API Lab, then adapt the request in PHP. The Upload Files API Tool documentation provides the complete PHP context for Upload a Single Binary to pdfRest values, defaults, and limitations.