php – SwaggerUI传递参数

前端之家收集整理的这篇文章主要介绍了php – SwaggerUI传递参数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个像这样定义的swagger.json:
  1. "/API/GetTieredInventory": {
  2. "post": {
  3. "summary": "Get Tiered inventory from ID","description": "Gets Tiered inventory for passed ID/IC combination","produces": [
  4. "application/json"
  5. ],"parameters": [
  6. {
  7. "name": "id","in": "path","description": "ID to retrieve Tiered inventory for","required": true,"type": "string"
  8. },{
  9. "name": "ic","description": "IC to retrieve Tiered inventory for","type": "string"
  10. }
  11. ],"responses": {
  12. "200": {
  13. "description": "successful operation"
  14. },"500": {
  15. "description": "Internal error"
  16. }
  17. }
  18. }
  19. }
  20. },

现在,我的API采用如下参数:

  1. private function GetTieredInventory() {
  2. if($this->get_request_method() != "POST"){
  3. $this->response('Error code 405,Method not allowed.',405);
  4. }
  5. if(is_array($this->_request['ic'])) {
  6. $v = array();
  7. foreach($this->_request['ic'] as $i) {
  8. $v[] = "'".$i."'";
  9. }
  10. $ic=html_entity_decode(implode(',',$v ));
  11. } else {
  12. $ic = "'".$this->_request['ic']."'";
  13. }
  14. $id=pg_escape_string($this->_request['id']);
  15.  
  16. <SNIP DB CODE>
  17.  
  18. try
  19. {
  20. $results = pg_query($sql);
  21. if(pg_num_rows($results) == 0) {
  22. $rows = [];
  23. }
  24. else
  25. {
  26. $data = pg_fetch_all($results);
  27. foreach($data as $item)
  28. {
  29. $rows[$item["ic"]][] = $item;
  30. }
  31. }
  32. pg_free_result($results);
  33. }
  34. catch (Exception $e)
  35. {
  36. $err = array("message"=>$e->getMessage(),"code"=> $e->getCode(),"error"=>$e->__toString().",\n".print_r($_REQUEST,true));
  37. $this->response($this->toJSON($err),500);
  38. }
  39. echo json_encode($rows);
  40. }

价值是什么并不重要,我仍然得到一个
注意:未定义的索引:ic和
注意:未定义的索引:id错误
回.

我已经尝试了所有这三个参数:

  1. "in": "path","in": "query","in": "body",

这是在我的服务中调用api的方式:

  1. $fields = array(
  2. 'id' => $this->compId,'ic' => $ic
  3. );
  4. $fields_string = http_build_query($fields);
  5.  
  6. $url = "/API/GetTieredInventory";
  7. $ch = curl_init();
  8. curl_setopt($ch,CURLOPT_URL,$url);
  9. curl_setopt($ch,CURLOPT_POST,count($fields));
  10. curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
  11. curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
  12. $response = curl_exec($ch);

像这样改变了参数定义,因为ic可以是一个值数组:

  1. "parameters": [
  2. {
  3. "name": "id","type": "string"
  4. },{
  5. "name": "ic","schema": {
  6. "type": "array","items": {
  7. "type": "string","style": "form"
  8. }
  9. },"description": "Interchange to retrieve Tiered inventory for","required": true
  10. }
  11. ],

但仍然是同样的错误

猜你在找的PHP相关文章