skip to Main Content
$endpoint = rtrim($site->url,"/").'/'.env('WP_ENDPOINT');

try {     
  $request = Http::post($endpoint);
  dd($request);            
} catch (GuzzleHttpExceptionConnectException $e) {
  $response = json_encode((string)$e->getResponse()->getBody());
}

I am trying to get response from a remote URL using guzzle http client but i get error ‘cURL error 6: Could not resolve host:’ on line 4.

$request = Http::post($endpoint);

I used try catch to handle the error but nothing happened so far.

Any help would be highly appreciated.

2

Answers


  1. actually i didn’t work anytime with laravel guzzle but i read something about guzzle’s requests and you should always catch these two types of common timeouts, connection timeouts and request timeouts can you try like this your post request:

        $retry_count = 0;
        do{
          try{
             $response = $client->post($endpoint);
          }
          catch (GuzzleHttpExceptionConnectException $e) {
            // log the error here
    
            Log::Warning('guzzle_connect_exception', [
                'url' => $this->request->fullUrl(),
                'message' => $e->getMessage()
            ]);
          } 
          catch (GuzzleHttpExceptionRequestException $e) {
            Log::Warning('guzzle_connection_timeout', [
                'url' => $this->request->fullUrl(),
                'message' => $e->getMessage()
            ]);
          }
          if(++retry_count == 5){
            break;
          }
        }while(!is_array($response));
    

    if you find any issue on this code, please notify me!

    Login or Signup to reply.
  2. Do not use env() outside the config file.

    Laravel’s Http client does not throw exceptions unlike Guzzle. Be sure to read the official documentation.
    https://laravel.com/docs/11.x/http-client#error-handling

    // try-catch style
    
    use IlluminateSupportFacadesHttp;
    use IlluminateHttpClientRequestException;
    
    try {     
      $response = Http::post($endpoint)->throw();
      dd($response);            
    } catch (RequestException $e) {
      //
    }
    
    // Laravel style
    
    use IlluminateSupportFacadesHttp;
        
    $response = Http::post($endpoint);
    
    if ($response->successful()) {
      //
    } else {
      //
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search