0

I am trying to see if JQuery CDN exists or not via PHP

essentially what Paul Irish did here but with PHP instead.

http://paulirish.com/2010/the-protocol-relative-url/

I am trying following but it's not working. Is this possible without http/https?

This is based on How can I check if a URL exists via PHP?

    $jquery_cur = '1.9.1'; // JQuery Version
    $jquery_cdn = '//code.jquery.com/jquery-'.$jquery_cur.'.min.js';
    $jquery_local = '/assets/js/libs/jquery-'.$jquery_cur.'.min.js';

    $jquery_ver = $jquery_cdn;  //Load the Jquery CDN version by default

    $cdn_headers = @get_headers($jquery_ver);
    if(strpos($cdn_headers[0], '404 Not Found')) {
        $jquery_ver = $jquery_cdn;
    }
    else {
        $jquery_ver = $jquery_local;
    }
Community
  • 1
  • 1
MonteCristo
  • 1,471
  • 2
  • 20
  • 41

1 Answers1

1

Hi check this solution you were check header without any protocol you need to add http or https to check files. test it with or without your internet connection.

$jquery_cur = '1.9.1'; // JQuery Version
        $jquery_cdn = '//code.jquery.com/jquery-'.$jquery_cur.'.min.js';
        $jquery_local = '/assets/js/libs/jquery-'.$jquery_cur.'.min.js';

        $jquery_ver = $jquery_cdn;  //Load the Jquery CDN version by default

        $jquery_url = ( $_SERVER['SERVER_PORT'] == 443 ? "https:" : "http:").$jquery_cdn;

        $test_url = @fopen($jquery_url,'r');

        if($test_url === False) {
        $jquery_ver = $jquery_local;
        }

        echo $jquery_ver;

with get_header

$headers = @implode('', @get_headers($jquery_url));
$test_url = (bool)preg_match('#^HTTP/.*\s+[(200|301|302)]+\s#i', $headers);

if($test_url === False) {
     $jquery_ver = $jquery_local;
 }

echo $jquery_ver;
umefarooq
  • 4,540
  • 1
  • 29
  • 38