How do I do HTTP basic authentication using Guzzle?

前端 未结 8 1405
无人及你
无人及你 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 01:14

    According to the Guzzle 6 documentation, you can do a request with basic authorization as simple as this:

    $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();
    

    NOTE: You don't need to use base64_encode() at all because it already does it before the request.

    I've tested and it works :)

    See more at: Guzzle 6 Documentation

提交回复
热议问题