php - Catch exception from guzzle -
i'm using laravel , have setup abstract class method response various apis i'm calling. if api url unreachable throws exception. know i'm missing something. great 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 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;
you should set guzzehttp client option 'http_errors' => false,
example code should this, document:guzzlehttp client http-error option explain
set false disable throwing exceptions on http protocol errors (i.e., 4xx , 5xx responses). exceptions thrown default when http protocol errors encountered.
$client->request('get', '/status/500'); // throws guzzlehttp\exception\serverexception $res = $client->request('get', '/status/500', ['http_errors' => false]); echo $res->getstatuscode(); // 500 $this->client = new client([ 'cookies' => true, 'headers' => $header_params, 'base_uri' => $this->base_url, 'http_errors' => false ]); $response = $this->client->request('get', '/'); if ($code = $response->getstatuscode() == 200) { try { // danger dom things in here }catch (/exception $e){ //catch } }
Comments
Post a Comment