weixina.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. declare (strict_types=1);
  3. namespace library\lib;
  4. // +----------------------------------------------------------------------
  5. // | [ WE CAN DO IT MORE SIMPLE ]
  6. // +----------------------------------------------------------------------
  7. // | Copyright (c) 2018-2020 rights reserved.
  8. // +----------------------------------------------------------------------
  9. // | Author: TABLE ME
  10. // +----------------------------------------------------------------------
  11. // | Date: 2020-11-10 13:21
  12. // +----------------------------------------------------------------------
  13. class weixina {
  14. //AppID(应用ID)
  15. private $appid = '';
  16. //AppSecret(应用密钥)
  17. private $secret = "";
  18. //授权回调URL
  19. private $redirect_uri = '';
  20. //返回类型,请填写code
  21. private $response_type = 'code';
  22. /**
  23. * 应用授权作用域,snsapi_base (不弹出授权页面,直接跳转,只能获取用户openid),snsapi_userinfo (弹出授权页面,可通过openid拿到昵称、性别、所在地。并且,即使在未关注的情况下,只要用户授权,也能获取其信息
  24. */
  25. private $scope ='snsapi_userinfo';
  26. private $state = 'STATE';
  27. /**
  28. * 构造函数
  29. */
  30. public function __construct($info = null) {
  31. if(empty($info)) $info = config('weixin');
  32. $this->appid = $info['APPID'];
  33. $this->secret = $info['APPSECRET'];
  34. $this->redirect_uri = $info['REDIRECT'] ;// config() .'api/weixin/result';
  35. }
  36. /**
  37. * 手机授权登录
  38. * @param string $state 状态值
  39. * @param string $scope 作用域
  40. */
  41. public function oauth($state = '', $scope = ''){
  42. if($state == '') $state = $this->state;
  43. if($scope == '') $scope = $this->scope;
  44. $oauth_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$this->appid
  45. ."&redirect_uri=".$this->redirect_uri
  46. ."&response_type=".$this->response_type
  47. ."&scope=".$scope.
  48. "&state=".$state."#wechat_redirect";
  49. return redirect($oauth_url);
  50. }
  51. /**
  52. * 获取access_token
  53. */
  54. public function oauth_reuslt($code){
  55. $oauth_reuslt_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$this->appid
  56. ."&secret=".$this->secret
  57. ."&code=".$code
  58. ."&grant_type=authorization_code";
  59. $data = $this->Post("",$oauth_reuslt_url);
  60. return json_decode($data,true);
  61. }
  62. /**
  63. * 拉取用户信息(需scope为 snsapi_userinfo)
  64. *
  65. * openid 用户的唯一标识
  66. nickname 用户昵称
  67. sex 用户的性别,值为1时是男性,值为2时是女性,值为0时是未知
  68. province 用户个人资料填写的省份
  69. city 普通用户个人资料填写的城市
  70. country 国家,如中国为CN
  71. headimgurl 用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空。若用户更换头像,原有头像URL将失效。
  72. privilege 用户特权信息,json 数组,如微信沃卡用户为(chinaunicom)
  73. unionid 只有在用户将公众号绑定到微信开放平台帐号后,才会出现该字段。详见:获取用户个人信息(UnionID机制)
  74. */
  75. public function userinfo($access_token){
  76. $userinfo_url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$this->appid."&lang=zh_CN";
  77. $data = $this->Post("",$userinfo_url);
  78. return json_decode($data,true);
  79. }
  80. /**
  81. * 检验授权凭证(access_token)是否有效
  82. * @param $access_token
  83. */
  84. public function auth($access_token,$openid){
  85. $r = false;
  86. $auth_url = "https://api.weixin.qq.com/sns/auth?access_token=&openid=".$openid;
  87. $data = $this->Post("",$auth_url);
  88. $d = json_decode($data,true);
  89. if($d['errcode'] == 0){
  90. $r = true;
  91. }
  92. return $r;
  93. }
  94. /**
  95. * 写入值
  96. * @param $curlPost
  97. * @param $url
  98. * @return mixed
  99. */
  100. function Post($curlPost, $url) {
  101. $curl = curl_init();
  102. curl_setopt($curl, CURLOPT_URL, $url);
  103. curl_setopt($curl, CURLOPT_HEADER, false);
  104. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  105. curl_setopt($curl, CURLOPT_NOBODY, true);
  106. curl_setopt($curl, CURLOPT_POST, true);
  107. curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
  108. $return_str = curl_exec($curl);
  109. curl_close($curl);
  110. return $return_str;
  111. }
  112. /**
  113. * 写入日志
  114. * @param type $word
  115. * @param type $name
  116. */
  117. function log_result($word, $name) {
  118. $fp = fopen($name . "_log.txt", "a");
  119. flock($fp, LOCK_EX);
  120. fwrite($fp, $word . "\n");
  121. flock($fp, LOCK_UN);
  122. fclose($fp);
  123. }
  124. }