PHP总是返回错误的邮递员空参数?

时间:2020-06-02 22:26:46

标签: php parameters get postman

我正在尝试使php获取,但它似乎总是调用haveEmptyParameters。

该调用是对getInformation()的,并且工作正常。只是参数似乎从未经历过?也许还有另一个错误?

    $app->get('/getinformation', function(Request $request, Response $response){

    if(!haveEmptyParameters(array('field', 'username'), $request, $response)){
        $request_data = $request->getParsedBody(); 
        $field = $request_data['field'];
        $username = $request_data['username'];

        $db = new DbOperations; 
        $result = $db->getInformation($field,$username);

        $response_data = array();
        $response_data['error'] = false; 
        $response_data['users'] = $result; 

        $response->write(json_encode($response_data));

        return $response
        ->withHeader('Content-type', 'application/json')
        ->withStatus(200);  
    }


});



function haveEmptyParameters($required_params, $request, $response){
$error = false; 
$error_params = '';
$request_params = $request->getParsedBody(); 

foreach($required_params as $param){
    if(!isset($request_params[$param]) || strlen($request_params[$param])<=0){
        $error = true; 
        $error_params .= $param . ', ';
    }
}

if($error){
    $error_detail = array();
    $error_detail['error'] = true; 
    $error_detail['message'] = 'Required parameters ' . substr($error_params, 0, -2) . ' are missing or empty';
    $response->write(json_encode($error_detail));
}
return $error;


}

$app->run();

-----邮递员输出的错误---------

{"error":true,"message":"Required parameters field, username are missing or empty"}

0 个答案:

没有答案