2

我以前成功地使用了guzzlehttp/guzzle v.6.*带有身份验证参数的包,如下所示:

        $client = new GuzzleClient([
            'base_uri'  => $base_uri ,
            'auth'      => [ $username, $password ]
        ]);

这很好用。但是,我现在正在尝试使用该"guzzlehttp/guzzle-services": "0.5.*"包来更轻松地使用 API 端点。

使用 guzzle-services 的 Github页面中的以下示例:

use GuzzleHttp\Client;
use GuzzleHttp\Command\Guzzle\GuzzleClient;
use GuzzleHttp\Command\Guzzle\Description;

$client = new Client();
$description = new Description([
    'baseUrl' => 'http://httpbin.org/',
    'operations' => [
        'testing' => [
            'httpMethod' => 'GET',
            'uri' => '/get/{foo}',
            'responseModel' => 'getResponse',
            'parameters' => [
                'foo' => [
                    'type' => 'string',
                    'location' => 'uri'
                ],
                'bar' => [
                    'type' => 'string',
                    'location' => 'query'
                ]
            ]
        ]
    ],
    'models' => [
        'getResponse' => [
            'type' => 'object',
            'additionalProperties' => [
                'location' => 'json'
            ]
        ]
    ]
]);

$guzzleClient = new GuzzleClient($client, $description);
$result = $guzzleClient->testing(['foo' => 'bar']);

使用包时,我如何以及在哪里添加身份验证参数"guzzlehttp/guzzle-services": "0.5.*"

我已经尝试了所有可能的方法,但无法让它发挥作用。

4

2 回答 2

1

我已经成功地通过以下代码使用 Guzzle 6.2.2 和 Guzzle Services 1.0.0 和 Basic Auth:

$config['auth'] = array('user', 'pass');
$client = new Client($config);

当然,您可能需要其他设置,但对于基本身份验证,仅需要此设置。检查GuzzleHttp\Client::applyOptions类方法以查看 Guzzle 何时使用此设置。

它与@revo 答案非常相似,但没有主要的“默认”数组。

这些是我的 guzzle 安装包:

"
gimler/guzzle-description-loader     v0.0.2  Load guzzle service description from various file formats
guzzlehttp/command                   1.0.0   Provides the foundation for building command-based web service clients
guzzlehttp/guzzle                    6.2.2   Guzzle is a PHP HTTP client library
guzzlehttp/guzzle-services           1.0.0   Provides an implementation of the Guzzle Command library that uses Guzzle service descriptions to describe web services, se...
guzzlehttp/promises                  1.3.0   Guzzle promises library
guzzlehttp/psr7                      1.3.1   PSR-7 message implementation
"
于 2016-12-19T19:43:02.013 回答
0

我怀疑Description类是否提供了一种将身份验证信息合并到请求的方法。但是您可以Client在 Guzzle v5.x 中实例化新的时添加它们,如下所示:

$client = new Client(['defaults' => ['auth' => ['user', 'pass']]]);
于 2016-07-14T08:40:27.350 回答