gpt4 book ai didi

php - 为什么我收到 fatal error : Uncaught exception 'GuzzleHttp\Exception\ClientException' with message 'Client error: 404' ?

转载 作者:行者123 更新时间:2023-12-05 01:46:45 27 4
gpt4 key购买 nike

我 try catch 异常,但我仍然在 C:\OS\OpenServer\domains\kinopoisk\parser\php\vendor 中收到“ fatal error :未捕获异常‘GuzzleHttp\Exception\ClientException’和消息‘客户端错误:404’”\guzzlehttp\guzzle\src\Middleware.php:69"

 <?php

ini_set('display_errors', 'on');
error_reporting(E_ALL);
set_time_limit(0);

require "vendor/autoload.php";

use GuzzleHttp\Client;
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Exception\ClientException;

$filmsUrl = [297, 298];

$urlIterator = new ArrayObject($filmsUrl);

$client = new Client([
'base_uri' => 'http://example.com',
'cookies' => true,
]);

foreach ($urlIterator->getIterator() as $key => $value) {
try {
$promise = $client->requestAsync('GET', 'post/' . $value, [
'proxy' => [
'http' => 'tcp://216.190.97.3:3128'
]
]);

$promise->then(
function (ResponseInterface $res) {
echo $res->getStatusCode() . "\n";
},
function (RequestException $e) {
echo $e->getMessage() . "\n";
echo $e->getRequest()->getMethod();
}
);
} catch (ClientException $e) {
echo $e->getMessage() . "\n";
echo $e->getRequest()->getMethod();
}
}
$promise->wait();

我的代码有什么问题吗?

最佳答案

我不确定,但您只在此处捕获 ClientException。也 try catch RequestException。查看 Middleware.php:69 中的代码是使用的异常类,但是如果你想捕获所有异常,那么你需要去寻找最抽象的异常类,它应该是RuntimeExceptionGuzzleException

尝试这样的事情:

try {
// your code here
} catch (RuntimeException $e) {
// catches all kinds of RuntimeExceptions
if ($e instanceof ClientException) {
// catch your ClientExceptions
} else if ($e instanceof RequestException) {
// catch your RequestExceptions
}
}

或者你可以试试下面的方法

try {
// your code here
} catch (ClientException $e) {
// catches all ClientExceptions
} catch (RequestException $e) {
// catches all RequestExceptions
}

希望对您有所帮助。

关于php - 为什么我收到 fatal error : Uncaught exception 'GuzzleHttp\Exception\ClientException' with message 'Client error: 404' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33854176/

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