标签: guzzlehttp

如何正确处理Guzzle ClientException?我可以将呼叫放入PHP中的Java try catch块中吗?

我是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,我可以处理它以创建自定义响应?

特纳克斯

php exception laravel guzzle guzzlehttp

1
推荐指数
1
解决办法
2283
查看次数

Laravel无法解析的依赖项,在类GuzzleHttp \ Psr7 \ Request中解析[Parameter#0 [<required> $ method]]

发布表单时出现此异常

“类GuzzleHttp \ Psr7 \ Request中无法解析的依赖项解析[Parameter#0 [$ method]]”

搜索并尝试了多种方法来解决此问题,但没有任何进展。似乎我的问题与此问题相同

任何积极的回应将不胜感激。谢谢。

php exception laravel laravel-5 guzzlehttp

1
推荐指数
3
解决办法
2970
查看次数

GuzzleHttp 在 Laravel 上突然给我一个错误

我的项目突然出现这个错误:

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)

php laravel composer-php guzzlehttp

1
推荐指数
1
解决办法
625
查看次数

Laravel Guzzle 参数

从 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)

php laravel guzzle guzzle6 guzzlehttp

0
推荐指数
1
解决办法
1739
查看次数