gpt4 book ai didi

php - 无法使用 try/catch 处理异常

转载 作者:行者123 更新时间:2023-12-02 04:04:21 24 4
gpt4 key购买 nike

我正在使用Goutte (使用 Guzzle )提取内容,尽管我在 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...)

这就是我所拥有的:

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;
}

最佳答案

尝试:

  } catch (\Exception $e) {

而不是:

  } catch (Exception $e) {

编辑:

如果您使用的是 PHP-7,您可以 try catch Throwable始终带有斜线,如下所示:

  } catch (\Throwable $e) {

希望这有帮助

关于php - 无法使用 try/catch 处理异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40194972/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com