mercredi 29 juin 2016

Catch exception from guzzle

I'm using laravel and I have setup the abstract class method to get response from the various APIs I'm calling. But if the API url is unreachable then it throws an exception. I know I'm missing something. Any help would be great for me.

$offers = [];
    try {
      $appUrl = parse_url($this->apiUrl);

      // Call Api using Guzzle
      $client = new Client('' . $appUrl['scheme'] . '://' . $appUrl['host'] . '' . $appUrl['path']);

      if ($appUrl['scheme'] == 'https') //If https then disable ssl certificate
        $client->setDefaultOption('verify', false);

      $request = $client->get('?' . $appUrl['query']);
      $response = $request->send();
      if ($response->getStatusCode() == 200) {
        $offers = json_decode($response->getBody(), true);
      }
    } catch (ClientErrorResponseException $e) {
      Log::info("Client error :" . $e->getResponse()->getBody(true));
    } catch (ServerErrorResponseException $e) {
      Log::info("Server error" . $e->getResponse()->getBody(true));
    } catch (BadResponseException $e) {
      Log::info("BadResponse error" . $e->getResponse()->getBody(true));
    } catch (\Exception $e) {
      Log::info("Err" . $e->getMessage());
    }

    return $offers;



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire