Php 我可以在Guzzle';什么是路由文件?
我经常向Web服务发出请求 我的JSON文件如下所示:Php 我可以在Guzzle';什么是路由文件?,php,rest,http-headers,guzzle,Php,Rest,Http Headers,Guzzle,我经常向Web服务发出请求 我的JSON文件如下所示: { "name": "Webservice name", "apiVersion": "1.0", "description": "description", "operations": { "commandName1": { "httpMethod": "POST", "uri": "some/uri/to/some/resource/{val
{
"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);
使用它的代码如下所示:
{
"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代码中指定了内容类型
和接受
标题。是否有某种方法可以在JSON文件中移动该信息,并为每个操作指定不同的值?例如:我希望“commandName1”的内容类型为HTML,但“commandName2”的内容类型为JSON
我想这样做是为了避免大量的代码重复
在过去的两个小时里,我一直在网上和Guzzle的文档中搜索,结果一无所获。然而,在我看来,文档编写得有点糟糕,我在阅读时确实错过了过去的事情。所以很有可能再次发生
有人做过这样的事吗?你是怎么解决的?先谢谢你
1=我所说的“写得不好”实际上是指每个部分都不完整。每一章似乎都涉及一个主题,但从来没有提供对参数、方法等或其全部功能的实际完整或深入的描述。没有任何代码片段是一个可复制的,因此您可以在不到2分钟的复制粘贴过程中看到它在眼前工作。但这是另一个主题…我查看了Guzzle的源代码,实际上没有办法将此类信息添加到JSON文件中 然而,我成功地改变了这一点:
$params = array(
'command.request_options' = array(
'timeout' => 5,
'connect_timeout' => 2
)
);
为此:
$params = array(
'command.request_options' => array(
'timeout' => 5,
'connect_timeout' => 2
),
'command.headers' => array(
'Accept' => 'whatever value I want',
'Content-Type' => 'whatever value I want'
)
);
它成功了
由于代码的这一部分位于每个其他类使用的单独/公共类中,因此没有代码重复,因此它可以工作。。。有点像。同意写得不好的文档。。。