微信公众号登陆、模板发送、access_token获取等PHP代码分享

我现在在项目中遇到要将公众号和小程序联系起来,我们知道,微信公众和小程序在微信开放平台下绑定就可以获得unionid,unionid是否相同就可以 确定用户是否是同一个人,微信小程序开发文档写的很清晰,要想了解详情就点这里(点击打开链接);小程序模板消息我们知道,发送需要formid或者prepay_id,但是formid或者prepay_id是有时间限制,7天,为了弥补这个缺陷,我就想用微信公众号来发送模板消息,因此我就写了这些功能代码,我主要分享一下,公众号登陆,和模板消息TP3.2代码;

代码如下:

public function _initialize(){     if(!isset($_SESSION["user_id"])||empty($_SESSION["user_id"])){         //没有登录
         // 去登录;
         $this->login();
     }
 }
 //用户登录
 public function login(){     $shop_id=18;//店铺id
     $roe=M('wx_gzh_config')->field('appid,appsecret')->where('shop_id='.$shop_id)->find();
     $kf_appid=$roe['appid'];
     $kf_secret=$roe['appsecret'];
     //网页授权
     $code=$_GET['code']?$_GET['code']:"";
     if(empty($code)){         $urlt="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$kf_appid."&redirect_uri=".urlEncode('http://gw.kxsp.net/Home/GZHao/login')."&response_type=code&scope=snsapi_userinfo&state=12345#wechat_redirect";
         header("location:".$urlt);return;
         // break;
     }else{         $url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$kf_appid."&secret=".$kf_secret."&code=".$code."&grant_type=authorization_code";
         $temXML=$this->CURLSend($url,"get");
         $objd=json_decode($temXML);
         $access_token =$objd->access_token;
         //$access_refresh_token=$objd->refresh_token;
         $openid=$objd->openid;
         //$access_refresh_token_time=date("Y-m-d H:i:s",strtotime("+30 day"));
         $unionid=$objd->unionid;  ///请先到 微信开放平台绑定公众号 后才可以获取 unionid ;
         //检验授权凭证(access_token)是否有效
         $urlu="https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
         $temtXML=$this->CURLSend($urlu,"get");
         $objy=json_decode($temtXML);
         $infoList=M('user')->field('gzh_openid,id,unionid')->where(array('unionid'=>$unionid))->find();
         if(!empty($infoList['id'])){             $timed=date("Y-m-d H:i:s");
             if(!empty($infoList['gzh_openid'])){                 M('user')->where(array('unionid'=>$unionid))->save(array('time'=>$timed));
             }else{                 M('user')->where(array('unionid'=>$unionid))->save(array('time'=>$timed,"gzh_openid"=>$openid));
             }
             $user_id=$infoList['id'];
         }else{             $data['gzh_openid']   = $openid;
             $data['username'] = $objy->nickname;
             $data['sex']      = $objy->sex;
             $data['avater']   = $objy->headimgurl;
             $data['country']  = $objy->country;
             $data['province'] = $objy->province;
             $data['city']     = $objy->city;
             $data['unionid'] = $unionid;
             $data['shop_id'] = 18;
             $data['ctime'] = date("Y-m-d H:i:s");
             $ids=M('user')->add($data);
             $user_id=$ids;
         }
         //echo json_encode($objy);exit;
        }
     $_SESSION["user_id"]=$user_id;
     if($_SESSION["user_id"]){         $this->redirect("Home/GZHao/index");
     }
    }
 public function index()
 {         if($_SESSION["user_id"]){             //unset($_SESSION["user_id"]);
             $info = M("user")->field('gzh_openid')->where('id='.$_SESSION["user_id"])->find();
             $user_name = "舞步天行者";
             $openid = $info["gzh_openid"];
             $time = date("Y-m-d H:i:s");
             $name = "温斯顿";
             $phone = "13926560746";
             if($info["gzh_openid"]){                 //存在发送消息
                 $myjson=$this->wx_moban($openid,$user_name,$time,$name,$phone);
                 $url_fstzv="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$this->getAccessToken();
                 $this->CURLSend($url_fstzv,"post",$myjson);
             }
         }

     $this->display();
 }
 public function CURLSend($url, $method = 'get', $data = ''){     $ch = curl_init();//初始化
     $headers = array('Accept-Charset: utf-8');
     //设置URL和相应的选项
     curl_setopt($ch, CURLOPT_URL, $url);//指定请求的URL
     curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($method));//提交方式
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//不验证SSL
     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);//不验证SSL
     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);//设置HTTP头字段的数组
     curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible;MSIE5.01;Windows NT 5.0)');//头的字符串

     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
     curl_setopt($ch, CURLOPT_AUTOREFERER, 1);//自动设置header中的Referer:信息
     curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//提交数值
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//是否输出到屏幕上,true不直接输出
     $temp = curl_exec($ch);//执行并获取结果
     curl_close($ch);
     return $temp;//return 返回值
 }
 //微信模板发送
 public function wx_moban($openid,$user_name,$l_name,$url_name,$end_time){     $wx_mo='{"touser":"'.$openid.'","template_id": "****************","url":"https://zs.kxsp.net/paimai/Api/Shop/","data": {"first": {"value": "尊敬的'.$user_name.'先生,您好!","color":"#173177"},"keyword1": {"value":"'.$l_name.'","color": "#173177"},"keyword2": {"value": "'.$url_name.'","color": "#173177"},"keyword3": {"value": "'.$end_time.'","color": "#173177"},"remark": {"value": "您的预约单已提交成功,我们将于24小时内与您联络并确认预约件的相关内容,请您保持手机畅通,祝您本次消费愉快!。","color": "#173177"}}}';
     return $wx_mo;
 }
//获取access_token,微信调用接口凭据(使用接口需要用到)
//access_token获取频次2000/天,2小时内有效
 public function getAccessToken(){     $shop_id=18;//店铺id
     $re=M('wx_gzh_config')->where('shop_id='.$shop_id)->find();
     $kf_appid=$re['appid'];
     $kf_secret=$re['appsecret'];
     if($re['expires_in']<time()){         $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$kf_appid."&secret=".$kf_secret;
         $tmp=$this->CURLSend($url,'get');
         $obj=json_decode($tmp);
         $dty=(time()+$obj->expires_in);
         $data['access_token']=$obj->access_token;
         $data['expires_in']=$dty;
         M('wx_gzh_config')->where('shop_id='.$shop_id)->save($data);
         return $obj->access_token;
     }else {         return  $re['access_token'];
     }
 }
店铺id是我自己的,查询数据库要按照自己的情况来改我的代码,不要直接复制,template_id是你公众号自己申请的,
有不好地方可以留言,谢谢

微信公众号登陆、模板发送、access_token获取 PHP代码相关推荐

  1. 微信公众号消息模板发送

    微信公众号消息模板发送 微信公众号消息模板群发功能 开通申请模板消息功能 获取模板消息发送所需参数 AccessToken pojo类 TemplateData pojo类 WxTagsEntity ...

  2. 微信公众号网页-模板发送失败:40001

    记录下遇到的问题 模板发送失败:40001:获取 access_token 时 AppSecret 错误,或者 access_token 无效.请开发者认真比对 AppSecret 的正确性,或查看是 ...

  3. php 公众号 模板消息id如何获取_微信公众号后台模板消息如何实现发送的功能...

    在公众平台实现发送模板消息功能,只能通过公众平台的接口开发实现,或者通过第三方平台微号帮功能模板消息群发实现,均能为微信公众号发送模板消息功能,模板消息发送不占用公众号每月的群发次数,模板消息仅用于公 ...

  4. php微信公众号向指定客服发信息,微信公众号给用户发送一条消息 客服消息

    可以用客服消息接口或模板消息接口实现.但是需要认证公众号才能有权限. 认证服务号可以发送客服接口消息[需要对应openid24小时内有互动]和模板消息,订阅号则没有模板消息权限. 下面是发送客服消息的 ...

  5. 通过微信公众号给npy发送天气的小程序

    通过微信公众号给npy发送天气的小程序 操作步骤: 模仿抖音上的给女朋友发送天气的小程序 1.免费虚拟主机(自带域名) https://profreehost.com/ 3.微信公众号测试号平台(个人 ...

  6. 微信小程序使用微信公众号的模板消息进行消息推送开发流程

    微信小程序使用微信公众号的模板消息进行消息推送开发流程 微信公众号服务号,微信公众号订阅号,微信公众号开发者平台,微信小程序 这些的账号都是独立的不能共用 微信开放平台开发者资质认证审核费用为300元 ...

  7. java 微信自定义菜单开发_微信公众号开发之设置自定义菜单实例代码【java版】...

    本实例是为了实现在管理后台实现微信菜单的添加删除管理. 1.首先我们需要新建一个数据库表用于存放menu菜单项 可包含的字段有id.父类id.name.排序.是否显示.类型(view.click).链 ...

  8. 微信公众号网页授权思路解析及具体代码

    微信公众号网页授权思路解析及具体代码 微信开发文档 实现方式也是两种: 1.静默授权登录 授权登录以snsapi_base为scope发起的网页授权,是用来获取进入页面的用户的openid的,并且是静 ...

  9. php创建菜单_php实现微信公众号创建自定义菜单功能的实例代码

    目的 创建自定义菜单,实现菜单事件. 首先获取Access_Token 接口: 我用的是测试号,修改APPID和APPSECRET,然后浏览器访问上面这个Url即可生成Access_Token 然后配 ...

  10. php如何判断是否关注,微信公众号判断用户是否已关注php代码解析

    搜索热词 <:微信公众号判断用户是否已关注PHP代码解析>要点: 本文介绍了:微信公众号判断用户是否已关注PHP代码解析,希望对您有用.如果有疑问,可以联系我们. 用户关注公众号,才能参与 ...

最新文章

  1. zzuli 20级新生周赛(1)题解
  2. (03) spring Boot 的配置
  3. SQL Between a and b
  4. Python 技术篇-按任意格式灵活获取日期、时间、年月日、时分秒。日期格式化。
  5. Bitmap转圆形图片Bitmap
  6. 你知道你常用的dos和linux命令吗?
  7. 马云:首批助力欧洲防疫的物资今天到达比利时
  8. centos6.5安装redmine2.6
  9. 向下兼容性格什么意思_向下兼容是什么意思
  10. 清华山维EPS二次开发VBS基础篇
  11. miscrosoft visio 2003记
  12. java笑话_[转]爆笑程序员的笑话集锦
  13. 本地win10服务器不能复制文件,解决Win10无法复制文件并提示“0x80070032”错误的方法...
  14. Python自我成长笔记(二)
  15. 伤害世界怎么自建服务器,伤害世界服务器架设方法_伤害世界Hurtworld怎么架设服务器_游戏堡...
  16. 中国移动国际iSolutions:中国首张覆盖全球的云网络
  17. 短视频选题的方法脚本结构撰写
  18. 经典非局部均值滤波(NLM)算法python实现(2)
  19. DNS无法区域传送(axfr,ixfr)
  20. kali手机内核编译汇总

热门文章

  1. 【n卡】GeForce Experience 的额外功能
  2. 线性代数-向量空间-基向量定义
  3. 生信分析是个什么玩意儿?
  4. R语言做生信分析系列(一)—— R软件简单安装
  5. pandas填充空数组_pandas | DataFrame基础运算以及空值填充
  6. 语法冠词,虚拟语气,形容词排序
  7. 路由器/交换机/服务器的分类
  8. google开放的一些接口
  9. 美国的人民币汇率谋略
  10. 图解项目绩效考核表实例--绩效考核不用愁