this is my (json.php) file which i try to get through file_get_contents('url/file/json.php') and try to decode it.
$query = "select * from tbl-item";
$stmt = $conn->query($query);
$json_array= array();
while($row= $stmt->fetch(PDO::FETCH_ASSOC)){
$json_array[]=$row
echo json_encode($json_array);
file_put_contents('my_file_json.json', json_encode($json_array));
What I have tried:
$json = file_get_contents('url/file/json.php');
$data = json_decode($json, true);
foreach ($data as $key => $value)
The
file_get_contents
[
^
] method reads the contents of the specified file. It does
not
execute the code stored in that file.
Your file contents are PHP script, not JSON.
If you want to execute the PHP script and load the result, you will need to specify the complete URL to the script. You cannot use a relative URL, since that will be interpreted as a file path.
$json
= file_get_contents(
'
https://www.yoursite.local/url/file/json.php'
);
If that still doesn't work, then you need to debug your code to examine the contents of the
$json
variable.
Read the question carefully.
Understand that English isn't everyone's first language so be lenient of bad
spelling and grammar.
If a question is poorly phrased then either ask for clarification, ignore it, or
edit the question
and fix the problem. Insults are not welcome.
Don't tell someone to read the manual. Chances are they have and don't get it.
Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.