How do I do HTTP basic authentication using Guzzle?

前端 未结 8 1424
无人及你
无人及你 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

提交回复
热议问题