HTTP client lifetime (ARCH7083)
Ce contenu n’est pas encore disponible dans votre langue.
Key php.performance.http-client-lifetime · Severity error · Category performance
Creating a new HTTP client per request opens fresh connection pools and leads to socket exhaustion. Build the client once and reuse it.
Incorrect
Section titled “Incorrect”<?phpclass ApiService{ public function fetch(string $path): string { $client = new \GuzzleHttp\Client(); return (string) $client->get($path)->getBody(); }}Correct
Section titled “Correct”<?phpclass ApiService{ private \GuzzleHttp\Client $client; public function __construct(\GuzzleHttp\Client $client) { $this->client = $client; } public function fetch(string $path): string { return (string) $this->client->get($path)->getBody(); }}Construct the HTTP client once (ideally injected via DI) and reuse it as a shared field.