Php 如何将guzzle服务描述模式映射到应用程序模型
我有一个基本的服务描述模式来获取如下所示的用户模型:Php 如何将guzzle服务描述模式映射到应用程序模型,php,guzzle,Php,Guzzle,我有一个基本的服务描述模式来获取如下所示的用户模型: { "name": "API", "baseUrl": "http://localhost/", "operations": { "GetUser": { "httpMethod": "GET", "uri": "users/{user_id}", "summary": "Show user details",
{
"name": "API",
"baseUrl": "http://localhost/",
"operations": {
"GetUser": {
"httpMethod": "GET",
"uri": "users/{user_id}",
"summary": "Show user details",
"responseClass": "GetUserOutput",
"parameters": {
"user_id": {
"location": "uri",
"description": "ID of the user to be returned"
}
}
}
},
"models": {
"User" : {
"type": "object",
"properties": {
"id": {
"location": "json",
"type": "integer",
"sentAs": "user_id"
},
"username": {
"location": "json",
"type": "string"
},
"email": {
"location": "json",
"type": "string"
}
}
},
"GetUserOutput": {
"$ref": "User"
}
}
}
我的客户执行以下操作:
require_once('../../vendor/autoload.php');
$client = new \Guzzle\Service\Client();
$client->setDescription(\Guzzle\Service\Description\ServiceDescription::factory(__DIR__ . '/client.json'));
$authPlugin = new \Guzzle\Plugin\CurlAuth\CurlAuthPlugin('23', '9bd2cb3f1bccc01c0c1091d7e88e51b208b3792b');
$client->addSubscriber($authPlugin);
$command = $client->getCommand('getUser', array('user_id' => 23));
$request = $command->prepare();
$request->addHeader('Accept', 'application/json');
try {
$result = $command->execute();
echo '<pre>' . print_r($result, true) . '</pre>';
}
如何将其映射到schema对象?或者更重要的是我自己的应用程序对象?显然,这里的解决方案不起作用:
"uri": "users/{user_id}",
"summary": "Show user details",
"responseType": "class",
"responseClass": "\\Users\\User",
不确定工作方式是否符合预期,但为了从架构中获取对象- 首先,我更改了json,将
class
的responseType
和模型目录的responseClass
包括在内:
public static function fromCommand(\Guzzle\Service\Command\OperationCommand $command)
{
$result = $command->getResponse()->json();
$user = new self();
$user->setId($result['user']['id']);
$user->setUsername($result['user']['username']);
$user->setEmail($result['user']['email']);
return $user;
}
然后在用户模型中,我在fromCommand
"uri": "users/{user_id}",
"summary": "Show user details",
"responseType": "class",
"responseClass": "\\Users\\User",
public static function fromCommand(\Guzzle\Service\Command\OperationCommand $command)
{
$result = $command->getResponse()->json();
$user = new self();
$user->setId($result['user']['id']);
$user->setUsername($result['user']['username']);
$user->setEmail($result['user']['email']);
return $user;
}