Api Bundle Symfony设置授权标头

时间:2014-03-01 10:59:49

标签: symfony oauth authorization

在Symfony 2中,我正在使用此捆绑库(https://github.com/LeaseWeb/LswApiCallerBundle)来进行API请求。 这是执行此操作的主要功能:

$output = $this->get('api_caller')->call(new HttpPostJson($url, $parameters));

我想为oAuth 2设置一个Authentication Header!

由于

2 个答案:

答案 0 :(得分:1)

我在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();
}

我刚刚添加了

    $curl->setopt(CURLOPT_HTTPHEADER, array(
        'Authorization: ' . $authorization
    ));

在每个需要授权的API调用中。

答案 1 :(得分:0)

您不必编写代码,因为该库已准备好接收任何卷曲选项。

将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
            )
        );
相关问题