Home >Backend Development >PHP Tutorial >Introduction to some php tool functions
本文主要和大家分享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()工具函数来筛选数组中的元素、
The above is the detailed content of Introduction to some php tool functions. For more information, please follow other related articles on the PHP Chinese website!