-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthentication.php
More file actions
31 lines (25 loc) · 944 Bytes
/
Copy pathauthentication.php
File metadata and controls
31 lines (25 loc) · 944 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
/**
* Authentication — verify the API key works by listing your jobs.
*
* Run with:
* API2CONVERT_API_KEY=your-key php examples/authentication.php
*/
declare(strict_types=1);
require __DIR__ . '/../vendor/autoload.php';
use Api2Convert\Api2Convert;
use Api2Convert\Exception\AuthenticationException;
use Api2Convert\Exception\Api2ConvertException;
// The constructor reads API2CONVERT_API_KEY. Every request authenticates with it
// via the X-Api2convert-Api-Key header — the SDK never logs it or puts it in an error.
$client = new Api2Convert();
try {
$jobs = $client->jobs()->list();
echo 'Authenticated. Jobs on this key: ' . count($jobs) . "\n";
} catch (AuthenticationException $e) {
fwrite(STDERR, 'Authentication failed (bad or missing key): ' . $e->getMessage() . "\n");
exit(1);
} catch (Api2ConvertException $e) {
fwrite(STDERR, 'Request failed: ' . $e->getMessage() . "\n");
exit(1);
}