<?php /** 公用函数 */ //导出数据 //导出数据 function exportExcel($expTitle, $expCellName, $expTableData) { $xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称 $fileName = $expTitle.date('_YmdHis');//or $xlsTitle 文件名称可根据自己情况设定 $cellNum = count($expCellName); $dataNum = count($expTableData); // vendor("PHPExcel.PHPExcel"); $objPHPExcel = new PHPExcel(); include_once(__DIR__."/../../vendor/PHPExcel/PHPExcel/IOFactory.php"); set_time_limit(0);//不对php(主要是写数据)执行时间做限制 ini_set("memory_limit", "1024M");//设置内存(防爆内存) $cacheMethod = \PHPExcel_CachedObjectStorageFactory::cache_in_memory_serialized;//设置缓存策略(减少内存占用) //判断缓存策略是否可用 if (!\PHPExcel_Settings::setCacheStorageMethod($cacheMethod)) { die($cacheMethod . " 缓存方法不可用" . EOL); } $cellName = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ'); //显示导出名称和导出时间 // $objPHPExcel->getActiveSheet(0)->mergeCells('A1:'.$cellName[$cellNum-1].'1');//合并单元格 // $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $expTitle.' 导出时间:'.date('Y-m-d H:i:s')); for($i=0;$i<$cellNum;$i++){ $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i].'1', $expCellName[$i][1]); } // Miscellaneous glyphs, UTF-8 for($i=0;$i<$dataNum;$i++){ for($j=0;$j<$cellNum;$j++){ $objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j].($i+2), $expTableData[$i][$expCellName[$j][0]]); } } header('pragma:public'); header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$xlsTitle.'.csv"'); header("Content-Disposition:attachment;filename=$fileName.csv");//attachment新窗口打印inline本窗口打印 //输出bom print(chr(0xEF).chr(0xBB).chr(0xBF)); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV'); $objWriter->save('php://output'); exit; } // curl function curlApi( $url , $params = array(), $method = 'GET' , $multi = false, $extheaders = array()) { if(!function_exists('curl_init')) exit('Need to open the curl extension'); $method = strtoupper($method); $ci = curl_init(); curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 3); curl_setopt($ci, CURLOPT_TIMEOUT, 3); curl_setopt($ci, CURLOPT_RETURNTRANSFER, true); curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ci, CURLOPT_HEADER, false); $headers = (array)$extheaders; switch ($method) { case 'POST': curl_setopt($ci, CURLOPT_POST, TRUE); if (!empty($params)) { if($multi) { foreach($multi as $key => $file) { $params[$key] = '@' . $file; } curl_setopt($ci, CURLOPT_POSTFIELDS, $params); $headers[] = 'Expect: '; } else { curl_setopt($ci, CURLOPT_POSTFIELDS, http_build_query($params)); } } break; case 'DELETE': case 'GET': $method == 'DELETE' && curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE'); if (!empty($params)) { $url = $url . (strpos($url, '?') ? '&' : '?') . (is_array($params) ? http_build_query($params) : $params); } break; } curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE ); curl_setopt($ci, CURLOPT_URL, $url); if($headers) { curl_setopt($ci, CURLOPT_HTTPHEADER, $headers ); } $response = curl_exec($ci); curl_close ($ci); return $response; } /** * webpower CURL 请求 * @param $url * @param $content * @param string $method * @return mixed */ function webpower_curl($url, $content, $method= 'post'){ //username and password $username = "xb@ichunt.com"; //短信平台的账号 $password = "Webp0wer"; //短信平台的密码 $data_string = json_encode($content); $header = array( "Content-Type: application/json", "X-HTTP-Method-Override: $method", "Authorization: Basic " . base64_encode($username . ":" . $password) ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_FAILONERROR, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); curl_setopt($ch,CURLOPT_HTTPHEADER, $header); $response = curl_exec($ch); return $response ? $response : curl_error($ch); } /** * 接口服务时加密方式 * @param [type] $data [description] * @param [type] $timestamp [description] * @param [type] $key [description] * @return [type] [description] */ function service_token($data, $timestamp, $key = null) { $key = is_null($key) ? Config('website.SERVICE_KEY') : $key; $token = md5($data.$timestamp.$key); return $token; } /** * 获取客户端IP地址 * @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字 * @param boolean $adv 是否进行高级模式获取(有可能被伪装) * @return mixed */ function get_client_ip($type = 0, $adv = false) { $type = $type ? 1 : 0; static $ip = null; if (null !== $ip) { return $ip[$type]; } if ($adv) { if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']); $pos = array_search('unknown', $arr); if (false !== $pos) { unset($arr[$pos]); } $ip = trim($arr[0]); } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) { $ip = $_SERVER['HTTP_CLIENT_IP']; } elseif (isset($_SERVER['REMOTE_ADDR'])) { $ip = $_SERVER['REMOTE_ADDR']; } } elseif (isset($_SERVER['REMOTE_ADDR'])) { $ip = $_SERVER['REMOTE_ADDR']; } // IP地址合法验证 $long = sprintf("%u", ip2long($ip)); $ip = $long ? array($ip, $long) : array('0.0.0.0', 0); return $ip[$type]; }