How do I do HTTP basic authentication using Guzzle?

前端 未结 8 1387
无人及你
无人及你 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: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();
    

提交回复
热议问题