'PHP sends an empty Response Payload instead of JSON

Sorry if I'm not good at formulating questions or if this isn't a good question, but I've been trying for days and getting no progress on this and I'm ready to effing cry. Please, please help.

So this PHP file:

<?php

 header("Content-Type: application/json; charset=UTF-8"); 
 
 $obj = json_decode($_GET["x"], true);
 $conn = new mysqli("localhost", "user", "password", "database");
 $i = 1;
 $message = array();
 
 while ($i < 31){
     
     $query = $conn->prepare("SELECT dag, maand, jaar, timeslot FROM boeker WHERE dag = $i AND maand = ? AND jaar = ?");
     $query->bind_param('ii', $obj["monthQ"], $obj["yearQ"]);
     $query->execute();
     $result = $query->get_result();
     $outp = $result->fetch_all(MYSQLI_ASSOC);
     $message = array();     
     
     if(empty($result)){
         //available
     } else if ($result["timeslot"] == null){
         array_push($message, "occupied", $i);
     } else {
         array_push($message, $result["timeslot"], $i);
     }
     $i++;
 }
 
 $package = json_encode($message);
 echo $package;
 
?>

is getting this request:

https://url/file.php?x='{"monthQ":10,"yearQ":2021}'

and the reply is an empty Response Payload, which throws an error, because it can't be JSON.parsed

I would think that $message, even when the array stays empty, would make $package a simple [], which should parse...

Is something wrong with my PHP?

Thanks for any help!



Solution 1:[1]

You need to remove the single quotes around the json string in your url. If you have no control over constructing that url, you can just run:

$obj = json_decode(trim($_GET['x'],"'"), true);

I've tested that, and it works.

If you do have control over constructing that url, I recommend that you remove the single quotes AND urlencode the json block.

Solution 2:[2]

Leaving breadcrumbs after solving a similar problem here. My php.ini file had a max_post_size set to 2K and I was sending a POST request with size >2K. PHP decided to sweep the problem under the rug and proceed with the request with an empty body.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 John Morton
Solution 2 CaseInPoint