How do I do HTTP basic authentication using Guzzle?

前端 未结 8 1267
无人及你
无人及你 2020-12-24 00:11

I want to do basic access authentication using Guzzle and I am very new to programming. I have no clue what to do. I tried to do this using curl but my environment requires

相关标签:
8条回答
  • 2020-12-24 00:48

    You can also configure the auth params when instantiating the client instead of adding it to each request:

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

    Here are the various doc links for Guzzle 6:

    • Creating a Client
    • Request Options
    • Auth Request Options
    0 讨论(0)
  • 2020-12-24 00:57

    According to what @bourgeois247 said about base64 encoding, the following worked perfectly for me on Guzzle 6:

    $client = new Client();
    $credentials = base64_encode('username:password');
    $response = $client->post('url',
            [
                'headers' => [
                    'Authorization' => 'Basic ' . $credentials,
                ],
            ]);
    
    0 讨论(0)
  • 2020-12-24 01:00

    If you're using Guzzle 5.0 or newer, the docs say that basic auth is specified using the auth parameter:

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

    Please note that the syntax is different if you're using Guzzle 3.0 or earlier. The constructor is different, and you also need to explicitly use the send method on a request to get a response:

    $client = new Guzzle\Http\Client();
    $request = $client->get('http://www.server.com/endpoint');
    $request->setAuth('username', 'password');
    $response = $request->send();
    
    0 讨论(0)
  • 2020-12-24 01:06

    If you use it with symfony, you can also define it in your configuration file (config/packages/eight_points_guzzle.yaml for symfony4 or flex or config.yml for the other version)

    In your configuration file :

    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: ~
    

    Then, in your service, controller, etc....

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

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

    0 讨论(0)
  • 2020-12-24 01:07

    In additional to @amenadiel answer. Sometimes handy specify auth parameters in constructor:

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

    Then every request will use this default auth parameters.

    0 讨论(0)
  • 2020-12-24 01:08
    $response = $client->request( 'GET', 'your_url', [
                        'auth'    => [
                            'your_username',
                            'your_password'
                        ],
                        'headers' => [
                            'if you want to pass something in the headers'
                        ]
                    ]
                );
    
    0 讨论(0)
提交回复
热议问题