duancai1904 2015-02-09 09:42
浏览 319
已采纳

如何在guzzle中传递参数来删除请求

I am using guzzle as http client for testing my symfony api.

There is url option given in documentation,but how do I pass userid & api id parameter so this can delete a particular record of particular user.

When I tested with curl

curl -i -X DELETE http://localhost/us/serenify/web/app_dev.php/userapi/delete/1/6

My api is working fine showing appropriate responses.

But I am not able to test it with guzzle as I am unable to find option for passing my parameter.

  • 写回答

1条回答 默认 最新

  • duanrang2627 2015-02-09 22:07
    关注

    Here is an example that defines and executes your Symfony route:

    {
        "operations": {
            "deleteEntity": {
                "httpMethod": "DELETE",
                "uri": "/userapi/delete/{userid}/{apiid}",
                "summary": "Deletes an entity",
                "parameters": {
                    "userid": {
                        "location": "query"
                    },
                    "apiid": {
                        "location": "query"
                    }
                }
            }
        }
    }
    

    and the code:

    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);
    

    Now, as it currently stands, the generated route would look something like

    http://localhost/us/serenify/web/app_dev.php/userapi/delete?userid=1&apiid=6.

    If you want the parameters not to be passed by Guzzle as query paramters, but instead like URL parameters, all you have to do is change their type in the JSON definition file from query to uri.

    PS: I did NOT test the code above. Might work out-of-the-box or it might not. Minor tweaks may be required.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?