2016-07-13 25 views
2

我以前成功地使用了guzzlehttp/guzzle v.6.*包認證參數,像這樣:狂飲 - 命令與服務:基本HTTP認證

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

這個偉大的工程。但是,我現在試圖使用"guzzlehttp/guzzle-services": "0.5.*"軟件包來簡化API端點的工作。

使用從GitHub page狂飲服務的下面的例子:

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.*"包當我添加AUTH參數?

我嘗試了一切可能的方式,但無法讓它工作。

回答

0

我懷疑Description類是否提供了一種將認證信息合併到請求的方法。但是,你可以將它們添加在實例中狂飲V5.x中新Client這樣的:

$client = new Client(['defaults' => ['auth' => ['user', 'pass']]]); 
+0

我已經嘗試過了,但它並沒有,至少在'guzzlehttp /狂飲.6工作*'包。 – CandyCoated

1

我已經成功地管理這個代碼與基本身份驗證使用狂飲6.2.2和狂飲服務1.0.0:

$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 
"