【问题标题】:How do I do HTTP basic authentication using Guzzle?如何使用 Guzzle 进行 HTTP 基本身份验证?
【发布时间】:2023-08-08 04:35:02
【问题描述】:

我想使用 Guzzle 进行基本的访问身份验证,而且我对编程非常陌生。我不知道该怎么做。我尝试使用 curl 执行此操作,但我的环境需要使用 guzzle。

【问题讨论】:

  • 您真的应该尝试粘贴您尝试过的任何代码,以增加获得有意义答案的机会。仅仅陈述你的需要和无知只会让你被否决:(
  • Guzzle 版本丢失。

标签: php http basic-authentication guzzle


【解决方案1】:

如果您使用的是 Guzzle 5.0 或更新版本the docs 表示基本身份验证是使用 auth 参数指定的:

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

如果您使用的是Guzzle 3.0 或更早版本,请注意syntax is different。构造函数不同,还需要在请求上显式使用send方法才能得到响应:

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

【讨论】:

    【解决方案2】:

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

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

    那么每个请求都会使用这个默认的认证参数。

    【讨论】:

    • 对于旧的"guzzlehttp/guzzle": "5.2.0",你应该通过['defaults' => [ 'auth' => ['username', 'password'] ]]
    • Guzzle 版本丢失。
    【解决方案3】:

    当我使用 Guzzlev6 并使用来自@amenadiel 的建议时,这很有效。当你使用 curl 时,你的语法看起来像

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

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

    授权:基本 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]
    ]);
    

    这将像 curl 一样附加标头,并且您尝试连接的任何服务都将停止对您大喊大叫,

    干杯。

    【讨论】:

      【解决方案4】:

      根据 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();
      

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

      我已经测试过了,它可以工作:)

      查看更多信息:Guzzle 6 Documentation

      【讨论】:

      • Guzzle 版本丢失。
      • 它将如何比较存储在数据库中的用户名和密码?我对使用 GET 方法实现的要求相同。感谢任何人的帮助。
      • @bhattraideb "它将如何比较存储在 DB 中的用户名和密码?" > 请为此提出一个新问题。 “我对使用 GET 方法实现的要求相同。” > 这是我的答案...只需更改 POST go GET。
      • @EricGruby,我已经提出了一个问题。你能检查一下吗:*.com/questions/50591063/…
      【解决方案5】:
      $response = $client->request( 'GET', 'your_url', [
                          'auth'    => [
                              'your_username',
                              'your_password'
                          ],
                          'headers' => [
                              'if you want to pass something in the headers'
                          ]
                      ]
                  );
      

      【讨论】:

        【解决方案6】:

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

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

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

        【讨论】:

          【解决方案7】:

          根据 @bourgeois247 关于 base64 编码的说法,以下内容在 Guzzle 6 上对我来说非常有效:

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

          【讨论】:

          • 这只是@bourgeois247 答案的副本,应该只是对该答案的评论
          【解决方案8】:

          如果你将它与 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: ~
          

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

          $client  = $this->getContainer()->get('eight_points_guzzle.client.your_service');
          $response = $client->get('yourRoute');
          

          见:https://packagist.org/packages/eightpoints/guzzle-bundle

          【讨论】:

            最近更新 更多