php – SwaggerUI传递参数

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – SwaggerUI传递参数脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个像这样定义的swagger.json:
"/API/GetTieredInventory": {
        "post": {
            "summary": "Get Tiered inventory From ID","description": "Gets Tiered inventory for passed ID/IC combination","PRoduces": [
                "application/json"
            ],"parameters": [
                {
                    "name": "id","in": "path","description": "ID to retrieve Tiered inventory for","required": true,"tyPE": "string"
                },{
                    "name": "ic","description": "IC to retrieve Tiered inventory for","type": "string"
                }
            ],"responses": {
                "200": {
                    "description": "successful operation"
                },"500": {
                    "description": "Internal error"
                }
            }
        }
    }
},

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

private function GetTieredInventory() {
    if($this->get_request_method() != "POST"){
        $this->response('Error code 405,Method not Allowed.',405);
    }
    if(is_array($this->_request['ic'])) {
        $v = array();       
        foreach($this->_request['ic'] as $i) {
            $v[] = "'".$i."'";
        }
        $ic=htML_entITy_decode(implode(',',$v ));
    } else {
        $ic = "'".$this->_request['ic']."'";
    }
    $id=pg_escape_string($this->_request['id']);

    <SNIP DB CODE>

    try 
    {       
        $results = pg_query($sql);
        if(pg_num_rows($results) == 0) {
            $rows = [];
        }
        else
        {
            $data = pg_fetch_all($results);
            foreach($data as $item)
            {                    
                $rows[$item["ic"]][] = $item;
            }
        }
        pg_free_result($results);
    }
    catch (Exception $e) 
    {
        $err = array("message"=>$e->getMessage(),"code"=> $e->getCode(),"error"=>$e->__toString().",\n".print_r($_REQUEST,true));
        $this->response($this->toJSON($err),500);
    }
    echo json_encode($rows);
}

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

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

"in": "path","in": "query","in": "body",

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

$fields = array(
    'id' => $this->compId,'ic'    => $ic
); 
$fields_string = http_build_query($fields);

$url = "/API/GetTieredInventory";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch,CURLOPT_RETURNtransfer,true);
$response = curl_exec($ch);

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

"parameters": [
    {
        "name": "id","type": "string"
    },{
        "name": "ic","schema": {
            "type": "array","items": {
                "type": "string","style": "form"
            }
        },"description": "Interchange to retrieve Tiered inventory for","required": true
    }
],

但仍然是同样的错误

本图文内容来网友网络收集整理提供,作为学习参考使用,版权属于原作者。

脚本宝典总结

以上是脚本宝典为你收集整理的php – SwaggerUI传递参数全部内容,希望文章能够帮你解决php – SwaggerUI传递参数所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。