4、强制下载文件
一些诸如 mp3 类型的文件,通常会在客户端浏览器中直接被播放或使用。如果你希望它们强制被下载,也没问题。可以使用以下代码:
- 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();
- }
点击这里查看详细情况:http://www.tecnocrazia.com/
5、使用 Google API 获取当前天气信息
想知道今天的天气?这段代码会告诉你,只需 3 行代码。你只需要把其中的 ADDRESS 换成你期望的城市。
- $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();
点击这里查看详细情况:http://ortanotes.tumblr.com/post/200469319/current-weather-in-3-lines-of-php
6、获得某个地址的经纬度
随着 Google Maps API 的普及,开发人员常常需要获得某一特定地点的经度和纬度。这个非常有用的函数以某一地址作为参数,返回一个数组,包含经度和纬度数据。
- function getLatLong($address){
- if (!is_string($address))die("All Addresses must be passed as a string");
- $_url = sprintf('http://maps.google.com/maps?output=js&q=%s',rawurlencode($address));
- $_result = false;
- if($_result = file_get_contents($_url)) {
- if(strpos($_result,'errortips') > 1 || strpos($_result,'Did you mean:') !== false) return false;
- preg_match('!center:\s*{lat:\s*(-?\d+\.\d+),lng:\s*(-?\d+\.\d+)}!U', $_result, $_match);
- $_coords['lat'] = $_match[1];
- $_coords['long'] = $_match[2];
- }
- return $_coords;
- }
点击这里查看详细情况:http://snipplr.com/view.php?codeview&id=47806
7、使用 PHP 和 Google 获取域名的 favicon 图标
有些网站或 Web 应用程序需要使用来自其他网站的 favicon 图标。利用 Google 和 PHP 很容易就能搞定,不过前提是 Google 不会连接被重置哦!
- function get_favicon($url){
- $url = str_replace("http://",'',$url);
- return "http://www.google.com/s2/favicons?domain=".$url;
- }
点击这里查看详细情况:http://snipplr.com/view.php?codeview&id=45928
原文:http://www.mangguo.org/7-super-useful-php-snippets/
PHP之友评论