user3037814
user3037814

Reputation: 153

Api Bundle Symfony set authorization header

In Symfony 2 I am using this bundle library (https://github.com/LeaseWeb/LswApiCallerBundle) to make API REQUEST. This is the main function to do it:

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

I would like to set an Authentication Header for oAuth 2!

Thanks

Upvotes: 0

Views: 1392

Answers (2)

Apuig
Apuig

Reputation: 360

You don't have to write code because the library is already prepared to receive any curl option.

Pass the httpheaders like an associative array as the next example:

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

Upvotes: 0

Danilo Paone
Danilo Paone

Reputation: 148

I used that library about 1 month ago. I figured it out just customizing the class HttpPostJson.

You should do something like this:

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

I just added

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

in every API Call that needed an authorization.

Upvotes: 1

Related Questions