-1

I created a webpage a while ago with php(+ a webAPI) and I want to rewrite it to javascript now.

The problem:
The site loads a lot of data and needs about 5-7 sec to fully load, which is not optimal. I want to use javascript to 'build' the side part by part. With php (after clicking 'load') nothing (for the user visible) happens for 5-7 secs and the browser creates the whole page at once. I want the user to see how the page is built, like first block first, then second block, not the whole page at once. I've read that this should be possible with javascript and now I want to rewrite the page.

I'm currently using php to get the information like this:

$feedraw = file_get_contents('https://' . $region . '.api.battle.net/wow/character/' . $server . '/' . $charname . '?fields=feed&locale=en_GB&apikey=' . $api_key . ');  
$feedarray = json_decode($feedraw);

After feeding my $feedarray with data, I access the data via (example)

foreach ($feedarray as $feed) {
            if ($feed->type === "ACHIEVEMENT") {
                echo $feed->title;
        }
}

Or is it even possible to load the site partially with php?

Thanks in advance

neverlucky
  • 35
  • 2
  • 10
  • You really don't want to send something secret like an API key in client-side JavaScript. – ceejayoz Feb 19 '18 at 15:05
  • too broad. there are too many variables and too many possible solutions. learn how to do basic ajax and if the cors headers is set you could *possibly* do it entirely on the client side.... read up on ajax and come back after you've made an attempt and have a specific problem. – I wrestled a bear once. Feb 19 '18 at 15:10
  • @ceejayoz: Wouldn't it be possible to get the API data via php, store it locally and then let javascript do the work? – neverlucky Feb 20 '18 at 11:37

1 Answers1

0

The answer to your question is basically “yes, learn JavaScript”.

However, I would possibly suggest that if you want to stick to your existing site, maybe consider saving the requests so that each time the page loads, it doesn’t have to fetch them all. Then using a cron job to fetch the pages every hour (or something) in the background.

See something like: Simple PHP caching

Zoe Edwards
  • 12,999
  • 3
  • 24
  • 43