Why did my PHP API stop working and no longer return json?
P粉447785031
P粉447785031 2023-12-16 09:46:32
0
1
550

A few years ago I created a backend (PHP to get some json data) for some mobile apps of mine. I haven't touched this code since then. Now it stopped working weeks ago. I'm not a backend developer so I don't have much experience here, but a few years ago I thought it would be better to create my own backend instead of using Firebase/Serverless... which wasn't my best idea: )

What I tried:

  • Check the url using Chrome --> everything works fine (I can see my json data)
  • Check inside the application or in 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.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>

This is my php file:

$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);

Can someone explain to me what I'm doing wrong/what can be changed?

P粉447785031
P粉447785031

reply all(1)
P粉821231319

Looks if($_SERVER['REQUEST_METHOD'] == 'GET') There is no $response. Unless there is more in the code, $response is undefined.

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);

Something like this should do the trick! I also recommend looking at the HTTP response, such as HTTP 405. https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/405

Edit: I saw your update and I'm sorry, but it raises more questions. In other words, what does $db->getHighestRatingWith31Results(); do? Function saveResultOfQueryToArray() accepts one parameter, but the usage is to provide two parameters to the function? saveResultOfQueryToArray is calling mysqli_fetch_array(), which requires a mysqli_result instance.

This is my suggestion:

  • Use PDO instead of mysqli. I know you said this was old code, but PDO is awesome. https://www.php.net/manual/en/class.pdo and https://phpdelusions.net/pdo
  • I probably wouldn't set $response as a global variable. I would either return the $response from saveResultOfQueryToArray() or consider passing it by reference. https://www.php.net/manual/en/language. references.pass.php
  • Once again I recommend you look into HTTP response codes.
  • Finally, I know this is hard, but name your variables something more understandable and document your code with comments. There's an old joke in computer science: "The two hardest parts of computer science are cache invalidation, naming things, and one-by-one errors." "A document is a love letter to your future self." - Damien Conway
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!