簡體   English   中英

如何讓 Guzzle 與 CURLOPT_USERPWD 一起工作?

[英]How can I get Guzzle to work with CURLOPT_USERPWD?

我有以下 PHP curl 代碼來在應用程序中生成安全令牌。 當我運行此代碼時,它完美無缺。

    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL, 'https://host.com/api/v1/auth/keys');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "{}");
    curl_setopt($ch, CURLOPT_USERPWD, 'admin' . ':' . 'password');
    
    $headers = array();
    $headers[] = 'Content-Type: application/json';
    $headers[] = 'Accept: application/json';
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    
    $result = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    }
    curl_close($ch);

但目前我需要這個 Curl 腳本的 Guzzle 版本,這就是這個問題的開始。

我找到了在 Guzzle 中處理身份驗證的不同方法,但到目前為止沒有任何效果。

這就是我想出的。

       use GuzzleHttp\Client;
        
        include "../vendor/autoload.php";
        
        try{
        
          $client = new Client(['base_uri' => 'https://host.com/api/v1/']);
          
          $client->request('POST', 'auth/keys',[
              'config' => [
                'curl' => [
                  // CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
                    CURLOPT_USERPWD  => 'admin:password'
                ]
              ],
                'headers' => [
                    'Content-Type' => 'application/json',
                    'Accept'       => 'application/json',
                ]
                ]);
        
            print_r($client->getBody()->getContents());
            
          }catch (Exception $e) {
            print_r([
                "status" => false,
                "message" => $e->getMessage()
            ]);
        }

我沒有返回安全令牌,而是收到一條錯誤消息“401 Unauthorized` response” - 這意味着沒有收到正確的身份驗證。

我到底做錯了什么?

先感謝您。

我相信你不是很清楚如何在 guzzle 中使用 curl 選項只使用 curl 作為請求選項不需要使用配置。(見文檔

<?php
require "../vendor/autoload.php";

use GuzzleHttp\Client;
    
    try{
    
      $client = new Client(['base_uri' => 'https://host.com/api/v1/']);
      
      $guzzleResponse = $client->request('POST', 'auth/keys', [
                'curl' => [
                  CURLOPT_USERPWD  => 'admin:password'
                ],
              
                'headers' => [
                    'Content-Type' => 'application/json',
                    'Accept'       => 'application/json',
                ]
            ]);
    
        print_r($guzzleResponse->getBody()->getContents());
        
    } catch (GuzzleHttp\Exception\RequestException $e) {
        print_r([
            "status" => false,
            "message" => $e->getMessage(),
        ]);// you can use logs here like monolog library
    } catch(Exception $e){
        print_r([
            "status" => false,
            "message" => $e->getMessage(),
        ]);
    }

方法二

參考這個答案,我相信你也可以使用 Basic Auth http header。

$encodedAuth = base64_encode( $usename.":".$passwd);

// ... other same as above

 $guzzleResponse = $client->request('POST', 'auth/keys', [
                'headers' => [
                    'Authorization' => 'Bearer '. $encodedAuth,
                    'Content-Type' => 'application/json',
                    'Accept'       => 'application/json',
                ]
            ]);
// ... other same as above

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM