blog

select * from [vaibhav].[thought]

Remotely Download Google AJAX Libraries Using PHP

by vaibhav jain
vaibhav jain
I am a 26 year old Software Engineer in India. Founded vaibhav-jain.com with the
User is currently offline
on September 07 2012 in php / mysql 0 Comment

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.

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;
}

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.

Hits: 1127
0 vote

About the author

vaibhav jain

I am a 26 year old Software Engineer in India. Founded vaibhav-jain.com with the purpose to share my experiments and interesting development news.

Comments

No comments made yet. Be the first to submit a comment

Leave your comment

Guest
Guest Oct 23, 2014

vaibhav jain

I am a 26 year old Microsoft Certified Application Development Engineer in Indore, India. Core Developer for the ASP.NET, PHP, JAVA, MooTools JavaScript framework and .  More →

contact

Tired of you old fashioned website? No problem - I'm available for new web projects!

If you are interested in any of my services, feel free to ask for details!

Contact Me

about the blog

This blog is targeted toward all levels of software engineering. All web topics are discussed, including .NET, SQL SERVER, PHP, JAVA, CSS, JavaScript (MooTools, Dojo, and jQuery), HTML5 and ∞.

Subscribe Newsletter