Repository navigation
Expand file tree
/
Copy path07-custom-http-client.php
More file actions
65 lines (54 loc) · 1.93 KB
/
Copy path07-custom-http-client.php
File metadata and controls
65 lines (54 loc) · 1.93 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
/**
* 07-custom-http-client — bring your own PSR-18 client instead of Guzzle.
*
* Guzzle is only the default. Any PSR-18 client works: build a RequestHandler
* yourself and hand it to the ZammadClient constructor. Credentials, retry
* behaviour and logging are configured exactly as with ZammadClient::create().
*
* This recipe uses Symfony HttpClient; the same shape applies to Buzz, Artax,
* or anything else implementing Psr\Http\Client\ClientInterface.
*
* For the ordinary case, see 00-connect.php.
*
* See docs/custom-http-client.md for Laravel/Symfony container wiring and for
* the security notes on timeouts, TLS verification and redirects.
*
* Requirements:
* composer require symfony/http-client nyholm/psr7
*
* Run: php examples/cookbook/07-custom-http-client.php
*/
declare(strict_types=1);
require __DIR__ . '/../../vendor/autoload.php';
use Nyholm\Psr7\Factory\Psr17Factory;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\HttpClient\Psr18Client;
use ZammadAPIClient\Core\Auth;
use ZammadAPIClient\Core\Transport\RequestHandler;
use ZammadAPIClient\ZammadClient;
$url = getenv('ZAMMAD_URL') ?: 'http://localhost:3000';
$token = getenv('ZAMMAD_TOKEN') ?: '';
if ($token === '') {
fwrite(STDERR, "Set ZAMMAD_TOKEN\n");
exit(1);
}
// No Authorization header here: PSR-18 has no concept of default headers, and
// Symfony's Psr18Client does not offer one. The credential is passed to the
// RequestHandler below, which applies it to every request it composes.
$httpClient = new Psr18Client(
HttpClient::create([
'headers' => ['User-Agent' => 'Zammad API PHP'],
'timeout' => 30,
]),
);
$handler = new RequestHandler(
$httpClient,
new Psr17Factory(),
$url,
maxRetries: 3,
auth: Auth::token($token),
);
$client = new ZammadClient($handler);
echo "Connected to {$url} (Symfony HttpClient)\n";
echo "Ticket #1: " . $client->ticket()->find(1)->title . "\n";