PHP常用的方法
<?php /** * 对数据根据字符串进行asc加密 * @param array curlData 加密数组 * @param string rands 加密字符串 * @return string date 加密后的字符串 * @author lyx 2019-06-19 */ function ascEncrypt($curlData,$rands){ $screct_key = $rands; $str = trim(json_encode($curlData)); $str = addPKCS7Padding($str); $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_ECB),MCRYPT_RAND); $encrypt_str = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $screct_key, $str, MCRYPT_MODE_ECB, $iv); $date = base64_encode($encrypt_str); return $date; } /** * 获取随机字符串 * @return string rands 随机16位字符串 * @author lyx 2019-06-19 */ function haveRands(){ $str1 = ‘ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890‘; $randStr = str_shuffle($str1);//打乱字符串 $rands = substr($randStr, 0, 16); return $rands; } /** * 获取银行卡真实信息 * @param string bankCardNumber 银行卡号 * @return string cardType DC借记卡 CC信用卡 * @return string bank 银行编码 * @author lyx 2019-06-19 */ function getbankCardDetail($bankCardNumber) { $url = ‘https://ccdcapi.alipay.com/validateAndCacheCardInfo.json?_input_charset=utf-8&cardBinCheck=true&cardNo=‘ . $bankCardNumber; $curl = curl_init(); curl_setopt($curl, CURLOPT_RETURNTRANSFER,1); curl_setopt($curl, CURLOPT_FOLLOWLOCATION,1); curl_setopt($curl, CURLOPT_URL,$url); curl_setopt($curl, CURLOPT_TIMEOUT,3600); curl_setopt($curl, CURLOPT_USERAGENT,‘Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36‘); $ssl = substr($url, 0, 8) == "https://" ? TRUE : FALSE; if ($ssl) { curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); } $res = curl_exec($curl); curl_close($curl); $data = json_decode($res,true); return $data; } /** * 计算时间区间的所有日期 * @param string $start 开始时间 * @param string $end 结束时间 * @return array */ function prDates($start,$end){ $list = array(); $dt_start = strtotime($start); $dt_end = strtotime($end); while ($dt_start<=$dt_end){ $time = date(‘Y-m-d‘,$dt_start); array_push($list,$time); $dt_start = strtotime(‘+1 day‘,$dt_start); } return $list; } /** * 生成唯一不重复随机数 * @return string */ function randChar(){ $string = date(‘Ymd‘) . str_pad(mt_rand(1, 99999), 5, ‘0‘, STR_PAD_LEFT). str_pad(mt_rand(1, 9999), 4, ‘0‘, STR_PAD_LEFT). str_pad(mt_rand(1, 99), 2, ‘0‘, STR_PAD_LEFT); return $string; } /** * 密码加密 * @param string $password 密码 * @return string */ function password($password) { $password = $password.‘6pTVUmY34BBBJwxmrEOBz7Gj877IGKBK‘; $string = base64_encode(sha1(md5($password))); $string = substr($string, 0, 8); $password = md5(hash("sha256", $password . $string)); return $password; } /** * curl获取请求文本内容 * @param string $url URL请求地址 * @param string $method 请求方式 POST GET * @param string $data 请求数据 * @return array */ function https_request($url, $method =‘GET‘, $data = array()) { if ($method == ‘POST‘) { //使用crul模拟 $ch = curl_init(); //禁用https curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); //允许请求以文件流的形式返回 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); curl_setopt($ch, CURLOPT_DNS_CACHE_TIMEOUT, 30); curl_setopt($ch, CURLOPT_URL, $url); $result = curl_exec($ch); //执行发送 curl_close($ch); }else { if (ini_get(‘allow_fopen_url‘) == ‘1‘) { $result = file_get_contents($url); }else { //使用crul模拟 $ch = curl_init(); //允许请求以文件流的形式返回 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); //禁用https curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_URL, $url); $result = curl_exec($ch); //执行发送 curl_close($ch); } } return $result; }
/**
* 过滤掉Emoji表情文字
*/
function filterEmoji($str)
{
$str = preg_replace_callback(
‘/./u‘,
function (array $match) {
return strlen($match[0]) >= 4 ? ‘‘ : $match[0];
},
$str);
return $str;
}
/**
* 分类转树型数组
* @param $data
* @param string $child
* @return array
*/
function cats_to_tree($data, $child = ‘list‘, $id = ‘id‘, $pid = ‘parent_id‘, $pval = 0)
{
$tree = [];
$keys = array_column($data, $id);
foreach ($data as $k => $ro) {
$key = $ro[$pid] != $pval ? array_search($ro[$pid], $keys) : -1;
if ($key >= 0) {
$data[$key][$child][] = &$data[$k];
} if ($ro[$pid] == $pval) {
$tree[] = &$data[$k];
}
}
return $tree;
}
/**
* 分类数组 id=>name
* @param $data
* @return array
*/
function cats_key_all($list, $key = ‘id‘, $val = ‘name‘)
{
$data = [];
foreach ($list as $ro) {
$data[$ro[$key]] = $ro[$val];
}
return $data;
}
/**
* 时间字符串
* @param $time
* @return string
*/
function date_ago($time)
{
$t = time() - $time;
$f = array(
‘31536000‘ => ‘年‘,
‘2592000‘ => ‘个月‘,
‘604800‘ => ‘星期‘,
‘86400‘ => ‘天‘,
‘3600‘ => ‘小时‘,
‘60‘ => ‘分钟‘,
‘1‘ => ‘秒‘
);
foreach ($f as $k => $v) {
if (0 != $c = floor($t / (int)$k)) {
return $c . $v . ‘前‘;
}
}
}
/**
* 判断是否SSL协议
* @return boolean
*/
function is_ssl()
{
if (isset($_SERVER[‘HTTPS‘]) && (‘1‘ == $_SERVER[‘HTTPS‘] || ‘on‘ == strtolower($_SERVER[‘HTTPS‘]))) {
return true;
} if (isset($_SERVER[‘SERVER_PORT‘]) && (‘443‘ == $_SERVER[‘SERVER_PORT‘])) {
return true;
}
return false;
}
/**
* 简化接口返回JSON
* @return json
*/
function api_json($code, $msg = ‘‘, $data = false, $url =null,$count = null)
{
//echo json_encode(22);exit;
$return[‘code‘] = $code;
$return[‘msg‘] = $msg;
$url !== null && $return[‘domain‘] = $url;
$data !== false && $return[‘data‘] = $data;
$count !== null && $return[‘count‘] = $count;
return json($return);
}
/**
* php打印控制台日志
* @param $data
* @echo log
*/
function console_log($data)
{
if (is_array($data) || is_object($data)) {
echo("<script>console.log(‘" . json_encode($data) . "‘);</script>");
} {
echo("<script>console.log(‘" . $data . "‘);</script>");
}
}
//上传图片
function upload($file)
{
// 给定一个目录
$info = $file->move(‘public/upload‘);
return ‘/‘ . $info->getPathname();
}
原文:https://www.cnblogs.com/ccw869476711/p/12853501.html