我是PHP的绝对新手(我来自Java),并且遇到以下与如何处理异常有关的问题
我正在使用Guzzle执行对REST Web服务的调用,如下所示:
$client = new Client(); //GuzzleHttp\Client
$response = $client->get('http://localhost:8080/Extranet/login',
[
'auth' => [
$credentials['email'],
$credentials['password']
]
]);
$dettagliLogin = json_decode($response->getBody());
Run Code Online (Sandbox Code Playgroud)
如果在响应中我的Web服务浏览了现有的用户信息,那么我没有问题。
如果该用户不存在,那么我的Web服务将返回以下内容:
[2017-01-30 11:24:44] local.INFO: INSERTED USER CREDENTIAL: pippo@google.com dddd
[2017-01-30 11:24:44] local.ERROR: exception 'GuzzleHttp\Exception\ClientException' with message 'Client error: `GET http://localhost:8080/Extranet/login` resulted in a `401 Unauthorized` response:
{"timestamp":1485775484609,"status":401,"error":"Unauthorized","message":"Bad credentials","path":"/Extranet/login"}
Run Code Online (Sandbox Code Playgroud)
因此在我看来,在这种情况下,客户端抛出了ClientException。
我的疑问是:是否可以将$ client-> get(...)放入类似Java try catch的块中,所以如果捕获到ClientException,我可以处理它以创建自定义响应?
特纳克斯
发布表单时出现此异常
“类GuzzleHttp \ Psr7 \ Request中无法解析的依赖项解析[Parameter#0 [$ method]]”
搜索并尝试了多种方法来解决此问题,但没有任何进展。似乎我的问题与此问题相同
任何积极的回应将不胜感激。谢谢。
我的项目突然出现这个错误:
Argument 1 passed to Vonage\Client::setHttpClient() must be an instance of Psr\Http\Client\ClientInterface, instance of GuzzleHttp\Client given, called in /home/vagrant/Code/xxx/vendor/nexmo/client-core/src/Client.php
Run Code Online (Sandbox Code Playgroud)
这是包中的一小部分代码:
if (is_null($client)) {
// Since the user did not pass a client, try and make a client
// using the Guzzle 6 adapter or Guzzle 7 (depending on availability)
/** @noinspection ClassConstantCanBeUsedInspection */
if (class_exists('\GuzzleHttp\Client')) {
$client = new \GuzzleHttp\Client();
} elseif (class_exists('\Http\Adapter\Guzzle6\Client')) {
/** @noinspection CallableParameterUseCaseInTypeContextInspection */
/** @noinspection PhpUndefinedNamespaceInspection */
/** @noinspection PhpUndefinedClassInspection */
$client = new \Http\Adapter\Guzzle6\Client(); …Run Code Online (Sandbox Code Playgroud) 从 API 文档我有这个curl请求:
curl https://api.example.com/api/Jwt/Token ^
-d Username="asd%40gmail.com" ^
-d Password="abcd1234"
Run Code Online (Sandbox Code Playgroud)
现在我正在尝试使用 Guzzle 库在 Laravel 5.1 中创建该请求,所以我写道:
public function test()
{
$client = new GuzzleHttp\Client();
$res = $client->createRequest('POST','https://api.example.com/api/Jwt/Token', [
'form_params' => [
'Username' => 'asd%40gmail.com',
'Password' => 'abcd1234'
]
]);
$res = json_decode($res->getBody()->getContents(), true);
dd ($res);
}
Run Code Online (Sandbox Code Playgroud)
但我收到此错误:
***ErrorException in Client.php line 126:
Argument 3 passed to GuzzleHttp\Client::request() must be of the type array, string given, called in /home/ibook/public_html/vendor/guzzlehttp/guzzle/src/Client.php on line 87 and defined***
Run Code Online (Sandbox Code Playgroud)
问题是什么,我该如何解决该错误?
ps我也试过
$res = …Run Code Online (Sandbox Code Playgroud) guzzlehttp ×4
laravel ×4
php ×4
exception ×2
guzzle ×2
composer-php ×1
guzzle6 ×1
laravel-5 ×1