PHP头条
热点:

4、强制下载文件

一些诸如 mp3 类型的文件,通常会在客户端浏览器中直接被播放或使用。如果你希望它们强制被下载,也没问题。可以使用以下代码:

  1. function downloadFile($file){  
  2.         $file_name = $file;  
  3.         $mime = 'application/force-download';  
  4.     header('Pragma: public');     // required  
  5.     header('Expires: 0');        // no cache  
  6.     header('Cache-Control: must-revalidate, post-check=0, pre-check=0');  
  7.     header('Cache-Control: private',false);  
  8.     header('Content-Type: '.$mime);  
  9.     header('Content-Disposition: attachment; filename="'.basename($file_name).'"');  
  10.     header('Content-Transfer-Encoding: binary');  
  11.     header('Connection: close');  
  12.     readfile($file_name);        // push it out  
  13.     exit();  

点击这里查看详细情况:http://www.tecnocrazia.com/

5、使用 Google API 获取当前天气信息

想知道今天的天气?这段代码会告诉你,只需 3 行代码。你只需要把其中的 ADDRESS 换成你期望的城市。

  1. $xml = simplexml_load_file('http://www.google.com/ig/api?weather=ADDRESS');  
  2.   $information = $xml->xpath("/xml_api_reply/weather/current_conditions/condition");  
  3.   echo $information[0]->attributes(); 

点击这里查看详细情况:http://ortanotes.tumblr.com/post/200469319/current-weather-in-3-lines-of-php

6、获得某个地址的经纬度

随着 Google Maps API 的普及,开发人员常常需要获得某一特定地点的经度和纬度。这个非常有用的函数以某一地址作为参数,返回一个数组,包含经度和纬度数据。

  1. function getLatLong($address){  
  2.     if (!is_string($address))die("All Addresses must be passed as a string");  
  3.     $_url = sprintf('http://maps.google.com/maps?output=js&q=%s',rawurlencode($address));  
  4.     $_result = false;  
  5.     if($_result = file_get_contents($_url)) {  
  6.         if(strpos($_result,'errortips') > 1 || strpos($_result,'Did you mean:') !== false) return false;  
  7.         preg_match('!center:\s*{lat:\s*(-?\d+\.\d+),lng:\s*(-?\d+\.\d+)}!U'$_result$_match);  
  8.         $_coords['lat'] = $_match[1];  
  9.         $_coords['long'] = $_match[2];  
  10.     }  
  11.     return $_coords;  

点击这里查看详细情况:http://snipplr.com/view.php?codeview&id=47806

7、使用 PHP 和 Google 获取域名的 favicon 图标

有些网站或 Web 应用程序需要使用来自其他网站的 favicon 图标。利用 Google 和 PHP 很容易就能搞定,不过前提是 Google 不会连接被重置哦!

  1. function get_favicon($url){  
  2. $url = str_replace("http://",'',$url);  
  3. return "http://www.google.com/s2/favicons?domain=".$url;  
  4. }  
  5.  

点击这里查看详细情况:http://snipplr.com/view.php?codeview&id=45928

原文:http://www.mangguo.org/7-super-useful-php-snippets/


www.phpzy.comtrue/php/7146.htmlTechArticle4、强制下载文件 一些诸如 mp3 类型的文件,通常会在客户端浏览器中直接被播放或使用。如果你希望它们强制被下载,也没问题。可以使用以下代码:...

相关文章

相关频道:

PHP之友评论

今天推荐