多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
~~~ <?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; } } } ~~~