如何在Zend Framework 2 http请求中执行基本身份验证?

Juj*_*ngh 2 http zend-framework2

大家早上好,

我正在尝试调用需要基本身份验证的api.我正在尝试使用Zend Framework 2 Zend\Http\Request,我不确定哪种最佳方式进行Basic Auth或者我应该手动设置标头?我试图强迫自己使用ZF2并停止使用ZF1,所以请耐心等待,老习惯会很难受. ZF2

$http= new Zend\Http\Request();
$http->setUri('https://api.my.tv/analytics/views/daily/');
$http->....
Run Code Online (Sandbox Code Playgroud)

ZF1中,我曾经做过以下操作,非常方便.

$http = new \Zend_Http_Client();
$http->setUri('https://api.my.tv/analytics/views/daily/');
$http->setAuth($my_api_key, 'x', \Zend_Http_Client::AUTH_BASIC);
$request = $http->request(\Zend_Http_Client::POST);
Run Code Online (Sandbox Code Playgroud)

ZF2中是否有类似的方法,或者我应该自己编写.说实话,我不知道我是不是只使用guzzle更好.

Jujhar

Ocr*_*ius 5

方法也完全相同Zend\Http\Client:

$client  = new \Zend\Http\Client();

$client->setAuth($user, $password, \Zend\Http\Client::AUTH_BASIC);
$client->setUri('http://api.buto.tv/analytics/views/daily/');
$client->setMethod(\Zend\Http\Request::METHOD_POST);

$response = $client->send();
Run Code Online (Sandbox Code Playgroud)