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

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)
     One moment, please...  

Please wait while your request is being verified...

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

Looksif($_SERVER['REQUEST_METHOD'] == 'GET')There is no$response. Unless there is more in the code,$responseis 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? FunctionsaveResultOfQueryToArray()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.pdoandhttps://phpdelusions.net/pdo
  • I probably wouldn't set $response as a global variable. I would either return the $response fromsaveResultOfQueryToArray()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!