如何使用Guzzle进行HTTP基本身份验证?

Gop*_*hra 54 php http basic-authentication guzzle

我想使用Guzzle进行基本的访问认证,我对编程很新.我不知道该怎么做.我试图用curl做这个,但我的环境需要使用guzzle.

ame*_*iel 86

如果您使用的是Guzzle 5.0或更高版本,则文档会说使用auth参数指定基本身份验证:

$client = new GuzzleHttp\Client();
$response = $client->get('http://www.server.com/endpoint', [
    'auth' => [
        'username', 
        'password'
    ]
]);
Run Code Online (Sandbox Code Playgroud)

请注意,如果您使用的是Guzzle 3.0或更早版本,则语法会有所不同.构造函数是不同的,您还需要在请求上显式使用该方法来获取响应:send

$client = new Guzzle\Http\Client();
$request = $client->get('http://www.server.com/endpoint');
$request->setAuth('username', 'password');
$response = $request->send();
Run Code Online (Sandbox Code Playgroud)


mnv*_*mnv 22

除了@amenadiel的回答.有时在构造函数中指定auth参数:

$client = new Client([
    'auth' => ['username', 'password'],
]); 
Run Code Online (Sandbox Code Playgroud)

然后每个请求都将使用此默认的auth参数.

  • 对于旧的````guzzlehttp/guzzle":"5.2.0"```你应该传递```['defaults'=> ['auth'=> ['username','password']]]` `` (3认同)
  • Guzzle 版本缺失。 (2认同)

bou*_*247 15

当我使用Guzzlev6并使用@amenadiel的建议时,这个工作正常.当你使用curl时,你的语法看起来像

curl -u someone@gmail.com:密码 http://service.com

在幕后它实际上需要"someone@gmail.com:密码"位,base64对其进行编码并使用带有编码值的"授权"标头发送请求.对于这个例子,那将是:

授权:基本c29tZW9uZUBnbWFpbC5jb206cGFzc3dvcmQ =

来自@amenadiel的建议附加了"auth:username,password"标题,因此我的身份验证仍然失败.要成功实现这一点,只需在实例化Guzzle客户端请求时制作标题,即

$client = new GuzzleHttp\Client();
$credentials = base64_encode('someone@gmail.com:password');
$response = $client->get('http://www.server.com/endpoint', [
    'Authorization' => ['Basic '.$credentials]
]);
Run Code Online (Sandbox Code Playgroud)

这会将标题附加为卷曲,并且您尝试连接的任何服务都将停止对您大喊大叫,

干杯.


Eri*_*uby 13

根据Guzzle 6文档,您可以使用基本授权来执行请求,如下所示:

$client = new Client();

$response = $client->request(
    'POST', /*instead of POST, you can use GET, PUT, DELETE, etc*/
    $url,
    [
      'auth' => ['username', 'password'] /*if you don't need to use a password, just leave it null*/
    ] 
);

echo $response->getBody();
Run Code Online (Sandbox Code Playgroud)

注意:您根本不需要使用base64_encode(),因为它已经在请求之前执行了.

我已经测试了它的工作原理:)

有关详细信息,请参阅:Guzzle 6文档


小智 12

$response = $client->request( 'GET', 'your_url', [
                    'auth'    => [
                        'your_username',
                        'your_password'
                    ],
                    'headers' => [
                        'if you want to pass something in the headers'
                    ]
                ]
            );
Run Code Online (Sandbox Code Playgroud)


kjo*_*nes 7

您还可以在实例化客户端时配置 auth 参数,而不是将其添加到每个请求中:

$this->client = new \GuzzleHttp\Client([                                                                                                                                             
    'base_uri' => $this->endpoint,                                                                                                                                                   
    'headers' => [                                                                                                                                                                   
        'Authorization' => ['Basic'.base64_encode($this->username.':'.$this->password)],                                                                                                 
    ],                                                                                                                                                                               
]);
Run Code Online (Sandbox Code Playgroud)

以下是 Guzzle 6 的各种文档链接:


Agu*_*ndo 6

根据@ bourgeois247关于base64编码的说法,以下代码在Guzzle 6上非常适合我:

$client = new Client();
$credentials = base64_encode('username:password');
$response = $client->post('url',
        [
            'headers' => [
                'Authorization' => 'Basic ' . $credentials,
            ],
        ]);
Run Code Online (Sandbox Code Playgroud)


HRo*_*oux 5

如果您将它与 symfony 一起使用,您还可以在配置文件中定义它(symfony4 为 config/packages/eight_points_guzzle.yaml,其他版本为 flex 或 config.yml)

在您的配置文件中:

eight_points_guzzle:
    clients:         
        your_service:
            # Write here the host where to do requests
            base_url: "yourURL"

            options:
                timeout: 30
                auth:
                    - yourLogin     # login
                    - yourPassword # password
            plugin: ~
Run Code Online (Sandbox Code Playgroud)

然后,在你的服务、控制器等中......

$client  = $this->getContainer()->get('eight_points_guzzle.client.your_service');
$response = $client->get('yourRoute');
Run Code Online (Sandbox Code Playgroud)

请参阅: https: //packagist.org/packages/eightpoints/guzzle-bundle