Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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
            )
        );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top