首頁  >  問答  >  主體

為什麼我的 PHP API 停止運作並且不再回傳 json?

幾年前,我為我的一些行動應用程式創建了一個後端(PHP 來獲取一些 json 數據)。從那時起我就沒有碰過這段程式碼。現在它幾週前就停止工作了。我不是後端開發人員,所以我在這裡沒有太多經驗,但幾年前我認為創建自己的後端而不是使用Firebase/Serverless 會更好......這不是我最好的主意: )

我嘗試了什麼:

<!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.addEventLis tener;}catch(e){return !!0;} },
            y=function(y,z){x() ? document.addEventLis tener("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);

有人可以跟我解釋一下我做錯了什麼/可以改變什麼嗎?

P粉447785031P粉447785031282 天前555

全部回覆(1)我來回復

  • P粉821231319

    P粉8212313192023-12-17 09:11:42

    看起來 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 回應代碼。
    • 最後,我知道這很難,但是將您的變數命名為更容易理解的名稱,並用註釋記錄您的程式碼。計算機科學中有一個老笑話:“計算機科學中最困難的兩個部分是緩存失效、命名事物和相差一錯誤。” 「文件是一封寫給未來的自己的情書。」- 達米安康威
    #

    回覆
    0
  • 取消回覆