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?