简单的PHP Web服务没有返回错误消息

时间:2014-05-12 13:45:24

标签: php web-services rest

我使用以下代码通过Web服务检查用户登录系统。当我在网址中使用正确的信息时,一切都还可以,但很快我就错了,它不会给我带来错误。

代码:

function service_response($api_response){

   $http_response_code = array(
        200 => 'OK',
        400 => 'Bad Request',
        401 => 'Unauthorized',
        403 => 'Forbidden',
        404 => 'Not Found'
    );

    header('HTTP/1.1 '.$api_response['status'].' '.$http_response_code[ $api_response['status'] ]);
    header('Content-Type: application/json; charset=utf-8');
    $json_response = json_encode($api_response);
    echo $json_response;
    exit;
}

$api_response_code = array(
    0 => array('HTTP Response' => 400, 'Message' => 'Unknown Error'),
    1 => array('HTTP Response' => 200, 'Message' => 'Success'),
    2 => array('HTTP Response' => 403, 'Message' => 'HTTPS Required'),
    3 => array('HTTP Response' => 401, 'Message' => 'Authentication Required'),
    4 => array('HTTP Response' => 401, 'Message' => 'Authentication Failed'),
    5 => array('HTTP Response' => 404, 'Message' => 'Invalid Request'),
    6 => array('HTTP Response' => 400, 'Message' => 'Invalid Response Format')
);


if( isset($_GET['actionid']) && $_GET['actionid'] == 'login_user'){
    $email    = $_GET['email'];
    $password = $_GET['password'];
    $query    = mysql_query("SELECT user_id FROM user WHERE email = '".$email."' AND password = '".md5($password)."'");
    $data     = array();
        if(mysql_num_rows($query)>0) { 
            while($row = mysql_fetch_assoc($query)) {
                $data[] = $row;
            }
    $response['code']               = 1;
    $response['status']             = $api_response_code[$response['code']]['HTTP Response'];
    $response['response_message']   = $api_response_code[$response['code']]['Message'];
    $response['message']            = 'You are logged In successfully';
    $response['data']               = $data;
    service_response($response);    

        }else{

    $response['code']               = 0;
    $response['status']             = $api_response_code[$response['code']]['HTTP Response'];
    $response['response_message']   = $api_response_code[$response['code']]['Message'];
    $response['message']            = 'Please enter correct email address and password';
    $response['data']               = $data;
    service_response($response);
    }
}

返回正确的信息:

{"code":1,"status":200,"response_message":"Success","message":"You are logged In successfully","data":[{"user_id":"72"}]}

但是当我在我的网址中输入错误的详细信息时,它会返回:

Bad Request

虽然我想要这个:{"code":0,"status":404,"data":null}

网址包含正确的信息:http://xxxxxx/actions.php?actionid=login_user&email=mohsin@balianti.com&password=mohsin

错误信息的网址:http://xxxxxx/actions.php?actionid=login_user&email=mohsin@balianti.com&password=ddfsdfdsf

有人可以帮帮我..

谢谢大家

1 个答案:

答案 0 :(得分:1)

在您定义的else { }区块中:

$response['code']               = 0;

您要映射到

0 => array('HTTP Response' => 400, 'Message' => 'Unknown Error'),

和400映射到

400 => 'Bad Request',

证明你的脚本有效,哇!

如果您想更改此设置,请更改映射:

$response['code']               = 3; // or 4 or whatever you define