本文主要和大家分享php一些工具函数介绍,这些都是比较常用的工具函数,希望能帮助到大家。
1.获得毫秒级别的时间戳
public static function get_millisecond()
{
//获取毫秒的时间戳
$time = explode( , microtime()); $time = $time[1] . substr($time[0], 2, 3); return $time;
}
2读取文件内容
public static function file_read($file_path)
{
if (file_exists($file_path)) { $fp = fopen($file_path, r); $str = fread($fp, filesize($file_path));//指定读取大小,这里把整个文件内容读取出来
$str = str_replace(\r\n, <br />, $str);
fclose($fp); return $str;
} else { return false;
}
}
3产生随机字符串,不长于32位
public static function get_nonce_number($length = 11)
{
$chars = 0123456789; $str = ; for ($i = 0; $i < $length; $i++) { $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
} return $str;
}
4.生成订单详情号 默认为16位
public static function get_order_id($length = 16)
{
//见方法一,获得毫秒级别时间戳
$time = self::get_millisecond(); $len = $length - 13; $str = self::get_nonce_number($len); if (strlen($time) != 13) { $orderid = $str . $time . rand(1, 9);
} else { $orderid = $str . $time;
} return $orderid;
}
5.随机产生字母和数字(有重复的可能性)
public static function randomkeys($length)
{
$returnstr = ''; $pattern = '1234567890abcdefghijklmnopqrstuvwxyzabcdefghijklomnopqrstuvwxyz'; for ($i = 0; $i < $length; $i++) { $returnstr .= $pattern{mt_rand(0, 61)}; //生成php随机数
} return $returnstr;
}
6.生成6位数验证码
public static function random_code($length = 6)
{
$code = rand(pow(10, ($length - 1)), pow(10, $length) - 1); return "$code";
}
7.防sql注入,xss攻击
public static function clean($str)
{
$str = trim($str); $str = strip_tags($str); $str = stripslashes($str); $str = addslashes($str); $str = rawurldecode($str); $str = quotemeta($str); $str = htmlspecialchars($str); return $str;
}
8.接收post或者get参数
public static function get_input($name)
{
$request = yii::$app->request; if (isset($_get[$name])) { $value = $request->get($name);
} elseif (isset($_post[$name])) { $value = $request->post($name);
} else { $value = '';
} return $value;
}
9.加密方法
public static function aes_encrypt($str)
{
$privatekey = 私钥文件字符串; $iv = 公钥文件字符串; //加密
$encrypted = mcrypt_encrypt(mcrypt_rijndael_128, $privatekey, $str, mcrypt_mode_cbc, $iv);// return urlencode(base64_encode($encrypted));
return base64_encode($encrypted);
}
10.解密方法
public static function aes_decrypt($str)
{
$privatekey = 私钥文件字符串; $iv = 公钥文件字符串; $encrypteddata = base64_decode($str); $decrypted = mcrypt_decrypt(mcrypt_rijndael_128, $privatekey, $encrypteddata, mcrypt_mode_cbc, $iv); $decrypted = rtrim($decrypted, \0); return $decrypted;
}
11.curl模拟带header头信息的get请求
public static function curl_get($request_url,$session=[])
{
if($session){ $ch = curl_init(); $header = [session:.$session]; //设置一个你的浏览器agent的header
curl_setopt($ch, curlopt_httpheader, $header);
curl_setopt($ch, curlopt_header, 0); //返回response头部信息
curl_setopt($ch, curlopt_url, $request_url);
curl_setopt($ch, curlopt_returntransfer, 1);//不在页面输出 返回数据
$data_json=curl_exec($ch);
curl_close($ch);
}else{ $curl = new curl(); $data_json = $curl->get($request_url);
} $data = json_decode($data_json, true); return $data;
}
12.curl模拟带header头信息的post方法
public static function curl_post($request_url,$params,$session=){
if($session){ $curl = curl_init(); //初始化
$header = [session:.$session]; //设置一个你的浏览器agent的header
curl_setopt($curl,curlopt_url,$request_url); //设置url// curl_setopt($curl,curlopt_httpauth,curlauth_basic); //设置http验证方法
curl_setopt($curl,curlopt_httpheader,$header); //设置头信息
curl_setopt($curl,curlopt_returntransfer,1); //设置curl_exec获取的信息的返回方式
curl_setopt($curl,curlopt_post,1); //设置发送方式为post请求
curl_setopt($curl,curlopt_postfields,$params); //设置post的数据
curl_setopt($curl, curlopt_returntransfer, 1);//不在页面输出 返回数据
$data_json = curl_exec($curl); if($data_json === false){ echo curl_errno($curl); exit();
}
curl_close($curl);
}else{ $curl = new curl(); $data_json = $curl->setoption(curlopt_postfields, http_build_query($params))->post($request_url);
} $data = json_decode($data_json, true); return $data;
}
13.远程下载图片到本地
public static function downloadimage($url, $user_token)
{
$ch = curl_init();
curl_setopt($ch, curlopt_url, $url);
curl_setopt($ch, curlopt_returntransfer, 1);
curl_setopt($ch, curlopt_connecttimeout, 30);
curl_setopt($ch, curlopt_ssl_verifypeer, false); $file = curl_exec($ch);
curl_close($ch); $filename=$user_token..jpg; $path=./images/user/.$filename; self::saveasimage($file, $path);
} private static function saveasimage($file, $path)
{
$resource = fopen($path, 'a');
fwrite($resource, $file);
fclose($resource);
}
14.七牛上传图片
public static function actionimg($filename,$filepath)
{
$auth = new auth(trim(yii::$app->params[qiniu_params][accesskey]),
trim(yii::$app->params[qiniu_params][secretkey])); $uploadmgr = new uploadmanager(); $token = $auth->uploadtoken(trim(yii::$app->params[qiniu_params][upload_path])); list($succ, $fail) = $uploadmgr->putfile($token, $filename, $filepath
); $url = yii::$app->params[qiniu_params][picture_url] . / . $succ[key]; return $url;
}
相关推荐:
如何使用jquery中$.grep()工具函数来筛选数组中的元素、
php用于网络的工具函数库
php中文处理工具函数的用法总结
以上就是php一些工具函数介绍的详细内容。
