Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

157
Views
Get JSON with PHP from URL and hand it over to JS

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!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!