dongzi5673 2014-05-12 13:45
浏览 58
已采纳

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

i am using below code for checking user login system by web services. all is ok when i used correct information in url but soon i used wrong it does not return me the error.

code:

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

return correct information:

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

But when i put wrong details in my url it returns:

Bad Request

while i want this: {"code":0,"status":404,"data":null}

url with correct info: http://xxxxxx/actions.php?actionid=login_user&email=mohsin@balianti.com&password=mohsin

url with wrong info: http://xxxxxx/actions.php?actionid=login_user&email=mohsin@balianti.com&password=ddfsdfdsf

can somebody please help me..

thank you all

  • 写回答

1条回答 默认 最新

  • doureng5668 2014-05-12 14:00
    关注

    In the else { } block you defined:

    $response['code']               = 0;
    

    This you are mapping to

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

    and 400 is mapped to

    400 => 'Bad Request',
    

    Proving your script works, wow!

    If you want to change this, change your mappings:

    $response['code']               = 3; // or 4 or whatever you define
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable