You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CodeIgniter充值订单API报'Invalid Input'错误排查请求

CodeIgniter充值订单API「Invalid Input」错误排查方案

以下是导致该错误的常见问题及修复建议:

  • 输入参数校验不严谨
    当前仅通过isset()判断参数是否存在,但未验证参数的有效性(如是否为空、格式是否合法)。比如$service_id或$user_id为空字符串时,传入模型查询会返回无效结果,触发后续的「Invalid Input」分支。
    修复示例:

    // 校验service_id合法性
    if (!isset($data['service_id']) || !is_numeric($data['service_id']) || $data['service_id'] <= 0) {
        echo json_encode(['status' => 'error', 'message' => 'Invalid service_id']);
        http_response_code(400);
        exit;
    }
    // 同理对user_id、category_id做相同校验
    
  • 模型查询结果未做空值与有效性判断
    代码直接访问$get_balance_by_service_id['price'],但如果$service_id无效,模型可能返回空数组或false,此时访问数组下标会触发PHP警告,且if条件判断失败,进入错误分支。此外,如果用户余额为0,$check_if_user_got_balance返回0,if ($check_if_user_got_balance)会直接判定为假,误返回「Invalid Input」。
    修复示例:

    $get_balance_by_service_id = $this->model->get_balance_by_service_id($service_id);
    if (!$get_balance_by_service_id || !isset($get_balance_by_service_id['price']) || $get_balance_by_service_id['price'] <= 0) {
        echo json_encode(['status' => 'error', 'message' => 'Invalid service or price']);
        http_response_code(404);
        exit;
    }
    
    $check_if_user_got_balance = $this->model->check_if_user_got_balance($user_id);
    // 假设模型返回false表示用户不存在,数字表示余额
    if ($check_if_user_got_balance === false) {
        echo json_encode(['status' => 'error', 'message' => 'User not found']);
        http_response_code(404);
        exit;
    }
    
  • 第三方API响应错误被笼统归类为「Invalid Input」
    当cURL请求第三方API返回非200状态码时,不管实际是服务报错、参数错误还是其他问题,都统一返回「Invalid Input」,无法定位真实原因。
    修复示例:

    } else {
        // 解析第三方API的错误信息(需根据实际返回格式调整)
        $third_party_error = json_decode($output, true)['message'] ?? 'Third-party service error';
        $response = ['status' => 'error', 'message' => $third_party_error];
        http_response_code($httpStatus);
        echo json_encode($response);
        exit;
    }
    
  • 未处理未授权及分支遗漏的情况
    当Authorization头为空、格式错误、JWT验证失败时,代码没有返回任何响应,导致请求无输出,前端可能误判为「Invalid Input」。同时,$_SESSION['uid']未做校验,如果Session失效,后续数据库操作会失败,但错误提示不明确。
    修复示例:

    // 在函数末尾添加默认未授权响应
    $response = ['status' => 'error', 'message' => 'Unauthorized'];
    http_response_code(401);
    header('Content-Type: application/json');
    echo json_encode($response);
    
    // 在使用$_SESSION['uid']前添加校验
    if (!isset($_SESSION['uid']) || empty($_SESSION['uid'])) {
        echo json_encode(['status' => 'error', 'message' => 'Session expired']);
        http_response_code(401);
        exit;
    }
    
  • 订单数据中service_id被设为空字符串
    创建订单时"service_id" => '',如果数据库中该字段为必填项,会导致插入失败,但当前代码未捕获数据库错误,可能间接引发流程中断,建议修正为传入正确的$service_id。

内容的提问来源于stack exchange,提问作者Saad Alti

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 10:25:56