[TOC] ## curl发送post请求 ~~~ function sendCurl($url, $data = [], $curlOption = []) { $curl = curl_init(); // 启动一个CURL会话 curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址 if(!empty($data)){ curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求 curl_setopt($curl, CURLOPT_POSTFIELDS, is_array($data) ? http_build_query($data) : $data); // Post提交的数据包 } curl_setopt($curl, CURLOPT_HEADER, false); // 显示返回的Header区域内容 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在 if(!empty($curlOption)){ foreach ($curlOption as $option => $value){ curl_setopt($curl, $option, $value); } } curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转 curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环 $result = curl_exec($curl); // 执行操作 // print_r(curl_error($curl)); curl_close($curl); // 关闭CURL会话 return $result; } ~~~ ## 返回成功或者失败 ~~~ /** * @author ty * @param string $msg * @param array $data * @desc 返回成功信息 */ public static function returnSuccess($msg = '', $data = []) { $data = [ 'code' => 1, 'msg' => $msg, 'data' => $data ]; echo json_encode($data, JSON_UNESCAPED_UNICODE); exit; } /** * @param string $msg * @param array $data * @desc 返回失败信息 */ public static function returnErr($msg = '', $data = []) { $data = [ 'code' => -1, 'msg' => $msg, 'data' => $data ]; echo json_encode($data, JSON_UNESCAPED_UNICODE); exit; } ~~~ ## 修改字符串编码 ``` /** * Convert encoding. * * @param string $string * @param string $fromEncoding * @param string $toEncoding * @static * @access public * @return string */ static public function convertEncoding($string, $fromEncoding, $toEncoding = 'utf-8') { $toEncoding = str_replace('utf8', 'utf-8', $toEncoding); if(function_exists('mb_convert_encoding')) { /* Remove like utf-8//TRANSLIT. */ $position = strpos($toEncoding, '//'); if($position !== false) $toEncoding = substr($toEncoding, 0, $position); /* Check string encoding. */ $encoding = strtolower(mb_detect_encoding($string, array('ASCII','UTF-8','GB2312','GBK','BIG5'))); if($encoding == $toEncoding) return $string; return mb_convert_encoding($string, $toEncoding, $encoding); } elseif(function_exists('iconv')) { if($fromEncoding == $toEncoding) return $string; $convertString = @iconv($fromEncoding, $toEncoding, $string); /* iconv error then return original. */ if(!$convertString) return $string; return $convertString; } return $string; } ~~~ ## 校验身份证是否合法 ~~~ //检测身份证是否合法以及是否正确 function cardCheck($cardId) { //$cardId="430181198707051478"; //判断是否为18位 if(strlen($cardId) != 18) { return false; } //取最后一位 该位为校验码 $lastc = substr($cardId,17,1); //加权因子 $wi = ['7', '9', '10', '5', '8', '4', '2', '1', '6', '3', '7', '9', '10', '5', '8', '4', '2']; //校验码对照表 $checkV = ['1', '0', 'x', '9', '8', '7', '6', '5', '4', '3', '2']; $sum = 0; for( $i=0; $i<17; $i++){ //取第$i位于加权因子数组的第$i行 相乘 $n = substr($cardId,$i,1); $num = (int)$n*(int)$wi[$i]; $sum += $num; } //和sum除11取余就为该身份证的最后一位校验码 $m = $sum%11; if($checkV[$m] == strtolower($lastc)){ return true; } else{ return false; } } ```