Get JSON with PHP from URL and hand it over to JS [closed]

i'm stuck at a problem. What i wanna do is to get a JSON file from a URL by using PHP (to overcome CORS issue when using AJAX) and then using JSON.parse to make it usable by JavaScript to make a list out of the JSON content.

I tried this code before but it isn't working.

<!DOCTYPE html>
<html>
<head>
    <title>Beispiel</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
    <?php
        $url = "http://geoweb.zamg.ac.at/static/event/lastday.json";
        ?>

        <script>
            var array = JSON.parse(<?php file_get_contents($url) ?>);
            console.log(array);
        </script>
</body>
</html>

Maybe someone of you can help me with that problem. I'm not really good at programming yet ;)

Many thanks in advance!


Solution 1:

You don't need to JSON.parse(), because the JSON is already parsed from the file source.

You also missed to echo the return of file_get_contents().

var array = <?php echo file_get_contents($url) ?>;
console.log(array);