eozzy
eozzy

Reputation: 68650

Can't handle exception with try/catch

I'm using Goutte (which uses Guzzle) to extract content and my script ends with an error although I'm running in try/catch:

Error: Client error: `GET http://example.com/C42C9CA3` resulted in a `403 Forbidden` response:
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
         "htt (truncated...)

This is what I have:

use Goutte\Client;
$HTTPconfig = [ "curl" => [
                  CURLOPT_TIMEOUT => 60,
                  CURLOPT_CONNECTTIMEOUT => 60,
                  CURLOPT_SSL_VERIFYPEER => false,
                ],
                ['http_errors' => false]
              ];
$HTTPclient = new \Goutte\Client;
$HTTPclient->setClient(new \GuzzleHttp\Client($HTTPconfig));
$HTTPclient->setHeader('user-agent', 'Mozilla/5.0 (Windows NT 6.2; rv:20.0) Gecko/20121202 Firefox/20.0');

try {
  $crawler = $HTTPclient->request('GET', $url);
  $doc = $crawler->html();
} catch (Exception $e) {
  write($e->getMessage());
  continue;
}

Upvotes: 2

Views: 1908

Answers (2)

Matteo
Matteo

Reputation: 39380

Try with:

  } catch (\Exception $e) {

instead of:

  } catch (Exception $e) {

EDIT:

If you are using PHP-7 you can try to catch Throwable always with a slash as follow:

  } catch (\Throwable $e) {

Hope this help

Upvotes: 5

Alex Blex
Alex Blex

Reputation: 37038

Remove ['http_errors' => false] option. It is true by default, and results with exception for 4xx/5xx response codes.

Upvotes: 0

Related Questions