Skip to content

Commit 77c462d

Browse files
php: Add TDM samples
Assisted-by: Codex
1 parent 11dd291 commit 77c462d

2 files changed

Lines changed: 88 additions & 0 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
require 'vendor/autoload.php'; // Require the autoload file to load Guzzle HTTP client.
3+
4+
use GuzzleHttp\Client; // Import the Guzzle HTTP client namespace.
5+
use GuzzleHttp\Psr7\Request; // Import the PSR-7 Request class.
6+
use GuzzleHttp\Psr7\Utils; // Import the PSR-7 Utils class for working with streams.
7+
8+
// By default, we use the US-based API service. This is the primary endpoint for global use.
9+
$apiUrl = "https://api.pdfrest.com";
10+
11+
/* For GDPR compliance and enhanced performance for European users, you can switch to the EU-based service by uncommenting the URL below.
12+
* For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work
13+
*/
14+
//$apiUrl = "https://eu-api.pdfrest.com";
15+
16+
$upload_client = new Client(['http_errors' => false]);
17+
$upload_headers = [
18+
'api-key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
19+
'content-filename' => 'filename.pdf',
20+
'Content-Type' => 'application/octet-stream'
21+
];
22+
$upload_body = file_get_contents('/path/to/file');
23+
$upload_request = new Request('POST', $apiUrl.'/upload', $upload_headers, $upload_body);
24+
$upload_res = $upload_client->sendAsync($upload_request)->wait();
25+
echo $upload_res->getBody() . PHP_EOL;
26+
27+
$upload_response_json = json_decode($upload_res->getBody());
28+
29+
$uploaded_id = $upload_response_json->{'files'}[0]->{'id'};
30+
31+
echo "Successfully uploaded with an id of: " . $uploaded_id . PHP_EOL;
32+
33+
$tdm_reserved_pdf_client = new Client(['http_errors' => false]);
34+
$tdm_reserved_pdf_headers = [
35+
'api-key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
36+
'Content-Type' => 'application/json'
37+
];
38+
$tdm_reserved_pdf_body = '{"id":"'.$uploaded_id.'", "policy":"https://example.com/tdm-policy"}';
39+
$tdm_reserved_pdf_request = new Request('POST', $apiUrl.'/tdm-reserved-pdf', $tdm_reserved_pdf_headers, $tdm_reserved_pdf_body);
40+
$tdm_reserved_pdf_res = $tdm_reserved_pdf_client->sendAsync($tdm_reserved_pdf_request)->wait();
41+
echo $tdm_reserved_pdf_res->getBody() . PHP_EOL;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
require 'vendor/autoload.php'; // Require the autoload file to load Guzzle HTTP client.
3+
4+
use GuzzleHttp\Client; // Import the Guzzle HTTP client namespace.
5+
use GuzzleHttp\Psr7\Request; // Import the PSR-7 Request class.
6+
use GuzzleHttp\Psr7\Utils; // Import the PSR-7 Utils class for working with streams.
7+
8+
// By default, we use the US-based API service. This is the primary endpoint for global use.
9+
$apiUrl = "https://api.pdfrest.com";
10+
11+
/* For GDPR compliance and enhanced performance for European users, you can switch to the EU-based service by uncommenting the URL below.
12+
* For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work
13+
*/
14+
//$apiUrl = "https://eu-api.pdfrest.com";
15+
16+
$client = new Client(); // Create a new instance of the Guzzle HTTP client.
17+
18+
$headers = [
19+
'Api-Key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' // Set the API key in the headers for authentication.
20+
];
21+
22+
$options = [
23+
'multipart' => [
24+
[
25+
'name' => 'file', // Specify the field name for the file.
26+
'contents' => Utils::tryFopen('/path/to/file', 'r'), // Open the file specified by the '/path/to/file' for reading.
27+
'filename' => '/path/to/file', // Set the filename for the file to be processed, in this case, '/path/to/file'.
28+
'headers' => [
29+
'Content-Type' => '<Content-type header>' // Set the Content-Type header for the file.
30+
]
31+
],
32+
[
33+
'name' => 'policy', // Specify the field name for the policy string.
34+
'contents' => 'https://example.com/tdm-policy' // Set a dummy URL policy value.
35+
],
36+
[
37+
'name' => 'output', // Specify the field name for the output option.
38+
'contents' => 'pdfrest_tdm_reserved_pdf' // Set the value for the output option.
39+
]
40+
]
41+
];
42+
43+
$request = new Request('POST', $apiUrl.'/tdm-reserved-pdf', $headers); // Create a new HTTP POST request with the API endpoint and headers.
44+
45+
$res = $client->sendAsync($request, $options)->wait(); // Send the asynchronous request and wait for the response.
46+
47+
echo $res->getBody(); // Output the response body, which contains the PDF with TDM rights metadata.

0 commit comments

Comments
 (0)