发送请求
function http($url, $params, $method = 'GET', $header = array(), $multi = false){
$opts = array(
CURLOPT_TIMEOUT => 30,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_HTTPHEADER => $header
);
switch(strtoupper($method)){
case 'GET':
$opts[CURLOPT_URL] = $url . '?' . http_build_query($params);
break;
case 'POST':
$params = $multi ? $params : http_build_query($params);
$opts[CURLOPT_URL] = $url;
$opts[CURLOPT_POST] = 1;
$opts[CURLOPT_POSTFIELDS] = $params;
break;
default:
throw new Exception('不支持的请求方式!');
}
$ch = curl_init();
curl_setopt_array($ch, $opts);
$data = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
if($error) throw new Exception('请求发生错误:' . $error);
return $data;
}
下载文件
function downloadFile( $fullPath ){
if( headers_sent() )
die('Headers Sent');
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');
if( file_exists($fullPath) ){
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
switch ($ext) {
case "pdf": $ctype="application/pdf"; break;
case "exe": $ctype="application/octet-stream"; break;
case "zip": $ctype="application/zip"; break;
case "doc": $ctype="application/msword"; break;
case "xls": $ctype="application/vnd.ms-excel"; break;
case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
case "gif": $ctype="image/gif"; break;
case "png": $ctype="image/png"; break;
case "jpeg":
case "jpg": $ctype="image/jpg"; break;
default: $ctype="application/force-download";
}
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Disposition: attachment; filename=".basename($fullPath).";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$fsize);
header("Content-type: $ctype; charset=gb2312");
ob_clean();
flush();
readfile( $fullPath );
} else
die('File Not Found');
}
时间比较函数,返回两个日期相差几秒、几分钟、几小时或几天
function DateDiff ( $date1 , $date2 , $unit = "" ) {
switch ( $unit ) {
case 's' :
$dividend = 1 ;
break;
case 'i' :
$dividend = 60 ;
break;
case 'h' :
$dividend = 3600 ;
break;
case 'd' :
$dividend = 86400 ;
break;
default:
$dividend = 86400 ;
}
$time1 = strtotime ( $date1 );
$time2 = strtotime ( $date2 );
if ( $time1 && $time2 )
return (float)( $time1 - $time2 ) / $dividend ;
return false ;
}
检查日期是否合法日期
function check_date ( $date ) {
$dateArr = explode ( "-" , $date );
if ( is_numeric ( $dateArr [ 0 ]) && is_numeric ( $dateArr [ 1 ]) && is_numeric ( $dateArr [ 2 ])) {
return checkdate ( $dateArr [ 1 ], $dateArr [ 2 ], $dateArr [ 0 ]);
}
return false ;
}
检查时间是否合法时间
function check_time ( $time ) {
$timeArr = explode ( ":" , $time );
if ( is_numeric ( $timeArr [ 0 ]) && is_numeric ( $timeArr [ 1 ]) && is_numeric ( $timeArr [ 2 ])) {
if (( $timeArr [ 0 ] >= 0 && $timeArr [ 0 ] <= 23 ) && ( $timeArr [ 1 ] >= 0 && $timeArr [ 1 ] <= 59 ) && ( $timeArr [ 2 ] >= 0 && $timeArr [ 2 ] <= 59 ))
return true ;
else
return false ;
}
return false ;
}
计算出给出的日期是星期几
function GetWeekDay ( $date ) {
$dateArr = explode ( "-" , $date );
return date ( "w" , mktime ( 0 , 0 , 0 , $dateArr [ 1 ], $dateArr [ 2 ], $dateArr [ 0 ]));
}
写入csv文件
function write_csv($row,$table_title,$file_name){
$fp = fopen($file_name, 'w');
fputcsv($fp,$table_title);
foreach($row as $key => $value)
{
$data = array();
array_push($data,$value['username']);
array_push($data,$value['subject']);
array_push($data,$value['money']);
array_push($data,$value['interface']);
array_push($data,$value['status']);
array_push($data,$value['deliver']);
fputcsv($fp,$data);
}
fclose($fp);
}