在Symfony 2中,我使用此捆綁庫(https://github.com/LeaseWeb/LswApiCallerBundle)來製作API請求。 這是做它的主要功能:Api Bundle Symfony設置授權標頭
$output = $this->get('api_caller')->call(new HttpPostJson($url, $parameters));
我想設置一個認證頭是OAuth 2!
謝謝
在Symfony 2中,我使用此捆綁庫(https://github.com/LeaseWeb/LswApiCallerBundle)來製作API請求。 這是做它的主要功能:Api Bundle Symfony設置授權標頭
$output = $this->get('api_caller')->call(new HttpPostJson($url, $parameters));
我想設置一個認證頭是OAuth 2!
謝謝
我在1個月前使用過這個庫。我想通過自定義類HttpPostJson。
你應該做這樣的事情:
in Lsw\ApiCallerBundle\Call
public function makeRequest($curl, $options, $authorization)
{
$curl->setopt(CURLOPT_URL, $this->url);
$curl->setopt(CURLOPT_POST, 1);
$curl->setopt(CURLOPT_POSTFIELDS, $this->requestData);
$curl->setopt(CURLOPT_HTTPHEADER, array(
'Authorization: ' . $authorization
));
$curl->setoptArray($options);
$this->responseData = $curl->exec();
}
我只是在需要授權每個API調用添加
$curl->setopt(CURLOPT_HTTPHEADER, array(
'Authorization: ' . $authorization
));
。
您不必編寫代碼,因爲庫已經準備好接收任何捲曲選項。
傳遞httpheaders像關聯數組作爲下一個例子:
//it's a fake test
$url = 'http://www.example.com';
$data = array();
$returnAssociativeArray = true;
//add curl options
$options = array(
'userpwd' => 'demo:privateKey'
'httpheader' => array('Content-type' => 'application/json')
);
$json = $this->container->get('api_caller')->call(
new HttpPostJson(
$url,
$data,
$returnAssociativeArray,
$options
)
);
在httppostjson()的第二個參數是LEASEWEB/LswApiCallerBundle /呼叫/ CurlCall.php和它的構造的一個目的是 公共函數__construct($ url,$ requestObject,$ asAssociativeArray = false) { $ this-> url = $ url; $ this-> requestObject = $ requestObject; $ this-> asAssociativeArray = $ asAssociativeArray; $ this-> generateRequestData(); } –
@abbiya謝謝!但我應該把授權:$授權?在我的例子中 – user3037814