首页
时事
归档
壁纸
更多
留言
关于
邻里
Search
1
使用必应Bing每日图片做网站背景(自动)
2,987 阅读
2
vue的输入值校验规则整理
1,662 阅读
3
VUE `ERR_CONNECTION_TIMED_OUT`的解决办法
1,628 阅读
4
好站推荐-https://wangchujiang.com/linux-command/
1,598 阅读
5
微信支付开发前准备(小程序、公众号、App、H5)
1,582 阅读
文章
图说
代码
吐槽
登录
Search
标签搜索
Linux
laravel
windows
TYPO3
php
shell脚本
git
微信
好站
vue
第三方登录
centos
linxu
centos7
thinkPHP
微信支付
api
MySQL
桌面
必应首图
Beer
累计撰写
114
篇文章
累计收到
22
条评论
首页
栏目
文章
图说
代码
吐槽
页面
时事
归档
壁纸
留言
关于
邻里
搜索到
4
篇与
微信
的结果
2019-03-13
微信小程序之 PHP 发送模板消息
今天入坑小程序模板消息的推送,也是踩了不少的坑啊,在这儿 share 一下~,有任何疑问可以联系我!准备1: 官方文档https://developers.weixin.qq.com/miniprogram/dev/api/notice.html#发送模板消息总的来说,小程序发送模板消息和公众号发送模板消息区别不是很大2: 小程序发送模板消息:- 获取access_token并存储以备再次使用 - 小程序端获取发送必要的formID(submit提交事件)或 prepay_id(支付事件) - 拼装模板消息数据 - 调用接口发送模板消息 吐槽一下微信小程序的机制是这样的formid 只能通过移动端进行获取(手机,iPad);每获取到一个 formid **只能使用一次**(一个formid只能发送一条模板消息);发起form表单提交事件 ==> 获取 form_id 和 发起人openid ==> php后台处理拼装模板消息 ==> 发送给发起人;上边说的意思是:A获取的formid只能用来给自己发送模板消息,不能用来给B发送模板消息;解决方案在A触发表单提交事件时,将本次获取到的formid保存到A提交的表单内容中,反正就是与A本次提交的数据绑定死(死结,除了自己,谁都不给用),在B触发对A的某事件时,需要发送模板消息了,这时把A之前绑定的formid拿出来用,还是给A自己用for example:A提交了请假申请(通过表单提交申请内容),B审核,审核完成后,把审核结果通过模板消息发送给A终于到代码了我不是前端妹子,小程序的代码就不放了,自己找找吧<?php namespace Jykj\\Template\\Weixin; /** * 小程序模板消息发送 * 微信规定:不能直接在小程序调用,只能在后台发起 */ class WxSendTemplate { private $token =''''; private $appid = ''******************''; private $appsecret = ''********************************''; private $templateid = ''*******************************************''; private $SEND_TEMPLATE_URL = ''https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=##TOKEN##''; private $GET_ACCESSTOKEN_URL = ''https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=##APPID##&secret=##APPSECRET##''; /** * 构造函数,获取access_token * @Author wanghongbin * @Email wanghongbin@ngoos.org * @DateTime 2018-07-06 */ public function __construct() { $this->token = $this->getAccessToken(); } /** * 发送模板消息 * @Author wanghongbin * @Email wanghongbin@ngoos.org * @DateTime 2018-07-06 * @param array * @return [type] */ public function sendTemplateData($data = array()) { $options = array( ''http'' => array( ''method'' => ''POST'', ''header'' => ''Content-type:application/json'',//header 需要设置为 JSON ''content'' => $this->getTemplatePostData($data), ''timeout'' => 60//超时时间 ) ); $url = str_replace("##TOKEN##",$this->token,$this->SEND_TEMPLATE_URL); $context = stream_context_create($options); $result = file_get_contents($url, false, $context); return $result; } /** * 获取模板消息 * @Author wanghongbin * @Email wanghongbin@ngoos.org * @DateTime 2018-07-06 * @param array * @return [type] */ private function getTemplatePostData($data=array()){ $post_data = array ( "touser" => $data[''openid''],//用户的 openID,可用过 wx.getUserInfo 获取 "template_id" => $this->templateid,//小程序后台申请到的模板编号 "page" => $data[''pageuri''],//点击模板消息后跳转到的页面,可以传递参数 "form_id" => $data[''formid''],//第一步里获取到的 formID "data" => array( ''keyword1'' => array( "value" => $data[''status''],"color"=>"green"), ''keyword2'' => array( "value" => $data[''shopername'']), ''keyword3'' => array( "value" => $data[''telephone'']), ''keyword4'' => array( "value" => $data[''ordertime'']) ), "emphasis_keyword" => "keyword1.DATA" ); //将数组编码为 JSON return \\json_encode($post_data, true); } /** * 获取AccessToken * @Author wanghongbin * @Email wanghongbin@ngoos.org * @DateTime 2018-07-06 * @return [type] */ private function getAccessToken () { $url = str_replace("##APPID##",$this->appid,$this->GET_ACCESSTOKEN_URL); $url = str_replace("##APPSECRET##",$this->appsecret,$url); $html = file_get_contents($url); $output = json_decode($html, true); $access_token = $output[''access_token'']; return $access_token; } /** * 获取所有的模板 * @Author wanghongbin * @Email wanghongbin@ngoos.org * @DateTime 2018-07-06 * @return [type] */ public function get_all_private_template() { $url = "https://api.weixin.qq.com/cgi-bin/wxopen/template/list?access_token=".$this->token; $res = file_get_contents($url); return json_decode($res,true); } }效果图
2019年03月13日
838 阅读
0 评论
0 点赞
2019-03-13
php用微信给指定用户发送消息实例
php用微信给指定用户发送消息实例function http_post($url, $param) { $data = ""; if (is_array($param)) { foreach ($param as $k=>$v) { $data .= '&'.$k.'='.$v; } if (strlen($data) > 0) { $data = substr($data, 1, strlen($data) - 1); } } else $data = $param; $ch = curl_init(); curl_setopt( $ch, CURLOPT_URL, $url); curl_setopt( $ch, CURLOPT_POST, 1 ); curl_setopt( $ch, CURLOPT_HEADER, 0 ); curl_setopt($ch, CURLOPT_TIMEOUT,300); //只需要设置一个秒的数量就可以 curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt( $ch, CURLOPT_POSTFIELDS, $data); $return = curl_exec($ch); curl_close($ch); return $return; }// 调用 $url = "https://api.weixin.qq.com/cgi-bin/token?"; $data = array( 'grant_type'=>'client_credential', 'appid'=>'微信appid', 'secret'=>'微信secret' ); $file = http_post($url,$data); $josn = json_decode($file,true); $token = $josn['access_token']; $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$token; $arr = array( 'touser'=>'o5Y-RwZtS6L7y1R1MZEtszIVp4Zc',////用户的openid 'msgtype'=>'news', "news"=>array( 'articles'=>array( array( 'title'=>'', 'description'=> 'Is Really A Happy Day', 'url'=>'http://www.kuitao8.com/', 'picurl'=>'http://static.s1.thumb.lanrenchengxu.com/1/1b9f3756bb2b82d71fa69446659062a3.jpg', ), ), ), ); $res = http_post($url,json_encode($arr)); print_r(json_decode($res,true));
2019年03月13日
551 阅读
0 评论
0 点赞
2019-03-13
微信扫码登录到系统方式(二):PHP实现PC端微信扫描二维码授权登录
前言之前有篇文章说了下微信扫描带参数二维码进行登录,今天再说一下另外一种微信扫码登录方式,即PC端授权登录吐槽两句作为一个开发者,对于微信的接口调用上,一开始我是拒绝的。。。为什么呢?因为微信的文档不是很完整,就拿支付宝的文档做比,同样功能用支付宝的接口开发,会节省至少一半的时间,这是大多数开发者的心声;但是,不得不说,微信在大量用户基础上的产品还是不错的,方便了一大批人,比如今天我们要说的第三方登录;还得吐槽一句,微信的认证机制真的恶心。。。做这个要认证,做那个要认证,每次认证三百大洋。。。而且还是每年都要认证。。。恶心!功能了解网站应用微信登录是基于OAuth2.0协议标准构建的微信OAuth2.0授权登录系统。在进行微信OAuth2.在进行微信OAuth2.0授权登录接入之前,在微信开放平台注册开发者帐号,并拥有一个已审核通过的网站应用,并获得相应的AppID和AppSecret,申请微信登录且通过审核后,可开始接入流程。微信OAuth2.0授权登录让微信用户使用微信身份安全登录第三方应用或网站,在微信用户授权登录已接入微信OAuth2.0的第三方应用后,第三方可以获取到用户的接口调用凭证(access_token),通过access_token可以进行微信开放平台授权关系接口调用,从而可实现获取微信用户基本开放信息和帮助用户实现基础开放功能等。微信OAuth2.0授权登录目前支持authorization_code模式,适用于拥有server端的应用授权。获取access_token第三方发起微信授权登录请求,微信用户允许授权第三方应用后,微信会拉起应用或重定向到第三方网站,并且带上授权临时票据code参数;通过code参数加上AppID和AppSecret等,通过API换取access_token;通过access_token进行接口调用,获取用户基本数据资源或帮助用户实现基本操作。实现方法实现一php代码处理/** * 获取openid */ public function followAction(){ $appid = 'appid'; $secret = 'appsecret'; //处理业务 if (GeneralUtility::_GET('code')) { $code = GeneralUtility::_GET('code'); $codeUrl = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret.'&code='.$code.'&grant_type=authorization_code'; $result = json_decode(file_get_contents($url), true); if (isset($result['access_token']) && isset($result['openid'])) { // 已获取到用户openid和access_token,可以继续使用这两个参数获取详细的用户信息了 } $this->redirect('follow'); } $baseUrl = GeneralUtility::getIndpEnv('TYPO3_SITE_URL'); $redirect_uri = urlencode(GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL')); //准备向微信发请求 $url = "https://open.weixin.qq.com/connect/qrconnect?appid=".$appid."&redirect_uri=".$redirect_uri ."&response_type=code&scope=snsapi_login&state=STATE#wechat_redirect"; // header("Location:".$url); $this->view->assign('loginurl', $url); }实现二php 处理业务代码/** * 获取openid */ public function followAction(){ $appid = 'appid'; $secret = 'appsecret'; //处理业务 if (GeneralUtility::_GET('code')) { $code = GeneralUtility::_GET('code'); $codeUrl = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret.'&code='.$code.'&grant_type=authorization_code'; $result = json_decode(file_get_contents($url), true); if (isset($result['access_token']) && isset($result['openid'])) { $user = $this->tuserRepository->findByUid($GLOBALS['TSFE']->fe_user->user['uid']); $user->setOpenid($result['openid']); $this->tuserRepository->update($user); $this->refreshObjData(); } $this->redirect('follow'); } $baseUrl = GeneralUtility::getIndpEnv('TYPO3_SITE_URL'); $redirect_uri = urlencode(GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL')); $this->view->assign('redirect_uri', $redirect_uri); }html代码<!--把这个代码放在你想要显示二维码的地方--!> <div id="login_container"></divjavascript代码<script src="http://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js"></script> <script type='text/javascript'> $(document).ready(function(){ var redirect_uri = 'redirect_uri';//用于处理业务的链接,可由后台传值 var hash = Date.parse(new Date()); var obj = new WxLogin({ id:"login_container",//二维码容器id appid: "appid",//APPID scope: "snsapi_login",//写死就行 redirect_uri:encodeURI("'.$redirect_uri.'"),//业务处理链接 需要用encodeURI()处理 state: hash,//任意字符 style: "black", //样式:white href: "https://typo3_go.gitee.io/wechat_login_css/wxlogin.css" //自定义样式链接,放在码云上最好不过了 }); }); </script>再说一句
2019年03月13日
570 阅读
0 评论
0 点赞
2019-03-13
微信扫码登录到系统方式(一):PHP实现微信扫描带参数的二维码登录
前段时间,公司有个项目,想让用户在登录系统的时候就关注公众号,但是现在让用户强制关注是违规的,只能另寻他法,然后,就找到了“微信扫描带参数二维码事件”,下面分享下配置微信服务器首先在你系统中新建一个文件,写入以下内容,确保文件可以被浏览器访问,该文件所在页面不能有任何其他内容输出,并记录你的文件访问链接,还有比较重要的一点,记录下你设置的token等会在配置微信服务器时要用// 微信服务器回调 public function wxcallbackAction() { // file_put_contents('checkSignature.log', date('Y-m-d H:i:s') . '===get===' . \json_encode($_GET) . chr(10) . chr(10),FILE_APPEND | LOCK_EX); if($this->checkSignature()){ echo $_GET["echostr"]; } exit; } private function checkSignature() { $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $token = 'yourtoken';//注意:这块是你自定义的token,等会在配置微信服务器时要用 $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ){ return true; }else{ return false; } }然后登录微信公众平台后,依次找到基本配置->微信服务器配置->修改配置->启用配置,配置时,URL是你刚才新建的文件的浏览器访问路径,TOKEN是你刚才新建的文件中自定义的token,设置完成后提交,token验证失败的话不会保存官方文档:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1472017492_58YV5 1.4生成微信带参数的二维码前端发起(使用js轮询发起请求,生成二维码,扫描后登录等)<script type="text/javascript"> var t2 =''; $(function() { var rand = parseInt(Math.random() * (999999999 - 10000000 + 1) + 10000000); //生成随机数做为二维码的参数 (用来判断扫码的用户) //生成二维码 $.post("后台用于生成二维码的链接",{str:rand},function(result){ console.log(result); var obj = JSON.parse(result); var sign = obj.scene_str; var src = "https://mp.weixin.qq.com/cgi-bin/showqrcode"+obj.src; $('#wxewm').attr('src',src); $('input[name=sign]').val(sign); }); //轮询扫码后的登录 t2 = window.setInterval("login()",1000);//使用字符串执行方法 //window.clearTimeout(t1);//去掉定时器 setTimeout("end()",30000); }) //如果超过一定时间不扫码,停止轮询,提示过期 function end(){ $('#wxewm').hide(); //隐藏二维码 window.clearTimeout(t2); //停止轮询 $('#notice').show(); //提示二维码过期 } //不停发送请求,获取扫码人的信息,如果扫码了获取信息停止轮询 function login(){ var code =$('input[name=sign]').val(); $.post("你将要进行数据处理的页面",{str:code},function(result){ console.log(result); var obj = JSON.parse(result); var user_name = obj.user_name; var pass = obj.pass; if(pass){ window.clearTimeout(t2); //跳转到登录后的页面 window.location.href="https://www.baidu.com"; } }); } </script>PHP后端进行数据处理public function get_access_token() //获取token { $appid= '你的公众号APPID'; $secret='你的公众号APPSECRET'; $data = json_decode(file_get_contents("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret")) ; return $data->access_token; } public function build_qrcode() //生成带参数二维码 { $str = $_POST['str']; $access_token=$this->get_access_token(); // scene_id 参数 $data = '{ "expire_seconds": 604800, "action_name": "QR_SCENE", "action_info": { "scene": { "scene_id":'.$str.' } } }'; $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$access_token; $obj = json_decode(httpRequest($url, 'POST', $data)); $ticket=$obj->ticket; return json_encode(['src'=>"?ticket=$ticket",'scene_str'=>$str]); } // 登录数据处理 public function login() { //$GLOBALS["HTTP_RAW_POST_DATA"]; //这个貌似在PHP7以后用不了了; $postStr = file_get_contents("php://input"); //换成这种方式收数据 $postObj = json_decode($this->xmlToArrayOrObject($postStr, true),true); // 取出openID $openid = $postObj['FromUserName']; $EventKey = $postObj['EventKey']; $user=db('fe_users')->field('id')->where(['openid'=>$openid])->find(); if(!$user['id']){ $access_token=$this->get_access_token(); $wxinfo =json_decode (file_get_contents('https://api.weixin.qq.com/sns/userinfo?access_token=' . $access_token . '&openid=' . $openid), true); } // 添加session成功后返回值,在前端进行跳转 return json_encode(['code'=>1,'msg'=>'登录成功','url'=>'http://www.yoururl.com']); } /** * 作用:将xml转为object/array */ public function xmlToArrayOrObject($xml,$flag=true) { if (!$flag) { //将XML转为object $xml_data = simplexml_load_string($xml,'SimpleXMLElement', LIBXML_NOCDATA); }else{ //将XML转为array $xml_data = json_decode(json_encode(simplexml_load_string($xml,'SimpleXMLElement', LIBXML_NOCDATA)), true); } return $xml_data; }
2019年03月13日
648 阅读
0 评论
0 点赞