为什么JSON响应没有通过回显得到?

时间:2019-01-25 10:53:18

标签: php json

我在Android应用程序的服务器端使用PHP。该应用使用POST方法发送登录参数 rollno 密码。代码正在获取结果并以JSON回显响应。我已经在实时服务器和本地主机上托管了相同的系统。实时服务器工作正常,但本地服务器未回显响应。代码如下:

// an array to display response
$response = array();
if (isset($_GET['apicall'])) {
    switch ($_GET['apicall']) {
    case 'login':
        // this part will handle the login
        // for login we need the email and password
        if (isTheseParametersAvailable(array(
            'rollno',
            'password'
        ))) {
            // getting server ip for building image url
            $server_ip = gethostbyname(gethostname());
            // image upload path
            define('UPLOAD_PATH', 'uploads/');
            // getting values
            $rollno = $_POST['rollno'];
            $user_password = $_POST['password'];
            $sql = "SELECT password from users WHERE rollno = \"$rollno\"";
            $result = $conn->query($sql);
            while ($row = $result->fetch_assoc()) {
                $pass_hash = $row["password"];
            }
            if (password_verify($user_password, $pass_hash)) {
                // creating the query
                $stmt = $conn->prepare("SELECT id, username, email, gender, rollno, image, personal_notice FROM users WHERE rollno = ?");
                $stmt->bind_param("s", $rollno);
                $stmt->execute();
                $stmt->store_result();
                // query for fetching fees records
                $feesqry = $conn->prepare("SELECT total, paid, remaining FROM fees WHERE rollno = ?");
                $feesqry->bind_param("s", $rollno);
                $feesqry->execute();
                $feesqry->store_result();
                // query for fetching attendance records
                $attqry = $conn->prepare("SELECT mon, tue, wed, thrs, fri, sat, sun FROM attendance WHERE rollno = ?");
                $attqry->bind_param("s", $rollno);
                $attqry->execute();
                $attqry->store_result();
                // query for fetching test records
                $tstqry = $conn->prepare("SELECT tst1_date, tst1_marks, tst1_rank, tst2_date, tst2_marks, tst2_rank, tst3_date, tst3_marks, tst3_rank, tst4_date, tst4_marks, tst4_rank, tst5_date, tst5_marks, tst5_rank FROM test WHERE rollno = ?");
                $tstqry->bind_param("s", $rollno);
                $tstqry->execute();
                $tstqry->store_result();
                // query for fetching common notice
                $noticeqry = $conn->prepare("SELECT common_notice FROM notice");
                $noticeqry->execute();
                $noticeqry->store_result();
                // if the user exist with given credentials
                if ($stmt->num_rows > 0) {
                    $stmt->bind_result($id, $username, $email, $gender, $rollno, $image_temp, $personal_notice);
                    $stmt->fetch();
                    $image = 'http://' . $server_ip . '/arsod_dynamic/' . UPLOAD_PATH . $image_temp;
                    $feesqry->bind_result($total, $paid, $remaining);
                    $feesqry->fetch();
                    $attqry->bind_result($mon, $tue, $wed, $thrs, $fri, $sat, $sun);
                    $attqry->fetch();
                    $tstqry->bind_result($tst1_date, $tst1_marks, $tst1_rank, $tst2_date, $tst2_marks, $tst2_rank, $tst3_date, $tst3_marks, $tst3_rank, $tst4_date, $tst4_marks, $tst4_rank, $tst5_date, $tst5_marks, $tst5_rank);
                    $tstqry->fetch();
                    $noticeqry->bind_result($common_notice);
                    $noticeqry->fetch();
                    $user = array(
                        'id' => $id,
                    // Various other key/values here
                    $response['user'] = $user;
                }
                else {
                    // if the user not found
                    $response['error'] = false;
                    $response['message'] = 'Invalid Roll No. or Password';
                }
            }
            else {
                // if the user not found
                $response['error'] = false;
                $response['message'] = 'Invalid Roll No. or Password';
            }
        }
        break;
        else {
            // if it is not api call
            // pushing appropriate values to response array
            $response['error'] = true;
            $response['message'] = 'Invalid API Call';
        }
        // displaying the response in json structure
        echo json_encode($response);
        // function validating all the paramters are available
        // we will pass the required parameters to this function
        function isTheseParametersAvailable($params)
        {
            // traversing through all the parameters
            foreach($params as $param) {
                // if the paramter is not available
                if (!isset($_POST[$param])) {
                    // return false
                    return false;
                }
            }
            // return true if every param is available
            return true;
        }

上面的代码还有echo json_encode($response);行,也没有返回json。请帮忙。

像这样在实时服务器上成功输出JSON

{"error":false,"message":"Login successfull","user":{"id":498,"username":"Test Entry","email":null,"gender":"NA","rollno":1000,"image":"https:\/\/softglobe.net\/client-projects\/arsodclasses\/dynamic\/uploads\/default-pic.png","personal_notice":"Not available yet","common_notice":"Freedom in Mind;Faith in Words;Pride in our Heart; Memories in our Souls.Let\u2019s salute the Nation on Republic Day.HAPPY REPUBLIC DAY!","total":"-","paid":"-","remaining":"-","mon":"-","tue":"-","wed":"-","thrs":"-","fri":"-","sat":"-","sun":"-","tst1_date":"-","tst1_marks":"-","tst1_rank":"-","tst2_date":"-","tst2_marks":"-","tst2_rank":"-","tst3_date":"-","tst3_marks":"-","tst3_rank":"-","tst4_date":"-","tst4_marks":"-","tst4_rank":"-","tst5_date":"-","tst5_marks":"-","tst5_rank":"-"}}

但是在本地服务器上,没有任何内容作为响应打印出来。

1 个答案:

答案 0 :(得分:0)

您在第一个切换案例结束后回响了响应。在break语句前回显