~~~
<?php
namespace app\common\library;
use think\Exception;
class WeChat
{
//code
public function get_code($appid, $redirect_uri, $response_type = 'code', $scope = 'snsapi_base', $state = 'STATE')
{
$api_url = 'https://open.weixin.qq.com/connect/oauth2/authorize?';
$api_url = $api_url . 'appid=' . $appid . '&redirect_uri=' . urlencode($redirect_uri) . '&response_type=' . $response_type . '&scope=' . $scope . '&state=' . $state . '#wechat_redirect';
header('Location:' . $api_url);
exit;
}
//openid
public function get_openid_by_code($appid, $secret, $code, $grant_type = 'authorization_code')
{
$result = $this->get_access_token($appid, $secret, $code, $grant_type);
return $result['openid'];
}
//access_token(拉取用户授权信息)
public function get_access_token($appid, $secret, $code, $grant_type = 'authorization_code')
{
$api_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?';
$api_url = $api_url . 'appid=' . $appid;
$api_url = $api_url . '&secret=' . $secret;
$api_url = $api_url . '&code=' . $code;
$api_url = $api_url . '&grant_type=' . $grant_type;
$response = $this->curlPost($api_url);
$result = json_decode($response, true);
return $result;
}
//refresh_token
public function get_refresh_token($appid, $grant_type = 'refresh_token', $refresh_token)
{
$api_url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?';
$api_url = $api_url . 'appid=' . $appid;
$api_url = $api_url . '&grant_type=' . $grant_type;
$api_url = $api_url . '&refresh_token=' . $refresh_token;
$response = $this->curlPost($api_url);
$result = json_decode($response, true);
return $result;
}
//userinfo
public function get_userinfo($access_token, $openid)
{
$api_url = 'https://api.weixin.qq.com/sns/userinfo?';
$api_url = $api_url . 'access_token=' . $access_token;
$api_url = $api_url . '&openid=' . $openid;
$api_url = $api_url . '&lang=zh_CN';
$response = $this->curlPost($api_url);
$result = json_decode($response, true);
return $result;
}
//check_access_token
public function check_access_token($access_token, $openid)
{
$api_url = 'https://api.weixin.qq.com/sns/auth?';
$api_url = $api_url . 'access_token=' . $access_token;
$api_url = $api_url . '&openid=' . $openid;
$api_url = $api_url . '&lang=zh_CN';
$response = $this->curlPost($api_url);
$result = json_decode($response, true);
return $result;
}
// 判断是否来自微信浏览器
public function from_weixin()
{
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {
return true;
}
return false;
}
public function createNonceStr($length = 16)
{
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$str = "";
for ($i = 0; $i < $length; $i++) {
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $str;
}
//获取公众号接口调用的access_token
public function get_api_access_token($appid, $secret)
{
$api_url = 'https://api.weixin.qq.com/cgi-bin/token?';
$api_url = $api_url . 'grant_type=client_credential';
$api_url = $api_url . '&appid=' . $appid;
$api_url = $api_url . '&secret=' . $secret;
$response = $this->curlPost($api_url);
$result = json_decode($response, true);
return $result;
}
//获取模板id
public function get_template_id($template_no, $access_token)
{
$url = "https://api.weixin.qq.com/cgi-bin/template/api_add_template?access_token=" . $access_token;
$params = json_encode(['template_id_short' => $template_no]);
$response = $this->curlData($url, 2, $params);
$result = json_decode($response, true);
return $result;
}
//获取微信JSSDK-jsapi_ticket
public function get_jsapi_ticket($access_token)
{
$api_url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?";
$api_url = $api_url . 'access_token=' . $access_token;
$api_url = $api_url . '&type=jsapi';
$response = $this->curlPost($api_url);
$result = json_decode($response, true);
return $result;
}
//推送模板消息
public function template_msg_send($json_template, $access_token)
{
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $access_token;
$response = $this->curlData($url, 2, $json_template);
$result = json_decode($response, true);
return $result;
}
/**
* @param $appid
* @param $secret
* @param $page 跳转地址参数['page'=>'pages/index/index?xxx=xxx','width'=>xxx]
* @param int $type 1 图片 2 base64参数
* @return string
*/
public function getWxQrCode($appid, $secret,$page,$type=1){
$ACCESS_TOKEN=$this->get_api_access_token($appid, $secret);
$url="https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=".$ACCESS_TOKEN['access_token'];
$post_data=json_encode($page);
$data=$this->curlData($url,2,$post_data);
$result=$this->data_uri($data,'image/png');
if($type==1){
return '<image src='.$result.'></image>';
}elseif ($type==2){
return $result;
}
}
public function data_uri($contents, $mime)
{
$base64 = base64_encode($contents);
return ('data:' . $mime . ';base64,' . $base64);
}
/**
* @param $appid //小程序唯一标识 (在微信小程序管理后台获取)
* @param $appsecret //小程序的 app secret (在微信小程序管理后台获取)
* @param $js_code
* @return mixed
*/
public function get_user_openid($appid,$appsecret,$js_code)
{
$grant_type = "authorization_code"; //授权(必填)
$curl = curl_init();
//使用curl_setopt() 设置要获得url地址
$url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' . $appid . '&secret=' . $appsecret . '&js_code=' . $js_code . '&grant_type=' . $grant_type;
curl_setopt($curl, CURLOPT_URL, $url);
//设置是否输出header
curl_setopt($curl, CURLOPT_HEADER, false);
//设置是否输出结果
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//设置是否检查服务器端的证书
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
//使用curl_exec()将curl返回的结果转换成正常数据并保存到一个变量中
$data = curl_exec($curl);
//关闭会话
curl_close($curl);
return json_decode($data,true);
}
/**
* 获取小程序明文
* @param $appid
* @param $encryptedData
* @param $iv
* @param $session_key
* @return bool|mixed
*/
public function get_decryptData($appid,$encryptedData,$iv,$session_key)
{
$aesKey = base64_decode($session_key);
$aesIV = base64_decode($iv);
$aesCipher = base64_decode($encryptedData);
$result = openssl_decrypt( $aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);
$data = json_decode( $result,true);
if( $data == NULL )
{
return false;
}
if($data['watermark']['appid'] != $appid )
{
return false;
}
return $data;
}
private function curlPost($url, $params = null, $method = 'GET', $header = array(), $multi = false)
{
$opts = array(
CURLOPT_TIMEOUT => 30,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_HTTPHEADER => $header
);
/* 根据请求类型设置特定参数 */
switch (strtoupper($method)) {
case 'GET':
if ($params) {
$opts[CURLOPT_URL] = $url . '?' . http_build_query($params);
} else {
$opts[CURLOPT_URL] = $url;
}
break;
case 'POST':
//判断是否传输文件
$params = $multi ? $params : http_build_query($params);
$opts[CURLOPT_URL] = $url;
$opts[CURLOPT_POST] = 1;
$opts[CURLOPT_POSTFIELDS] = $params;
break;
default:
throw new Exception('不支持的请求方式!');
}
/* 初始化并执行curl请求 */
$ch = curl_init();
curl_setopt_array($ch, $opts);
$data = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
return $error == 0 ? $data : $error;
}
//专供微信post请求使用
private function curlData($url, $type = 1, $post_data = array())
{
// 1. 初始化
$ch = curl_init();
// 2. 设置选项,包括URL
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//post请求
if ($type == 2) {
// post数据
curl_setopt($ch, CURLOPT_POST, 1);
// post的变量
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
}
curl_setopt($ch, CURLOPT_HEADER, 0);
// 3. 执行并获取HTML文档内容
$output = curl_exec($ch);
if ($output === FALSE) {
//echo "CURL Error:" . curl_error($ch);
return json_encode(array(), JSON_UNESCAPED_UNICODE);
} else {
// 4. 释放curl句柄
//echo "Get data:" . iconv("utf-8", "gbk", serialize($post_data)) . "...........done!\n";
curl_close($ch);
//$data = json_decode($output);
return $output;
}
}
}
~~~
- 面试
- php
- laravel
- 安装
- 创建数据
- 创建控制器
- 错误
- 权限认证
- 图形验证码
- 管理员
- 无限分类
- 视频播放
- 推流
- pc页面
- thinkphp5
- 表单提交安全验证
- crontab
- 获取下级所有数据
- 过滤敏感词
- 二维码
- 邀请码
- jwt token
- 笛卡尔
- 高并发解决优化
- 设置多语言
- 计算经纬度两点距离
- 分片上传
- centos swoole
- 查看swoole版本
- centos 安装 redis
- 把整数随机分成n份或者红包
- 生成随机字符串
- 获取上级
- 宝塔搭建nginx推拉流
- 生成二维码海报
- 二维数组去重
- 经纬度 ---边距 和判断是不是范围内
- workerman
- 扩展
- redis
- mysql
- mysql 主从复制
- 定位问题语句
- 列的选择规则
- Enum介绍
- 索引介绍
- 延迟关联
- in 查询陷阱
- limit 优化
- 事务
- 读写分离
- mysql 权限错误
- mysql计算距离
- mysql数据导出导入
- 索引失效
- mysql 语句技巧
- linux
- 配置nginx 高可用
- Nginx内核参数优化
- 配置优化
- nginx-keepalived实现高可用负载
- lvs+keepalived
- centos5关闭防火墙
- nginx操作
- linux操作
- mysql 安装
- php
- 安装phpadmin
- php7安装mongodb
- 查看php位置
- 查找开启的进程端口
- 守护进程
- centos安装php7.2
- centos 错误解决
- 安装php7错误
- mysql 错误
- 支付宝支付
- html
- js 倒计时
- go vscode 配置
- go util
- 获取两个数之间随机数
- java
- lambda
- 随机抽取
- lombok
- springboot
- 常见注解
- 接受参数常用注解
- 文件上传
- 上传错误日志拦截
- 拦截器
- 定时任务
- mybatis
- 关联查询
- 全局配置
- 缓存
- git
- 微信相关
- 体现到零钱
- 服务商转账
- 公众号小程序相关
- 小程序二维码
- svn
- layui