Skip to content

Commit d35a971

Browse files
Copilotsamdark
andcommitted
Address review feedback: fix composer commands, reorder PSR standards, use example.com, improve DI configuration
Co-authored-by: samdark <[email protected]>
1 parent 7c6162a commit d35a971

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

cookbook/en/making-http-requests.md

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,24 @@
11
# Making HTTP requests
22

3-
When building modern web applications, you often need to make HTTP requests to external APIs, microservices, or third-party services. This article demonstrates how to make HTTP requests in Yii3 applications using Guzzle with PSR (PHP Standards Recommendations) interfaces.
3+
When building modern applications, you often need to make HTTP requests to external APIs. This article demonstrates how to make HTTP requests in Yii3 applications using Guzzle with and [PSR interfaces](https://www.php-fig.org/psr/).
44

55
## What are PSR interfaces for HTTP
66

77
The PHP-FIG (PHP Framework Interoperability Group) has defined several PSR standards for HTTP handling:
88

99
- **PSR-7**: HTTP message interfaces for requests and responses
10-
- **PSR-18**: HTTP client interface for sending PSR-7 requests and returning PSR-7 responses
1110
- **PSR-17**: HTTP factory interfaces for creating PSR-7 message objects
11+
- **PSR-18**: HTTP client interface for sending PSR-7 requests and returning PSR-7 responses
1212

1313
Using these interfaces ensures your code is framework-agnostic and follows established PHP standards.
1414

1515
## Installation
1616

17-
### Install Guzzle HTTP client
18-
19-
Install the Guzzle HTTP client with PSR-18 support:
20-
21-
```shell
22-
composer require guzzlehttp/guzzle --prefer-dist
23-
```
24-
25-
### Install PSR factories (optional)
26-
27-
For creating PSR-7 requests and responses manually, you can install PSR-17 factories:
17+
Install the Guzzle HTTP client with PSR-18 support and PSR-17 factories:
2818

2919
```shell
30-
composer require guzzlehttp/psr7 --prefer-dist
20+
composer require guzzlehttp/guzzle
21+
composer require guzzlehttp/psr7
3122
```
3223

3324
## Basic usage
@@ -41,7 +32,6 @@ Here's how to make a basic GET request using PSR-18 interfaces:
4132

4233
declare(strict_types=1);
4334

44-
use GuzzleHttp\Client;
4535
use Psr\Http\Client\ClientInterface;
4636
use Psr\Http\Message\RequestFactoryInterface;
4737
use Psr\Http\Message\ResponseInterface;
@@ -58,7 +48,7 @@ class ApiService
5848
{
5949
$request = $this->requestFactory->createRequest(
6050
'GET',
61-
"https://jsonplaceholder.typicode.com/users/{$userId}"
51+
"https://example.com/users/{$userId}"
6252
);
6353

6454
return $this->httpClient->sendRequest($request);
@@ -94,7 +84,7 @@ class UserService
9484
$jsonData = json_encode($userData, JSON_THROW_ON_ERROR);
9585
$stream = $this->streamFactory->createStream($jsonData);
9686

97-
$request = $this->requestFactory->createRequest('POST', 'https://jsonplaceholder.typicode.com/users')
87+
$request = $this->requestFactory->createRequest('POST', 'https://example.com/users')
9888
->withHeader('Content-Type', 'application/json')
9989
->withHeader('Accept', 'application/json')
10090
->withBody($stream);
@@ -118,7 +108,6 @@ declare(strict_types=1);
118108
// config/common/http-client.php
119109

120110
use GuzzleHttp\Client;
121-
use GuzzleHttp\Psr7\HttpFactory;
122111
use Psr\Http\Client\ClientInterface;
123112
use Psr\Http\Message\RequestFactoryInterface;
124113
use Psr\Http\Message\ResponseFactoryInterface;
@@ -136,10 +125,19 @@ return [
136125
],
137126
],
138127

139-
RequestFactoryInterface::class => HttpFactory::class,
140-
ResponseFactoryInterface::class => HttpFactory::class,
141-
StreamFactoryInterface::class => HttpFactory::class,
142-
UriFactoryInterface::class => HttpFactory::class,
128+
// Configure PSR-17 factories - these will depend on your chosen PSR-7 implementation
129+
RequestFactoryInterface::class => static function (): RequestFactoryInterface {
130+
return new \GuzzleHttp\Psr7\HttpFactory();
131+
},
132+
ResponseFactoryInterface::class => static function (): ResponseFactoryInterface {
133+
return new \GuzzleHttp\Psr7\HttpFactory();
134+
},
135+
StreamFactoryInterface::class => static function (): StreamFactoryInterface {
136+
return new \GuzzleHttp\Psr7\HttpFactory();
137+
},
138+
UriFactoryInterface::class => static function (): UriFactoryInterface {
139+
return new \GuzzleHttp\Psr7\HttpFactory();
140+
},
143141
];
144142
```
145143

@@ -259,6 +257,8 @@ class HttpClientFactory
259257

260258
For better performance when making multiple requests, you can use asynchronous requests:
261259

260+
> **Note**: Async functionality is not part of PSR interfaces, so this code depends on Guzzle explicitly.
261+
262262
```php
263263
<?php
264264

@@ -285,7 +285,7 @@ class BatchApiService
285285

286286
foreach ($userIds as $userId) {
287287
$promises[$userId] = $this->httpClient->getAsync(
288-
"https://jsonplaceholder.typicode.com/users/{$userId}"
288+
"https://example.com/users/{$userId}"
289289
);
290290
}
291291

0 commit comments

Comments
 (0)