-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom-client.php
More file actions
143 lines (117 loc) · 4.16 KB
/
custom-client.php
File metadata and controls
143 lines (117 loc) · 4.16 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
/**
* Custom HTTP Client Example
*
* This example shows how to use different HTTP client implementations
* (Guzzle, Symfony) with Transport PHP.
*/
require __DIR__.'/../vendor/autoload.php';
use Farzai\Transport\Factory\ClientFactory;
use Farzai\Transport\TransportBuilder;
echo "=== Custom HTTP Client Examples ===\n\n";
// Example 1: Auto-detection
echo "1. Auto-detecting HTTP Client\n";
echo str_repeat('-', 50)."\n";
try {
$clientName = ClientFactory::getDetectedClientName();
echo "Auto-detected client: {$clientName}\n\n";
$transport = TransportBuilder::make()
->withBaseUri('https://jsonplaceholder.typicode.com')
->build();
$response = $transport->get('/posts/1')->send();
echo "Successfully fetched post using {$clientName}\n";
echo "Title: {$response->json('title')}\n\n";
} catch (\Exception $e) {
echo "Error: {$e->getMessage()}\n\n";
}
// Example 2: Explicitly using Guzzle (if available)
echo "2. Using Guzzle HTTP Client\n";
echo str_repeat('-', 50)."\n";
if (ClientFactory::isAvailable('guzzle')) {
try {
$guzzleClient = ClientFactory::createGuzzle([
'timeout' => 30,
'verify' => true,
]);
$transport = TransportBuilder::make()
->setClient($guzzleClient)
->withBaseUri('https://jsonplaceholder.typicode.com')
->build();
$response = $transport->get('/posts/1')->send();
echo "Successfully fetched post using Guzzle\n";
echo "Title: {$response->json('title')}\n\n";
} catch (\Exception $e) {
echo "Error: {$e->getMessage()}\n\n";
}
} else {
echo "Guzzle is not installed\n";
echo "Install it with: composer require guzzlehttp/guzzle\n\n";
}
// Example 3: Explicitly using Symfony HTTP Client (if available)
echo "3. Using Symfony HTTP Client\n";
echo str_repeat('-', 50)."\n";
if (ClientFactory::isAvailable('symfony')) {
try {
$symfonyClient = ClientFactory::createSymfony([
'timeout' => 30,
'max_redirects' => 5,
]);
$transport = TransportBuilder::make()
->setClient($symfonyClient)
->withBaseUri('https://jsonplaceholder.typicode.com')
->build();
$response = $transport->get('/posts/1')->send();
echo "Successfully fetched post using Symfony HTTP Client\n";
echo "Title: {$response->json('title')}\n\n";
} catch (\Exception $e) {
echo "Error: {$e->getMessage()}\n\n";
}
} else {
echo "Symfony HTTP Client is not installed\n";
echo "Install it with: composer require symfony/http-client\n\n";
}
// Example 4: Checking Available Clients
echo "4. Checking Available HTTP Clients\n";
echo str_repeat('-', 50)."\n";
$clients = [
'guzzle' => 'GuzzleHTTP\\Client',
'symfony' => 'Symfony\\Component\\HttpClient\\Psr18Client',
];
echo "Available HTTP clients:\n";
foreach ($clients as $name => $class) {
$available = ClientFactory::isAvailable($name);
$status = $available ? '✓ Available' : '✗ Not installed';
echo " {$name}: {$status}\n";
if (! $available) {
echo ' Install with: composer require ';
echo $name === 'guzzle' ? 'guzzlehttp/guzzle' : 'symfony/http-client';
echo "\n";
}
}
echo "\n";
// Example 5: Using with Logger
echo "5. HTTP Client with Logging\n";
echo str_repeat('-', 50)."\n";
try {
// Create a simple logger
$logger = new class extends \Psr\Log\AbstractLogger
{
public function log($level, $message, array $context = []): void
{
$contextStr = ! empty($context) ? ' '.json_encode($context) : '';
echo "[{$level}] {$message}{$contextStr}\n";
}
};
// Create client with logger (logs which client was detected)
$client = ClientFactory::create($logger);
$transport = TransportBuilder::make()
->setClient($client)
->setLogger($logger)
->withBaseUri('https://jsonplaceholder.typicode.com')
->build();
$response = $transport->get('/posts/1')->send();
echo "\nTitle: {$response->json('title')}\n\n";
} catch (\Exception $e) {
echo "Error: {$e->getMessage()}\n\n";
}
echo "=== Examples Complete ===\n";