<?php
require __DIR__.'/../utils/vendor/autoload.php';
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
function gmt_iso8601($time) {
$dtStr = date("c", $time);
$mydatetime = new DateTime($dtStr);
$expiration = $mydatetime->format(DateTime::ISO8601);
$pos = strpos($expiration, '+');
$expiration = substr($expiration, 0, $pos);
return $expiration."Z";
}
function oss_signature($id,$key,$host,$expire,$dir,$SecurityToken){
$now = time();
$end = $now + $expire;
$expiration = gmt_iso8601($end);
//最大文件大小.用户可以自己设置
$condition = array(0=>'content-length-range', 1=>0, 2=>1048576000);
$conditions[] = $condition;
//表示用户上传的数据,必须是以$dir开始, 不然上传会失败,这一步不是必须项,只是为了安全起见,防止用户通过policy上传到别人的目录
$start = array(0=>'starts-with', 1=>'$key', 2=>$dir);
$conditions[] = $start;
$arr = array('expiration'=>$expiration,'conditions'=>$conditions);
//echo json_encode($arr);
//return;
$policy = json_encode($arr);
$base64_policy = base64_encode($policy);
$string_to_sign = $base64_policy;
$signature = base64_encode(hash_hmac('sha1', $string_to_sign, $key, true));
$response = array();
$response['accessid'] = $id;
$response['host'] = $host;
$response['policy'] = $base64_policy;
$response['signature'] = $signature;
$response['expire'] = $end;
//这个参数是设置用户上传指定的前缀
$response['dir'] = $dir;
$response['secToken'] = $SecurityToken;
return $response;
}
//构建一个阿里云客户端,用于发起请求。
AlibabaCloud::accessKeyClient('AccessKeyId', 'AccessKeySecret')
->regionId('region_id')
->asDefaultClient();
//设置参数,发起请求。
try {
$result = AlibabaCloud::sts()
->v20150401()
->assumeRole()
->withRoleSessionName($client_id)
->withRoleArn($oss_access_role)
->request();
$result = $result->toArray();
echo json_encode($result);
} catch (ClientException $e) {
echo json_encode(array('status'=>'0','message'=>'ERROR_CLIENT'));
} catch (ServerException $e) {
echo json_encode(array('status'=>'0','message'=>'ERROR_SERVER','DATAS'=>$e->getErrorMessage()));
}
?>
PHP 获取微信小程序用户OPENID
<?php
/**
* desc: 获取小程序用户openid
*/
function getOpenid($appid,$appSecret,$code) {
$code = $code;//小程序传来的code值
$appid = $appid;//小程序的appid
$appSecret = $appSecret;// 小程序的$appSecret
$wxUrl = 'https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code';
$getUrl = sprintf($wxUrl, $appid, $appSecret, $code);//把appid,appsecret,code拼接到url里
$result = curl_get($getUrl);//请求拼接好的url
$wxResult = json_decode($result, true);
if (empty($wxResult)) {
echo '获取openid时异常,微信内部错误';
} else {
$loginFail = array_key_exists('errcode', $wxResult);
if ($loginFail) {//请求失败
var_dump($wxResult);
} else {//请求成功
$openid = $wxResult['openid'];
echo $openid;
}
}
}
//php请求网络的方法
function curl_get($url, &$httpCode = 0) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//不做证书校验,部署在linux环境下请改为true
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
$file_contents = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $file_contents;
}
getOpenid('AccessKeyId','AccessKeySecret',$_GET['code']);
?>
PHP 调用阿里云短信接口发送短信单文件版
<?php
date_default_timezone_set('GMT');
$phone = $_POST['phone'];//手机号
$code = rand(100000,999999);//六位随机数验证码
$signName = 'XXXX'; //短信签名(注意不是工单号)
$appid = ''; //AccessKeyId
$secret = ''; //AccessKeySecret
$tplid = 'SMS_XXXXXXXXX'; //模板id
$tplParam = json_encode(['code' => $code]);
$params = [
'AccessKeyId' => $appid, // appid
'Timestamp' => date('Y-m-d\TH:i:s\Z'), //时间
'SignatureMethod'=>'HMAC-SHA1', //固定值
'SignatureVersion'=> '1.0', //固定值
'SignatureNonce' => uniqid(), //随机码(这是PHP函数基于以微秒计的当前时间,生成一个唯一的ID)
//'Signature' => ''
'Format' => 'JSON', //返回的数据类型(如果不传官方说默认JSON经测试是XML)
//业务参数
'Action' => 'SendSms', //固定值不要更改
'Version' => '2017-05-25', //API的版本(固定值)
'RegionId' => 'cn-hangzhou', //API支持的RegionID(固定值)
'PhoneNumbers' => $phone, //接收号码
'SignName' => $signName, //签名
'TemplateCode' => $tplid, //模板id
'TemplateParam' => $tplParam, //短信模板变量替换JSON串,如$code
];
//排序
ksort($params);
//http_build_query($params)这个函数是只把值urlencode
//拼接字符串(键和值都需要urlencode)
$str = '';
foreach($params as $k => $v){
$str .= urlencode($k) . '=' . urlencode($v) . '&';
}
$str = substr($str,0,-1);//把最后一个'&'符号截取下去
$str = str_replace(['+','*','%7E'],['%20','%2A','~'],$str );//替换字符
$new_str = 'GET&' . urlencode('/') . '&' . urlencode($str);//拼接新字符串
$sign = base64_encode(hash_hmac('sha1', $new_str, $secret . '&',true));
$sign = urlencode($sign);//生成签名
$url = 'http://dysmsapi.aliyuncs.com/?Signature=' . $sign . '&'. $str;
//初始化
$ch = curl_init();
//设置选项,包括URL
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
//执行并获取HTML文档内容
$output = curl_exec($ch);
//释放curl句柄
curl_close($ch);
$ret = json_decode($output);
if($ret->Message == 'OK'){
//短信发送成功的逻辑
}
//打印获得的数据
echo $output;
?>
PHP+OPENSSL非对称加密解密
<?php
/**
* @description RSA公钥加密 私钥解密
* @param string $data 待加解密数据
* @param string $operate 操作类型 encode:加密 decode:解密
* @return string 返回加密内容/解密内容
*/
function RSA_openssl($data, $operate = 'encode') {
//RSA 公钥
$rsa_public = '此处填写公钥';
//RSA 私钥
$rsa_private = '此处填写私钥';
//RSA 公钥加密
if ('encode' == $operate) {
$public_key = "-----BEGIN PUBLIC KEY-----\n" . wordwrap($rsa_public, 64, "\n", true) . "\n-----END PUBLIC KEY-----";
$key = openssl_pkey_get_public($public_key);
if (!$key) {
return '公钥不可用';
}
$return_en = openssl_public_encrypt($data, $crypted, $key);
if (!$return_en) {
return '公钥加密失败';
}
return base64_encode($crypted);
}
//RSA 私钥解密
if ('decode' == $operate) {
$private_key = "-----BEGIN PRIVATE KEY-----\n" . wordwrap($rsa_private, 64, "\n", true) . "\n-----END PRIVATE KEY-----";
$key = openssl_pkey_get_private($private_key);
if (!$key) {
return '私钥不可用';
}
$return_de = openssl_private_decrypt(base64_decode($data), $decrypted, $key);
if (!$return_de) {
return '私钥解密失败';
}
return $decrypted;
}
return '';
}
?>
PHP身份证号、手机号、姓名掩码
<?php
/**
* 对银行卡号进行掩码处理
* 掩码规则头4位,末尾余数位不变,中间4的整数倍字符用星号替换,并且用每隔4位空格隔开
* @author 晓风<215628355@qq.com>
* @param string $bankCardNo 银行卡号
* @return string 掩码后的银行卡号
*/
function formatBankCardNo($bankCardNo){
//每隔4位分割为数组
$split = str_split($bankCardNo,4);
//头和尾保留,其他部分替换为星号
$split = array_fill(1,count($split) - 2,"****") + $split;
ksort($split);
//合并
return implode(" ",$split);
}
/**
* 对手机号进行掩码处理
* 手机号掩码的规则是末尾4位,开头余数位不变,中间4的整数倍字符用星号替换
* @author 晓风<215628355@qq.com>
* @param string $mobile 手机号
* @return string
*/
function formatMobile($mobile){
//颠倒顺序每隔4位分割为数组
$split = str_split(strrev($mobile),4);
//头和尾保留,其他部分替换为星号
$split = array_fill(1,count($split) - 2,"****") + $split;
ksort($split);
//合并颠倒顺序
return strrev( implode("",$split));
}
/**
* 姓名第一个字掩码
* @author 晓风<215628355@qq.com>
* @param string $true_name 真实姓名
* @return string
*/
function formatTrueName($true_name){
return "*" . mb_substr($true_name,1);
}
/**
* 对身份证进行掩码
* 掩码规则是显示头两位和末尾1位,中间星号
* @author 晓风<215628355@qq.com>
* @param string $id_card 身份证号
* @return string
*/
function formatIdCard($id_card){
//每隔1位分割为数组
$split = str_split($id_card,1);
//头2位和尾保留,其他部分替换为星号
$split = array_fill(2,count($split) - 3,"*") + $split;
ksort($split);
//合并
return implode("",$split);
}
?>
PHP5.6-MySQL数据库操作类
<?php
class db_mysql
{
var $querynum = 0 ; //当前页面进程查询数据库的次数
var $dblink ; //数据库连接资源
//链接数据库
function connect($dbhost,$dbuser,$dbpw,$dbname='',$dbcharset='utf-8',$pconnect=0 , $halt=true)
{
$func = empty($pconnect) ? 'mysql_connect' : 'mysql_pconnect' ;
$this->dblink = @$func($dbhost,$dbuser,$dbpw) ;
if ($halt && !$this->dblink)
{
$this->halt("无法链接数据库!");
}
//设置查询字符集
mysql_query("SET character_set_connection={$dbcharset},character_set_results={$dbcharset},character_set_client=binary",$this->dblink) ;
//选择数据库
$dbname && @mysql_select_db($dbname,$this->dblink) ;
}
//选择数据库
function select_db($dbname)
{
return mysql_select_db($dbname,$this->dblink);
}
//执行SQL查询
function query($sql)
{
$this->querynum++ ;
return mysql_query($sql,$this->dblink) ;
}
//返回最近一次与连接句柄关联的INSERT,UPDATE 或DELETE 查询所影响的记录行数
function affected_rows()
{
return mysql_affected_rows($this->dblink) ;
}
//取得结果集中行的数目,只对select查询的结果集有效
function num_rows($result)
{
return mysql_num_rows($result) ;
}
//获得单格的查询结果
function result($result,$row=0)
{
return mysql_result($result,$row) ;
}
//取得上一步 INSERT 操作产生的 ID,只对表有AUTO_INCREMENT ID的操作有效
function insert_id()
{
return ($id = mysql_insert_id($this->dblink)) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"), 0);
}
//从结果集提取当前行,以数字为key表示的关联数组形式返回
function fetch_row($result)
{
return mysql_fetch_row($result) ;
}
//从结果集提取当前行,以字段名为key表示的关联数组形式返回
function fetch_assoc($result)
{
return mysql_fetch_assoc($result);
}
//从结果集提取当前行,以字段名和数字为key表示的关联数组形式返回
function fetch_array($result)
{
return mysql_fetch_array($result);
}
//从结果集提取当前行,以对象形式返回
function fetch_object($result)
{
return mysql_fetch_object($result);
}
//释放内存
function free_result($result){
return mysql_free_result($result);
}
//关闭链接
function close()
{
return mysql_close($this->dblink) ;
}
//输出简单的错误html提示信息并终止程序
function halt($msg)
{
$message = "<html>\n<head>\n" ;
$message .= "<meta content='text/html;charset=gb2312'>\n" ;
$message .= "</head>\n" ;
$message .= "<body>\n" ;
$message .= "数据库出错:".htmlspecialchars($msg)."\n" ;
$message .= "</body>\n" ;
$message .= "</html>" ;
echo $message ;
exit ;
}
}
?>