A single function for CDN-hosted scripts in WordPress

I have generally run across two methods, depending on server configuration, of remotely checking the availability of a CDN-hosted script using PHP. One is cURL, the other is fopen. I’ve combined the two functions I use in their respective cases like so:

function use_cdn(){
   $url = 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js'; // the URL to check against
   $ret = false;
   if(function_exists('curl_init')) {
      $curl = curl_init($url);
      curl_setopt($curl, CURLOPT_NOBODY, true);
      $result = curl_exec($curl);
      if (($result !== false) && (curl_getinfo($curl, CURLINFO_HTTP_CODE) == 200)) $ret = true;   
      curl_close($curl);
   }
   else {
      $ret = @fopen($url,'r');
   }
   if($ret) {
      wp_deregister_script('jquery'); // deregisters the default WordPress jQuery
      wp_register_script('jquery', $url); // register the external file
      wp_enqueue_script('jquery'); // enqueue the external file
   }
   else {
      wp_enqueue_script('jquery'); // enqueue the local file
   }
}

…but I’m not looking to reinvent the wheel. Is this a good, solid technique or can anyone offer pointers as to how to simplify/streamline the process?

Related posts

Leave a Reply

1 comment

  1. Using get_headers() we can issue a HEAD request and check the response code to see if the file is available, and also will allow us to see if network or DNS is down since it will cause get_headers() to fail (keep the @ sign to suppress PHP error if domain is not resolvable, which will cause it to return FALSE in that case and thus load local file:

    function use_cdn()
    {
        $url = 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js'; // the URL to check against
        $online = FALSE;
    
        if(function_exists('curl_init')) 
        {
            $curl = curl_init($url);
            curl_setopt($curl, CURLOPT_NOBODY, TRUE);
            $result = curl_exec($curl);
            if ((FALSE !== $result) && ('200' == curl_getinfo($curl, CURLINFO_HTTP_CODE)))
            {
                $online = TRUE;
            }
            curl_close($curl);
        } 
        else if (ini_get('allow_url_fopen'))
        {
            stream_context_set_default(array('http' => array('method' => 'HEAD'))); // set as HEAD request
            $headers = @get_headers($url, 1); // get HTTP response headers
            if ($headers && FALSE !== strpos($headers[0], '200')) // if get_headers() passed and 200 OK
            {
                $online = TRUE;
            }
        }
    
        if ($online)
        {
            wp_deregister_script('jquery'); // deregisters the default WordPress jQuery
            wp_register_script('jquery', $url); // register the external file
        }
        wp_enqueue_script('jquery'); // enqueue registered files
    }
    

    get_headers() would be faster as it is a build in function, opposed to having to load a PECL extension such as cURL. As for fopen() well the task you need to do is check the response headers, get_headers()’s only use is to do just that, fopen() can’t get headers, and cURL has other uses not to mention the unnecessary overhead and don’t specialize in getting headers so it would be the most suitable choice to use in this case.