I have following code:
Client class
use GuzzleHttp\Client;
use GuzzleHttp\Command\Guzzle\GuzzleClient;
use GuzzleHttp\Command\Guzzle\Description;
class ApiClient extends Client
{
public static function factory($config = array())
{
$config = array_merge($config, json_decode(file_get_contents(__DIR__.'/Resources/config/client.json'), TRUE));
$guzzleClient = new GuzzleClient(new Client(), new Description($config));
return $guzzleClient;
}
}
Config settings
{
"operations": {
"me":{
"description": "Returns user profile details",
"httpMethod": "GET",
"uri": "users/me",
"parameters": {
"token": {
"type":"string",
"location": "header",
"sentAs": "Authorization"
}
}
}
}
}
```
Usage
$config = [
'baseUrl' => 'http://localhost/api/v1/'
];
$api_client = ApiClient::factory($config);
$token = 'Bearer V6tBMG6FKL7wLxZh116IGdvfDUtOvlFIJI61nrHp';
$command = $api_client->me(array(
'token' => $token
));
// returns an array of results
$results = $command->execute();
And I want add custom Autorizarion Bearer header to all requests, how I can do that? Add header to $client doesn't help cuz $command after call me method is NULL.
Thanks
UPDATED
Found error, It doesn't work cuz guzzle 4 configuration works in different way:
{
"operations": {
"me":{
"description": "Return solidoptics user profile details",
"httpMethod": "GET",
"uri": "users/me",
"responseModel": "getResponse",
"parameters": {
"token": {
"type":"string",
"location": "header",
"sentAs": "Authorization",
"required": true
}
}
}
},
"models": {
"getResponse": {
"type": "object",
"additionalProperties": {
"location": "json"
}
}
}
}
So now issue is not actual for me.