So I have a php file which generates json data based on different "params"
.php?params=foo,bar,blaa
(some sort of API?) I have checked it, and it returns valid json data. Mostly I'm using this file for jQuery/ajax -calls, but this time I want't this data to php array.
?param=comments returns all comments from database (because no extra params given like userid, time etc..). Different [GET]params make it to return different data. And yes - it works.
[{"type":"comment","commentid":"789006","userid":"235541","to_postid":"4003","content":"Hello","commented":"2014-07-01 13:54:54"}]
I return it like this:
// Output
header('Content-Type: application/json');
echo json_encode($result);
$result is array containing all data. This is how I push data (nothing wrong here?)
$result[] = array(
"type"=>"comment",
"commentid"=>$foo->commentid,
"userid"=>$foo->userid,
"to_postid"=>$foo->to_postid,
"content"=>$foo->content,
"commented"=>$foo->commented
);
In PHP I'm trying to use
json_decode(file_get_contents(HTTP_PATH."foo/myFile.php?params=comments"),true);
HTTP_PATH is just a defined variable for my localhost (using XAMPP) path
var_dump(file_get_content(...));
returns
string(2) "[]"
For some reason JSON is empty. Can't get it to php array. I have tried various things. Also checked whitespaces, file path, charset, error_log ... Also posted this problem to here (not in english): http://www.ohjelmointiputka.net/keskustelu/28188-json-php-array/sivu-1
json_decode(file_get_contents(HTTP_PATH."file/myFile.php?params=comments"),true); json_decode(file_get_contents(HTTP_PATH."file/myFile.php?params=comments"));
If I add $result[] = array("Hello!");
It will be displayed http://gyazo.com/81a96c8c5570e5fddacb47038bfa01ff
Hope u understood. What am I doing wrong??
JSON to PHP array
, as you are not getting any JSON. – Smuuf Jul 5 at 11:22string(2) "[]"
is simply empty json array. Withjson_decode()
it will turn into empty php array without any problem. If now your question is "why this is empty?" then you should look in your code or show some, because somewhere between $result array assignment andecho json_encode($result);
that$result
becomes empty. – shudder Jul 5 at 12:58