博客列表 >微信开发H5接口篇

微信开发H5接口篇

人傻钱少好说话
人傻钱少好说话原创
2020年09月02日 02:25:09869浏览
  1. 今天简单的介绍下PHP在微信中的应用,首先我们需要用到公众号中的appid AppSecret 就可以开发了,看代码:
  2. <?php
  3. /**
  4. * 使用原生
  5. * 微信类
  6. */
  7. class Wechat{
  8. //定义常量
  9. const APPID = '11122';
  10. const APPSECRET = '3333';
  11. //定义变量
  12. public $access_token ; //微信access_token
  13. public $redis;//redis
  14. public $show;//返回
  15. //实例化
  16. public function __construct($redis_password = 123456,$redis_domain = '127.0.0.1',$redis_point = 6379,$redis_select = 0){
  17. //返回参数
  18. $show = $this->show = 1;
  19. // function ($code = 1, $msg = '', $data = []){
  20. // echo json_encode(array('code' => $code, 'msg' => $msg, 'data' => $data));
  21. // exit;
  22. // };
  23. //实例化redis
  24. $redis = new Redis();
  25. $redis->connect($redis_domain, $redis_point);
  26. $redis->auth($redis_password);
  27. $redis->select($redis_select);
  28. $this->redis = &$redis;
  29. }
  30. /**
  31. * redis
  32. */
  33. function redis():Redis{
  34. return $this->redis;
  35. }
  36. /**
  37. * 获取assaccess_token
  38. * 地址:https://api.weixin.qq.com/sns/oauth2/access_token
  39. */
  40. function get_access_token():string{
  41. //access_token是否存在redis中
  42. if(!$this->redis->exists('wechat_access_token')){
  43. $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".self::APPID."&secret=".self::APPSECRET;
  44. $res = json_decode(self::curl('GET', $url, []), true);
  45. if (isset($res['access_token'])) {
  46. $this->redis->set('wechat_access_token', $res['access_token'], ['ex'=> 7100]);
  47. return $this->redis->get('wechat_access_token');
  48. } else {
  49. self::show(0, '获取ACCESS_TOKEN错误:'.$res['errcode'].",msg:".$res['errmsg']);
  50. }
  51. }
  52. return $this->redis->get('wechat_access_token');
  53. }
  54. /**
  55. * 微信获取code码
  56. * @param url 前端地址
  57. * @return void
  58. */
  59. function getcode():viod{
  60. $reques_url = $_POST['url'] ?? 'https://baidu.com';
  61. $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.self::APPID.'&redirect_uri='.urlencode($reques_url).'&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect';
  62. header("Location:$url");//跳转
  63. }
  64. /**
  65. * 微信登录
  66. * @param code $code码 客户端传递的code编码
  67. * @return arrary
  68. */
  69. function wxlogin():array{
  70. $openid = $_POST['openid'] ?? '';
  71. $access_token = self::get_access_token();
  72. //获取用户信息
  73. $url ='https://api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$openid}&lang=zh_CN';
  74. $userinfo = json_decode($url,self::curl('GET',$url,[]),true);
  75. isset($userinfo['errmsg']) ? self::show(0,'失败:'.$userinfo['errmsg']) : self::show(1,'成功',$userinfo);
  76. }
  77. /**
  78. * 返回
  79. * @param code
  80. * @param msg
  81. * @param data
  82. * @return json|void
  83. */
  84. function show($code = 1, $msg = '', $data = []):void{
  85. echo json_encode(array('code' => $code, 'msg' => $msg, 'data' => $data));
  86. exit;
  87. }
  88. /**
  89. * Curl操作
  90. * @param string $type 请求类型 'POST' 或 'GET' 大小写都可以
  91. * @param string $url 请求地址 url
  92. * @param array $data 数组 cookie 请求cookie data post请求数据
  93. * @param bool $headerFile 返回头信息 如果页面做了跳转 则可以从返回头信息获得跳转地址,应用场景不多
  94. * @return bool|mixed
  95. */
  96. function curl($type, $url, $data=[], $headerFile=false) {
  97. $type = strtoupper($type);
  98. $type_list = ['POST', 'GET', 'PUT'];
  99. if(!in_array($type, $type_list)) $type = 'POST';
  100. $ch = curl_init();
  101. // 请求类型
  102. if($type == 'POST') {
  103. curl_setopt($ch, CURLOPT_POST, 1);
  104. }else if($type == 'PUT') {
  105. curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"PUT"); //设置请求方式
  106. }
  107. curl_setopt($ch, CURLOPT_URL, trim($url));
  108. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  109. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  110. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  111. curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
  112. curl_setopt($ch, CURLOPT_ENCODING, ''); // 这个是解释gzip内容, 解决获取结果乱码 gzip,deflate
  113. // 是否存在请求字段信息
  114. if(!empty($data['data'])) {
  115. curl_setopt($ch, CURLOPT_POSTFIELDS, $data['data']);
  116. }
  117. // 是否存在cookie
  118. if(!empty($data['cookie'])) {
  119. curl_setopt($ch, CURLOPT_COOKIE, $data['cookie']);
  120. }
  121. // 请求头
  122. if(!empty($data['header'])) {
  123. curl_setopt($ch, CURLOPT_HTTPHEADER, $data['header']);
  124. }
  125. // 设置代理
  126. if(!empty($data['proxy'])) {
  127. curl_setopt ($ch, CURLOPT_PROXY, $data['proxy']);
  128. }
  129. // 证书
  130. if(!empty($data['ssl_cert'])) {
  131. curl_setopt($ch,CURLOPT_SSLCERT, $data['ssl_cert']);
  132. }
  133. if(!empty($data['ssl_key'])) {
  134. curl_setopt($ch,CURLOPT_SSLKEY, $data['ssl_key']);
  135. }
  136. // 返回ResponseHeader
  137. if($headerFile) {
  138. curl_setopt($ch, CURLOPT_HEADER, 1);
  139. }
  140. // 设置请求超时时间
  141. curl_setopt($ch, CURLOPT_TIMEOUT, 6);
  142. // 发送请求
  143. $result = curl_exec($ch);
  144. if (curl_errno($ch)) return false;
  145. curl_close($ch);
  146. return $result;
  147. }
  148. }
  149. $class = new Wechat();
  150. $access_token = $class->getcode();
  151. ?>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议