为什么我的PHP API停止工作,不再返回JSON?

ki0zmccv  于 4个月前  发布在  PHP
关注(0)|答案(1)|浏览(32)

几年前,我为我的一些移动的应用程序创建了一个后端(PHP以JSON格式获取一些数据)。从那以后我就没有碰过这个代码。现在它在几周前停止工作了。我不是一个后端开发人员,所以我在这里没有太多经验,但几年前我认为创建自己的后端会更好,而不是使用Firebase/Serverless.不是我最好的主意:)
我尝试了什么:

  • 用Chrome检查URL-->一切正常(我可以看到我的JSON数据)
  • 在应用程序内部或Postman中检查:(Header:Content-Type text/html)
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="robots" content="noindex, nofollow">
<title>One moment, please...</title>
<style>
body {
    background: #F6F7F8;
    color: #303131;
    font-family: sans-serif;
    margin-top: 45vh;
    text-align: center;
}
</style>
</head>
<body>
<h1>Please wait while your request is being verified...</h1>
<form id="wsidchk-form" style="display:none;" action="/z0f76a1d14fd21a8fb5fd0d03e0fdc3d3cedae52f" method="get">
<input type="hidden" id="wsidchk" name="wsidchk"/>
</form>
<script>
(function(){
    var west=+((+!+[])+(+!+[]+!![]+!![]+!![]+[])+(+!+[]+!![]+!![]+!![])+(+!+[]+!![]+!![]+!![]+[])+(+!+[])+(+!+[]+!![]+!![]+!![]+!![]+!![]+!![]+!![]+[])+(+!+[]+!![]+!![]+!![]+!![])+(+!+[]+[])),
        east=+((+!+[])+(+!+[]+!![]+[])+(+!+[]+!![]+!![]+!![]+!![]+!![]+!![]+!![])+(+!+[]+[])+(+![])+(+!+[]+!![]+[])+(+!+[]+!![])+(+!+[]+!![]+!![]+[])),
        x=function(){try{return !!window.addEventListener;}catch(e){return !!0;} },
        y=function(y,z){x() ? document.addEventListener("DOMContentLoaded",y,z) : document.attachEvent("onreadystatechange",y);};
    y(function(){
        document.getElementById('wsidchk').value = west + east;
        document.getElementById('wsidchk-form').submit();
    }, false);
})();
</script>
</body>
</html>

字符串
这是我的PHP文件:

$response = array();

function saveResultOfQueryToArray($result){
  global $response;
  $response['workouts'] = array();

  while($row = mysqli_fetch_array($result)){

      $temp = array();

      //  $temp['aufruf'] = $aufruf;
      $temp['error'] = false;
      $temp['workoutname'] = $row['workoutname'];          
      $temp['duration'] = $row['duration'];          
      ...    

      array_push($response['workouts'],$temp);

  }
}

if($_SERVER['REQUEST_METHOD']=='GET') {
  $db = new DbOperation();
  $users = $db->getHighestRatingWith31Results();
  saveResultOfQueryToArray($users, $chooseMethod);
}
else {
    $response['error'] = true;
    $response['message'] = "Invalid Request";
}

echo json_encode($response);


有人能告诉我我做错了什么吗?有什么可以改变的吗?

lskq00tm

lskq00tm1#

看起来if($_SERVER['REQUEST_METHOD'] == 'GET')没有$response。除非有更多的代码,否则$response是未定义的。

if($_SERVER['REQUEST_METHOD']=='GET') {
  $db = new DbOperation();
  $users = $db->getHighestRatingWith31Results();
  saveResultOfQueryToArray($users, $chooseMethod);
  $response['error'] = false;
  $response['message'] = 'Whatever you want returned here.';
else {
    $response['error'] = true;
    $response['message'] = "Invalid Request";
}

echo json_encode($response);

字符串
我还建议查看HTTP响应,比如HTTP 405. https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/405
编辑:所以我看到你的更新,我很抱歉,但它引发了更多的问题。也就是说,$db->getHighestRatingWith31Results();做什么?函数saveResultOfQueryToArray()接受一个参数,但用法是给函数两个参数?saveResultOfQueryToArray调用mysqli_fetch_array(),它需要一个mysqli_result示例。
以下是我的建议:

  • 使用PDO而不是mysqli。我知道你说这是旧代码,但PDO是梦幻般的。https://www.php.net/manual/en/class.pdohttps://phpdelusions.net/pdo
  • 我可能不会让$response成为一个全局变量。我要么从saveResultOfQueryToArray()返回$response,要么通过引用传递。https://www.php.net/manual/en/language.references.pass.php
  • 再次,我建议查看HTTP响应代码。
  • 最后,我知道这很难,但是给你的变量命名一个更容易理解的名字,并用注解记录你的代码。计算机科学中有一个老笑话:“计算机科学中最难的两个部分是缓存失效,命名事物,以及一个错误。”“文档是你写给未来自己的情书。”- Damian Conway。

相关问题