我如何使用Guzzle进行HTTP基本身份验证? [英] How do i do HTTP basic authentication using Guzzle?

查看:766
本文介绍了我如何使用Guzzle进行HTTP基本身份验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Guzzle进行基本的访问身份验证,并且我对编程非常陌生.我不知道该怎么办.我尝试使用curl进行此操作,但我的环境需要使用食指.

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 using guzzle.

推荐答案

如果您使用的是 Guzzle 5.0或更高版本,请

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'
    ]
]);

请注意,如果您使用的是 Guzzle 3.0或更早版本,则语法是不同的 .构造函数是不同的,并且您还需要在请求上显式使用send方法以获取响应:

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

这篇关于我如何使用Guzzle进行HTTP基本身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆