开放的编程资料库

当前位置:我爱分享网 > PHP教程 > 正文

使用 PHP 远程下载 Google AJAX 库

我不知道如何使用所有的 JavaScript 库,但细读它们的代码很有趣。如果我想编写一些代码,我会看看其他每个库是如何完成任务的。问题是你需要出去下载每一个。当然,它们都处于不同的开发计划中,因此您还需要确保获取最新版本的库。我没有手动完成该任务,而是选择创建一个脚本来为我完成所有这些工作。

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

噗!一个快速脚本,让您在 10 秒内获得所有流行的 JavaScript 库。您可能不知道如何使用每个库,但拥有它们肯定不会有什么坏处。

未经允许不得转载:我爱分享网 » 使用 PHP 远程下载 Google AJAX 库

感觉很棒!可以赞赏支持我哟~

赞(0) 打赏