php tips小集
来源:互联网 发布:空调品牌 知乎 编辑:程序博客网 时间:2024/05/16 05:52
在老外BLOG看到10个PHP的小TIPS,看上去不是很主流,但有的时候估计还很有用的,故笔记之
1 简单页面cache
<?php
// define the path and name of cached file
$cachefile = 'cached-files/'.date('M-d-Y').'.php';
// define how long we want to keep the file in seconds. I set mine to 5 hours.
$cachetime = 18000;
// Check if the cached file is still fresh. If it is, serve it up and exit.
if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) {
include($cachefile);
exit;
}
// if there is either no file OR the file to too old, render the page and capture the HTML.
ob_start();
?>
<html>
output all your html here.
</html>
<?php
// We're done! Save the cached content to a file
$fp = fopen($cachefile, 'w');
fwrite($fp, ob_get_contents());
fclose($fp);
// finally send browser output
ob_end_flush();
?>
2 知道两点的经度伟度计算距离
function distance($lat1, $lon1, $lat2, $lon2, $unit) {
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
$unit = strtoupper($unit);
if ($unit == "K") {
return ($miles * 1.609344);
} else if ($unit == "N") {
return ($miles * 0.8684);
} else {
return $miles;
}
}
3 比如有时象MP3等文件,要浏览器强制下载而不是用MP打开,可以这样
function downloadFile($file){
$file_name = $file;
$mime = 'application/force-download';
header('Pragma: public'); // required
header('Expires: 0');// no cache
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private',false);
header('Content-Type: '.$mime);
header('Content-Disposition: attachment; filename="'.basename($file_name).'"');
header('Content-Transfer-Encoding: binary');
header('Connection: close');
readfile($file_name);// push it out
exit();
}
4 使用GOOGLE API获得某个地址的天气预报
$xml = simplexml_load_file('http://www.google.com/ig/api?weather=ADDRESS');
$information = $xml->xpath("/xml_api_reply/weather/current_conditions/condition");
echo $information[0]->attributes();
5 获得某个网站的收藏夹图标
function get_favicon($url){
$url = str_replace("http://",'',$url);
return "http://www.google.com/s2/favicons?domain=".$url;
}
1 简单页面cache
<?php
// define the path and name of cached file
$cachefile = 'cached-files/'.date('M-d-Y').'.php';
// define how long we want to keep the file in seconds. I set mine to 5 hours.
$cachetime = 18000;
// Check if the cached file is still fresh. If it is, serve it up and exit.
if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) {
include($cachefile);
exit;
}
// if there is either no file OR the file to too old, render the page and capture the HTML.
ob_start();
?>
<html>
output all your html here.
</html>
<?php
// We're done! Save the cached content to a file
$fp = fopen($cachefile, 'w');
fwrite($fp, ob_get_contents());
fclose($fp);
// finally send browser output
ob_end_flush();
?>
2 知道两点的经度伟度计算距离
function distance($lat1, $lon1, $lat2, $lon2, $unit) {
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
$unit = strtoupper($unit);
if ($unit == "K") {
return ($miles * 1.609344);
} else if ($unit == "N") {
return ($miles * 0.8684);
} else {
return $miles;
}
}
3 比如有时象MP3等文件,要浏览器强制下载而不是用MP打开,可以这样
function downloadFile($file){
$file_name = $file;
$mime = 'application/force-download';
header('Pragma: public'); // required
header('Expires: 0');// no cache
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private',false);
header('Content-Type: '.$mime);
header('Content-Disposition: attachment; filename="'.basename($file_name).'"');
header('Content-Transfer-Encoding: binary');
header('Connection: close');
readfile($file_name);// push it out
exit();
}
4 使用GOOGLE API获得某个地址的天气预报
$xml = simplexml_load_file('http://www.google.com/ig/api?weather=ADDRESS');
$information = $xml->xpath("/xml_api_reply/weather/current_conditions/condition");
echo $information[0]->attributes();
5 获得某个网站的收藏夹图标
function get_favicon($url){
$url = str_replace("http://",'',$url);
return "http://www.google.com/s2/favicons?domain=".$url;
}
- php tips小集
- php tips
- PHP tips
- php tips
- PHP work tips
- 一些PHP Coding Tips
- 一些PHP Coding Tips
- PHP Coding Tips
- 一些PHP Coding Tips
- PHP开发tips
- Tips On PHP & MySQL
- php & TP tips
- php的tips
- PHP之学习Tips
- php小tips
- PHP日常实用小Tips
- PHP优化tips及方法
- PHP header函数用法 Tips
- (转)项目经理如何有效地进行项目沟通?
- Google新放出的几个管理mysql的工具
- (转)android Notification 的使用
- spring mvc3+hibernate+mysql的一个不错的CRUD例子
- IOS phonegap的Plugins开发
- php tips小集
- Activity.managedQuery()
- Android tips3
- Android-Universal-Image-Loader 属性设置
- php中的变量函数
- Android中的soundpool小结
- PHP框架YII笔记之1
- (转)用mysql的存储过程和event来做计划任务
- (转)mysql事件详细