经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » PHP » 查看文章
php获取微信openid方法总结
来源:jb51  时间:2019/10/11 8:36:19  对本文有异议

使用微信接口,无论是自动登录还是微信支付我们首先需要获取的就是openid,获取openid的方式有两种,一种是在关注的时候进行获取,这种订阅号就可以获取的到,第二种是通过网页授权获取,这种获取需要的是认证服务号。

今天我要说的是第二种网页授权获取openid。下面是我写的一个关于获取openid的类

  1. <?php
  2.  
  3. /**
  4.  
  5. * 微信授权相关接口
  6.  
  7. *
  8.  
  9. * @link http://www.phpddt.com
  10.  
  11. */
  12.  
  13. class Wchat
  14.  
  15. {
  16.  
  17. private $app_id = 'wx444444444444';
  18.  
  19. private $app_secret = '77777777';
  20.  
  21. private $state='aaaa';
  22.  
  23. /**
  24.  
  25. * 获取微信授权链接
  26.  
  27. *
  28.  
  29. * @param string $redirect_uri 跳转地址
  30.  
  31. * @param mixed $state 参数
  32.  
  33. */
  34.  
  35. public function get_authorize_url($redirect_uri = '', $state = '')
  36.  
  37. {
  38.  
  39. $redirect_uri = urlencode($redirect_uri);
  40.  
  41. return "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->app_id}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_userinfo&state={$state}#wechat_redirect";
  42.  
  43. }
  44.  
  45. /**
  46.  
  47. * 获取微信openid
  48.  
  49. */
  50.  
  51. public function getOpenid($turl)
  52.  
  53. {
  54.  
  55. if (!isset($_GET['code'])){
  56.  
  57. //触发微信返回code码
  58.  
  59.  
  60. $url=$this->get_authorize_url($turl, $this->state);
  61.  
  62.  
  63. Header("Location: $url");
  64.  
  65. exit();
  66.  
  67. } else {
  68.  
  69. //获取code码,以获取openid
  70.  
  71. $code = $_GET['code'];
  72.  
  73. $access_info = $this->get_access_token($code);
  74.  
  75. return $access_info;
  76.  
  77. }
  78.  
  79.  
  80. }
  81.  
  82. /**
  83.  
  84. * 获取授权token网页授权
  85.  
  86. *
  87.  
  88. * @param string $code 通过get_authorize_url获取到的code
  89.  
  90. */
  91.  
  92. public function get_access_token($code = '')
  93.  
  94. {
  95.  
  96. $appid=$this->app_id;
  97.  
  98. $appsecret=$this->app_secret;
  99.  
  100.  
  101. $token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code";
  102.  
  103. //echo $token_url;
  104.  
  105. $token_data = $this->http($token_url);
  106.  
  107. // var_dump( $token_data);
  108.  
  109. if($token_data[0] == 200)
  110.  
  111. {
  112.  
  113. $ar=json_decode($token_data[1], TRUE);
  114.  
  115. return $ar;
  116.  
  117. }
  118.  
  119.  
  120. return $token_data[1];
  121.  
  122. }
  123.  
  124.  
  125.  
  126. public function http($url, $method='', $postfields = null, $headers = array(), $debug = false)
  127.  
  128. {
  129.  
  130. $ci = curl_init();
  131.  
  132. /* Curl settings */
  133.  
  134. curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  135.  
  136. curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30);
  137.  
  138. curl_setopt($ci, CURLOPT_TIMEOUT, 30);
  139.  
  140. curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
  141.  
  142.  
  143. switch ($method) {
  144.  
  145. case 'POST':
  146.  
  147. curl_setopt($ci, CURLOPT_POST, true);
  148.  
  149. if (!empty($postfields)) {
  150.  
  151. curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
  152.  
  153. $this->postdata = $postfields;
  154.  
  155. }
  156.  
  157. break;
  158.  
  159. }
  160.  
  161. curl_setopt($ci, CURLOPT_URL, $url);
  162.  
  163. curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
  164.  
  165. curl_setopt($ci, CURLINFO_HEADER_OUT, true);
  166.  
  167.  
  168. $response = curl_exec($ci);
  169.  
  170. $http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
  171.  
  172.  
  173. if ($debug) {
  174.  
  175. echo "=====post data======\r\n";
  176.  
  177. var_dump($postfields);
  178.  
  179.  
  180. echo '=====info=====' . "\r\n";
  181.  
  182. print_r(curl_getinfo($ci));
  183.  
  184.  
  185. echo '=====$response=====' . "\r\n";
  186.  
  187. print_r($response);
  188.  
  189. }
  190.  
  191. curl_close($ci);
  192.  
  193. return array($http_code, $response);
  194.  
  195. }
  196.  
  197.  
  198. }
  199.  
  200. ?>

getOpenid($turl)这个方法就是获取openid的方法。前端调用代码如下:

  1. $openid=isset($_COOKIE['openid'])?$_COOKIE['openid']:'';
  2.  
  3.  
  4. if(empty($openid))
  5.  
  6. {
  7.  
  8. $wchat=new wchat();
  9.  
  10. $t_url='http://'.$_SERVER['HTTP_HOST'].'/user.php?act=register';
  11.  
  12.  
  13. $info=$wchat->getOpenid($t_url);
  14.  
  15.  
  16. if($info){
  17.  
  18. $openid=$info['openid'];
  19.  
  20. setcookie('openid',$openid,time()+86400*30);
  21.  
  22.  
  23. }
  24.  
  25.  
  26. }

以上就是我总结的获取openid的方法啦。

以上就是php获取微信openid的详细内容,更多请关注w3xue其它相关文章!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号