PHP Guzzle Basic Auth:抓住持票人令牌

时间:2018-02-22 06:01:53

标签: php http-headers basic-authentication guzzle bearer-token

背景

将postman中的API移到PHP上,并使用guzzle来完成它。我主要是自学成才,因此我对HTTP基本身份验证的理解有些漏洞。 API工作正常,我只是觉得我在这里有点多余。 也许有人可以帮忙澄清一下?

预期结果

  1. 我在标头中发送了user:pass的请求
    • 我收到了一个令牌
  2. 我在标头中发送了bearer {{token}}的请求
    • 我在API
    • 中进行了身份验证
  3. 实际结果

    1. 我在标头中发送了user:pass的请求
      • 我在API中进行了身份验证,没有令牌
    2. 问题

      那是什么给出的?我是想抓住令牌还是一点,但我没有?我知道邮递员为我生成了一个令牌,可以通过它的基本身份验证使用,但我似乎无法弄清楚如何在Guzzle或Curl中进行操作。

      示例代码

        

      请记住,这段代码有效,但我似乎无法取回持票人令牌......

      Guzzle示例

      public function Test() {
      
          $this->http_client = new Guzzle([
              'base_uri' => $this->request_url,
              'headers' => [
                  'Accept' => 'application/json',
                  'Content-Type' => 'application/json',
              ],
          ]);
      
          $result = $this->http_client->request('GET', 'customers',
              ['auth' => [$this->api_name, $this->api_key]
          ]);
      
          //$result->getHeader('Authorization'); <-- This doesn't work...
      
          return $result->getBody();
      }
      

      卷曲示例

      public function Test() {
      
          $username = $this->api_name;
          $password = $this->api_key;
          $url = $this->request_url . 'customers';
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_URL,$url);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
          curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
          curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
          $result = curl_exec($ch);
          curl_close($ch);
          return $result;
      }
      

0 个答案:

没有答案
相关问题