亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

為什麼我的 PHP API 停止運作並且不再回傳 json?
P粉447785031
P粉447785031 2023-12-16 09:46:32
0
1
778

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

我嘗試了什麼:

  • 使用 Chrome 檢查網(wǎng)址 --> 一切正常(我可以看到我的 json 資料)
  • 在應(yīng)用程式內(nèi)部或 Postman 中檢查:(標(biāo)頭: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.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粉447785031
P粉447785031

全部回覆(1)
P粉821231319

看起來 if($_SERVER['REQUEST_METHOD'] == 'GET') 沒有 $response。除非程式碼中有更多內(nèi)容,否則 $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);

類似的東西應(yīng)該??可以解決問題!我還建議查看 HTTP 回應(yīng),例如 HTTP 405。 https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/405

#編輯:我看到了您的更新,很抱歉,但這引發(fā)了更多問題。也就是說,$db->getHighestRatingWith31Results(); 是做什麼的?函數(shù) saveResultOfQueryToArray() 接受一個參數(shù),但用法是給函數(shù)兩個參數(shù)? saveResultOfQueryToArray 正在呼叫 mysqli_fetch_array(),它需要一個 mysqli_result 實例。

這是我的建議:

  • 使用 PDO 而不是 mysqli。我知道您說過這是舊代碼,但 PDO 非常棒。 https://www.php.net/manual/en/class.pdohttps://phpdelusions.net/pdo
  • 我可能不會將 $response 設(shè)定為全域變數(shù)。我要么從 saveResultOfQueryToArray() 返回 $response,要么考慮透過引用傳遞。 https://www.php.net/manual/en/language。 references.pass.php
  • #我再次建議您研究一下 HTTP 回應(yīng)代碼。
  • 最後,我知道這很難,但是將您的變數(shù)命名為更容易理解的名稱,並用註釋記錄您的程式碼。計算機科學(xué)中有一個老笑話:“計算機科學(xué)中最困難的兩個部分是緩存失效、命名事物和相差一錯誤?!?「文件是一封寫給未來的自己的情書?!? 達米安康威
#
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板