Remotely Download Google AJAX Libraries Using PHP

By  on  

I don't know how to use all of the JavaScript libraries but perusing their code is interesting. If I'm looking to code something I'll look at how each of the other libraries accomplishes the task. The problem is that you need to go out and download each one. And of course they're all on different development schedules so you'd also need to make sure to grab the latest version of the library. Instead of manually accomplishing that task, I've chosen create a script that does all of that for me.

The PHP

//settings
$dir = 'js-libs/';
$url = 'http://code.google.com/apis/ajaxlibs/documentation/index.html';

//open file
$content = get_content($url);
echo 'Retrieved page from Google.';

//parse
$regex = '/http:\/\/ajax.googleapis.com\/ajax\/libs\/.*.js/isU';

//match?
preg_match_all($regex,$content,$matches);

//make sure there are no repeated files
$js_files = array_unique($matches[0]);

//download every file and save locally
foreach($js_files as $file) {
	//download
	$content = get_content($file);
	//save
	$filename = str_replace(array('http://ajax.googleapis.com/ajax/libs/','/'),array('','-'),$file);
	file_put_contents($dir.$filename,$content);
	//
	echo 'saving ',$file;
}

//function to grab content from a url
function get_content($url) {
	$ch = curl_init();
	curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
	curl_setopt($ch,CURLOPT_URL,$url);
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
	curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5);
	$content = curl_exec($ch);
	curl_close($ch);
	return $content;
}

Pow! One quick script to get you all of the popular JavaScript libraries in 10 seconds. You may not know how to use each library but it sure doesn't hurt to have them around.

Recent Features

  • By
    CSS 3D Folding Animation

    Google Plus provides loads of inspiration for front-end developers, especially when it comes to the CSS and JavaScript wonders they create. Last year I duplicated their incredible PhotoStack effect with both MooTools and pure CSS; this time I'm going to duplicate...

  • By
    LightFace:  Facebook Lightbox for MooTools

    One of the web components I've always loved has been Facebook's modal dialog.  This "lightbox" isn't like others:  no dark overlay, no obnoxious animating to size, and it doesn't try to do "too much."  With Facebook's dialog in mind, I've created LightFace:  a Facebook lightbox...

Incredible Demos

  • By
    Optimize Your Links For Print Using CSS — Show The URL

    When moving around from page to page in your trusty browser, you get the benefit of hovering over links and viewing the link's target URL in the status bar. When it comes to page printouts, however, this obviously isn't an option. Most website printouts...

  • By
    CSS Tooltips

    We all know that you can make shapes with CSS and a single HTML element, as I've covered in my CSS Triangles and CSS Circles posts.  Triangles and circles are fairly simply though, so as CSS advances, we need to stretch the boundaries...

Discussion

  1. I should note that I run this from the command line.

  2. Mohamed Jama

    Nice script David! did you redesign your site?! keep up the good work mate :)

  3. Nice reminder to think things through and do the extra mile to save time in the future.

    The url has changed in the last four years: https://developers.google.com/speed/libraries/devguide. Seems people at google don’t know how to put a permanent redirect into place…

    Regards,

    pepebe

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!