我正在使用服务描述创建Guzzle客户端。服务描述中的每个操作都包含一个URI。我正在访问的REST端点需要一个授权标头,该标头通过将公钥和端点的uri粘在一起,然后从结果字符串创建md5。这用作授权值。
我不知道如何在实例化客户端之后从服务描述中获取uri值。
我正在创建这样的Guzzle客户端:
class RestClient extends Client
{
public static function factory($config = array())
{
// The following values are required when creating the client
$required = array(
'base_url',
'public_key',
'private_key'
);
$path = drupal_get_path('module', 'junkrest');
// Merge in default settings and validate the config
$config = Collection::fromConfig($config, $required);
// Create a new client
$client = new self($config->get('base_url'), $config);
// Set the service description
$client->setDescription(ServiceDescription::factory($path . '/config/services.json'));
$authstring = md5($public_key, 'the uri value from an operation in the services.json file');
$client->setDefaultHeaders(array(
'Authentication' => $authstring));
return $client;
}
}
The services.json file contains this:
{
"name": "TheName",
"apiVersion": "1",
"baseUrl": "https://apidev.example.com",
"description": "Custom REST API client",
"operations": {
"GetFranchiseList": {
"httpMethod": "GET",
"uri": "v1/franchise",
"summary": "Returns an array of franchises."
},
"GetReviews": {
"httpMethod": "GET",
"uri": "v1/review",
"summary": "Returns an array of reviews."
}
}
}
如何访问GetFranchiseList中的'uri'值,以便我可以用它来创建$ authstring?
答案 0 :(得分:3)
客户端有一个包含操作的服务描述对象。这些操作包含各种属性的getter方法以及用作输入的每个参数。
例如:
$client->getDescription()->getOperation('GetFranchiseList')->getUri();