0

the problem is that I have a remote js file to include in a page and it has to be included the last version of it. To be more specific: The file is for example: http://example.com/javascript/v03/header.js The problem is that when they generate a new javascript file the url will be changed to http://example.com/javascript/v04/header.js for example.

The URL will be changed to a new version number.

My question is what will be the best/clean way to detect a new version of that file in Php?

Thank you!

9
  • 1
    If possible, I would recommend doing it the other way around: Get a local copy of the file and only update when you see there is an update and it doesn't break anything on your side. Commented Apr 12, 2018 at 13:07
  • Why not use version control and have the same name be the most current version? Commented Apr 12, 2018 at 13:08
  • @kchason It seems the external file is not under the OP's control so that is most likely not an option. Commented Apr 12, 2018 at 13:10
  • @kchason you are right and maybe I can run a cron job daily to check if there is a new version like increment the existing version with 1 and see if the file exists. Commented Apr 12, 2018 at 13:18
  • 4
    When new versions are provided on new URLs it is usually because you shouldn't blindly use the latest version of it, but upgrade only after careful consideration and testing. Commented Apr 12, 2018 at 13:31

1 Answer 1

1

Untested but should work.
You file_get_contents the versions and when it returns false you have gone to far.

$link = "http://example.com/javascript/VERSION/header.js";

For($i=1; $i<100; $i++){
    $test= str_replace("VERSION", "v" . str_pad($i, 2, "0", STR_PAD_LEFT), $link);
    If(file_get_contents($test)){
        $latest = $test;
    }else{
        Break;
    }
}

Echo "latest version is " . $latest;

Then optionally you save the $i number somewhere and use that the next time you run it so that you don't need to recheck version 1-4 every time.

EDIT, noticed I had $str instead of $link.

Sign up to request clarification or add additional context in comments.

2 Comments

Just a note: you should never ever do such thing on page generation. It will slow down page rendering and downtime of http://example.com will kill you website too. If you really need this, do it in backround via cron or some queue.
@rob006 I think it can be used if used smart. Such as, you don't need to check it at every page load, once a week may be enough. If it has been checked this week can be saved in a textfile or database. Also I wouldn't want to run the js automatically, maybe make the page email me about the new version. This way it will not use much/any performance and example.com won't notice your pinging.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.