Php 如何在guzzle中传递参数以删除请求
我使用guzzle作为http客户端来测试我的symfony api 文档中给出了url选项,但如何传递userid和api id参数,以便删除特定用户的特定记录 当我用curl测试的时候 curl-i-X删除 我的api运行良好,显示了适当的响应Php 如何在guzzle中传递参数以删除请求,php,symfony,curl,Php,Symfony,Curl,我使用guzzle作为http客户端来测试我的symfony api 文档中给出了url选项,但如何传递userid和api id参数,以便删除特定用户的特定记录 当我用curl测试的时候 curl-i-X删除 我的api运行良好,显示了适当的响应 但是我无法用guzzle测试它,因为我找不到传递参数的选项。下面是一个定义和执行Symfony路由的示例: { "operations": { "deleteEntity": { "httpMethod
但是我无法用guzzle测试它,因为我找不到传递参数的选项。下面是一个定义和执行Symfony路由的示例:
{
"operations": {
"deleteEntity": {
"httpMethod": "DELETE",
"uri": "/userapi/delete/{userid}/{apiid}",
"summary": "Deletes an entity",
"parameters": {
"userid": {
"location": "query"
},
"apiid": {
"location": "query"
}
}
}
}
}
以及守则:
class MyApi
{
protected $client;
public function __construct(ClientInterface $client, $baseUrl)
{
$this->client = $client;
//tell the client what the base URL to use for the request
$this->client->setBaseUrl($baseUrl);
//fill the client with all the routes
$description = ServiceDescription::factory("/path/to/routes.json");
$this->client->setDescription($description);
}
public function deleteEntity($userId, $apiId)
{
$params = array(
'userid' => $userId,
'apiid' => $apiId
);
$command = $this->client->getCommand('deleteEntity', $params);
$command->prepare();
$response = $this->client->execute($command);
return $response;
}
}
$client = new Guzzle\Service\Client();
$api = new MyApi($client, ' http://localhost/us/serenify/web/app_dev.php');
$api->deleteEntity(1, 6);
现在,就目前的情况而言,生成的路由看起来像
如果您不希望Guzzle将参数作为查询参数传递,而是像URL参数一样传递,那么您所要做的就是将JSON定义文件中的参数类型从query更改为uri
PS:我没有测试上面的代码。可能是现成的,也可能不是。可能需要进行细微调整。您收到了什么错误消息?这是我的api用户,其中1是用户id,7是api id。我尝试了$request=$client->createRequest'delete';这是它现在的工作。我尝试了$request=$client->createRequest'delete',';&尝试通过设置字段传递参数,但它不起作用。如果您有任何传递数据的示例,请让我知道。您想将参数嵌入url,还是想将它们作为http get/post请求从一个页面传递到另一个页面?我正在创建api。上面是我的api url。我只想向我的url传递两个参数,但这些不需要查询字符串,因为我使用的是MVC结构,我的控制器函数需要两个参数。我已经通过curl测试了api,它工作得很好。但我想根据客户的要求通过guzzle来测试这一点。与论坛网站不同,我们不使用感谢、感谢的帮助或签名。看见顺便说一句,这是提前感谢,不是提前感谢。嗨,伙计们,我找到了一个方法,没有必要做任何事情。我以不同的方式实现了。公共函数testDeleteAction{$client=new client;$request=$client->createRequest'Delete',;$response=$client->send$request;echo;exit;}@hiteshrane您正在对URL中的值进行硬编码,这不太理想。看看我上面的代码,将根据您给MyApi::deleteEntity方法的参数生成不同的URL。这是更好的和可维护的。