简体   繁体   English

PHP API 端点在 Google Cloud Platform 中部署时不返回任何 JSON 数据

[英]PHP API Endpoint not returning any JSON data when deployed in Google Cloud Platform

So I have a project of mine which we had been testing in a server using Google Cloud Platform.所以我有一个我的项目,我们一直在使用谷歌云平台在服务器上进行测试。 However, there is a bug where my API endpoint is not returning anything from an AJAX get request when a specific query string id is specified (does not happen in localhost).但是,存在一个错误,即当指定特定查询字符串 id 时,我的 API 端点未从 AJAX get 请求返回任何内容(在 localhost 中不会发生)。 Here is the code, I am also aware about the sql injection risks so bear with me.这是代码,我也知道 sql 注入风险,所以请耐心等待。

PHP Endpoint Script: PHP端点脚本:

<?php
    header("Content-Type: application/json");
    include_once __DIR__.'/../helpers/mysql.php';
    include_once __DIR__.'/../helpers/helper.php';

    $helper = new Helper();
    $db = new Mysql_Driver();

    if (isset($_GET["module_id"]))
    {
        $module_id = $_GET["module_id"];
        $db->connect();
        $query = "SELECT m.*, i.item_path FROM Module m INNER JOIN Item i ON m.module_id = i.module_id WHERE m.module_id=$module_id";
        $moduleInfoResult = $db->query($query);
        // $moduleInfo = $db->fetch_array($moduleInfoResult);

        if ($db->num_rows($moduleInfoResult) > 0) {
            $moduleInfoResult = $db->fetch_array($moduleInfoResult);
            $response = array(
                'status' => 200,
                'message' => 'Success',
                'data' => $moduleInfoResult
            );
        } else {
            $response = array(
                'status' => 404,
                'message' => 'Query Failed: Query return 0 records'
            );
        }
    } else {
        $response = array(
            'status' => 400,
            'message' => 'Error Occured: Must provide module_id query string'
        );
    }

    echo json_encode($response)
?>

Screenshots of result using postman (id that is working) :使用邮递员的结果截图(正在工作的 id) 在此处输入图片说明

Screenshots of result using postman (id that is not working but works in localhost) :使用邮递员的结果截图(id 不起作用但在 localhost 中有效) 在此处输入图片说明

What I have tried I tried echo'ing out the response and by using Postman I realized that ids that are returning nothing has this character:我尝试过的我尝试回显响应,并通过使用 Postman 我意识到没有返回任何内容的 ID 具有以下特征:

在此处输入图片说明

Is there a way to prevent or escape this character?有没有办法阻止或逃避这个角色?

After a lot of research and debugging, I found out that the problem was character encoding!经过大量的研究和调试,我发现问题出在字符编码上! So what I did to solve the problem was to UTF-8 my mysql db and mysqli Reference link: Stack Overflow Thread Credits to @04FS所以我为解决这个问题所做的是将我的 mysql db 和 mysqli 参考链接改为 UTF-8: Stack Overflow Thread Credits to @04FS

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM