0

I'm running a PHP Wordpress site that's part of an ecosystem of sites.

The Wordpress site relies on another service to provide the site's navigation menu. I'm trying to make the Wordpress site more resilient whenever the other service goes down or is not available. Wordpress is using Guzzle to make a network request from the other service, but I cannot seem to configure it to not throw an exception whenever it cannot reach the other service.

I've tried setting the http_errors, timeout, and connect_timeout on Guzzle, but I still get an exception whenever the other service is down.

Here's an example of the request that I'm making:

$client = new GuzzleHttp\Client(['base_uri' => 'http://localhost']);
$response = $client->get('/menu', [
  'http_errors' => false,
  'connect_timeout' => 5,
  'timeout' => 5,
]);

Which raises a Fatal Error:

Fatal error: Uncaught Exception: cURL error 7: Failed to connect to localhost port 80 after 0 ms: Connection refused (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)

I have also tried:

$client = new GuzzleHttp\Client([
  'base_uri' => 'http://localhost',
  'http_errors' => false,
  'connect_timeout' => 5,
  'timeout' => 5,
]);
$response = $client->get('/menu');

Can I suppress the connection refused error?

Aaron Breckenridge
  • 1,723
  • 18
  • 25
  • Have a local cache for the menu.. if it doesn't update, use the cache. Regarding the exception: you should be able to catch it.. – Honk der Hase May 10 '23 at 18:06
  • Yes, @HonkderHase it's already cached. I just need to catch the exception for when it's not cached. – Aaron Breckenridge May 10 '23 at 18:09
  • that's what try and catch is for !! Does this answer your question? [Catching exceptions from Guzzle](https://stackoverflow.com/questions/17658283/catching-exceptions-from-guzzle) – OMi Shah May 10 '23 at 18:42

0 Answers0