我之前已经成功地使用了带有身份验证参数的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.*"
包时,我如何以及在世界何处添加身份验证参数?
我已经尝试了所有可能的方法,但无法让它工作。
我已经成功地使用以下代码将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答案非常相似,但没有主要的"默认值"数组。
这些是我安装的软件包:
"
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
"
我怀疑Description
类是否提供了一种将身份验证信息合并到请求的方法。但是您可以在 Guzzle v5.x 中实例化新Client
时添加它们,如下所示:
$client = new Client(['defaults' => ['auth' => ['user', 'pass']]]);