我使用Guzzle向网络服务发出请求。
我有看起来像这样的 JSON 文件:
{
"name": "Webservice name",
"apiVersion": "1.0",
"description": "description",
"operations": {
"commandName1": {
"httpMethod": "POST",
"uri": "some/uri/to/some/resource/{value}",
"summary": "description",
"parameters": {
"value": {
"location": "uri",
"description": "description"
}
}
},
"commandName2": {
"httpMethod": "POST",
"uri": "some/uri/to/some/resource/{value}",
"summary": "description",
"parameters": {
"value": {
"location": "uri",
"description": "description"
}
}
}
}
}
并且使用它的代码看起来像这样:
$client = new Client(); // instance of Guzzle\Service\Client
$this->client->setDefaultOption(
'auth',
array('admin', 'admin', 'Basic')
);
$this->client->setDefaultOption(
'headers',
array('Accept' => 'text/html', 'Content-Type' => 'text/html')
);
$description = ServiceDescription::factory('/path/to/json/file/with/routes');
$client->setDescription($description);
$params = array(
'command.request_options' = array(
'timeout' => 5,
'connect_timeout' => 2
)
);
$command = $client->getCommand('commandName1', $params);
$command->prepare();
$client->execute($command);
如您所见,我在 PHP 代码中指定了Content-Type
和标头。Accept
有什么方法可以在 JSON 文件中移动该信息并为每个操作指定不同的值?例如:我希望“commandName1”具有 HTML 作为内容类型,但“commandName2”具有 JSON。
我想这样做是为了避免大量的代码重复。
在过去的 2 个小时里,我一直在网上和 Guzzle 的文档中查找,结果一无所获。但是,在我看来,文档写得有点糟糕1我在阅读它时确实错过了一些东西。所以很有可能再次发生。
有没有人不得不做这样的事情?你是怎么解决的?先感谢您。
1 =“写得不好”实际上是指每个部分都不完整。每一章似乎都涉及一个主题,但从未真正完整或深入地描述参数、方法等或其全部功能。没有代码片段是SSCCE,因此您可以在不到 2 分钟的复制粘贴中看到它在您眼前工作。但这是另一个话题……