weixina.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. public $error="";
  28. /**
  29. * 构造函数
  30. */
  31. public function __construct($info = null) {
  32. if(empty($info)) $info = config('weixin');
  33. $this->appid = $info['APPID'];
  34. $this->secret = $info['APPSECRET'];
  35. $this->redirect_uri = $info['REDIRECT'] ;// config() .'api/weixin/result';
  36. }
  37. /**
  38. * 新版授权登录
  39. * @param type $jsCode
  40. */
  41. public function auth_code2Session($jsCode){
  42. $oauth_url = "https://api.weixin.qq.com/sns/jscode2session?"
  43. ."appid=".$this->appid
  44. ."&secret=".$this->secret
  45. ."&js_code=".$jsCode
  46. ."&grant_type=authorization_code";
  47. $data = $this->Get($oauth_url);
  48. if(empty($data)){
  49. return false;
  50. }
  51. return json_decode($data,true);
  52. }
  53. /**
  54. * 新版获取微信手机号
  55. * @param type $jsCode
  56. */
  57. public function getPhoneNumber($jsCode){
  58. $accessData = $this->getAccessToken();
  59. if(empty($accessData) || empty($accessData["access_token"])){
  60. return false;
  61. }
  62. $access_token = $accessData["access_token"];
  63. $url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?"
  64. ."access_token=".$access_token;
  65. $postData = json_encode(array(
  66. "code"=>$jsCode
  67. ));
  68. $data = $this->Post($postData,$url);
  69. $res = $data;
  70. if(empty($data)){
  71. return false;
  72. }
  73. $data = json_decode($data,true);
  74. if(empty($data) || empty($data["phone_info"]) || empty($data["phone_info"]["purePhoneNumber"])){
  75. $this->error="返回参数名称错误".$res;
  76. return false;
  77. }
  78. return $data["phone_info"];
  79. }
  80. /**
  81. * 新版获取接口调用凭证
  82. * @return type
  83. */
  84. public function getAccessToken(){
  85. $url = "https://api.weixin.qq.com/cgi-bin/token?"
  86. ."appid=".$this->appid
  87. ."&secret=".$this->secret
  88. ."&grant_type=client_credential";
  89. $data = $this->Get($url);
  90. if(empty($data)){
  91. return false;
  92. }
  93. return json_decode($data,true);
  94. }
  95. /**
  96. * 手机授权登录
  97. * @param string $state 状态值
  98. * @param string $scope 作用域
  99. */
  100. public function oauth($state = '', $scope = ''){
  101. if($state == '') $state = $this->state;
  102. if($scope == '') $scope = $this->scope;
  103. $oauth_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$this->appid
  104. ."&redirect_uri=".$this->redirect_uri
  105. ."&response_type=".$this->response_type
  106. ."&scope=".$scope.
  107. "&state=".$state."#wechat_redirect";
  108. return redirect($oauth_url);
  109. }
  110. /**
  111. * 处理事情
  112. */
  113. public function oauth_reuslt($code){
  114. $oauth_reuslt_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$this->appid
  115. ."&secret=".$this->secret
  116. ."&code=".$code
  117. ."&grant_type=authorization_code";
  118. $data = $this->Post("",$oauth_reuslt_url);
  119. return json_decode($data,true);
  120. }
  121. /**
  122. * 拉取用户信息(需scope为 snsapi_userinfo)
  123. *
  124. * openid 用户的唯一标识
  125. nickname 用户昵称
  126. sex 用户的性别,值为1时是男性,值为2时是女性,值为0时是未知
  127. province 用户个人资料填写的省份
  128. city 普通用户个人资料填写的城市
  129. country 国家,如中国为CN
  130. headimgurl 用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空。若用户更换头像,原有头像URL将失效。
  131. privilege 用户特权信息,json 数组,如微信沃卡用户为(chinaunicom)
  132. unionid 只有在用户将公众号绑定到微信开放平台帐号后,才会出现该字段。详见:获取用户个人信息(UnionID机制)
  133. */
  134. public function userinfo($access_token){
  135. $userinfo_url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$this->appid."&lang=zh_CN";
  136. $data = $this->Post("",$userinfo_url);
  137. return json_decode($data,true);
  138. }
  139. /**
  140. * 检验授权凭证(access_token)是否有效
  141. * @param $access_token
  142. */
  143. public function auth($access_token,$openid){
  144. $r = false;
  145. $auth_url = "https://api.weixin.qq.com/sns/auth?access_token=&openid=".$openid;
  146. $data = $this->Post("",$auth_url);
  147. $d = json_decode($data,true);
  148. if($d['errcode'] == 0){
  149. $r = true;
  150. }
  151. return $r;
  152. }
  153. /**
  154. * 写入值
  155. * @param $curlPost
  156. * @param $url
  157. * @return mixed
  158. */
  159. function Post($curlPost, $url) {
  160. $curl = curl_init();
  161. curl_setopt($curl, CURLOPT_URL, $url);
  162. curl_setopt($curl, CURLOPT_HEADER, false);
  163. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  164. curl_setopt($curl, CURLOPT_POST, true);
  165. curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
  166. $return_str = curl_exec($curl);
  167. $err = "";
  168. if(empty($return_str)){
  169. $err = "系统错误:".curl_error($curl);
  170. }
  171. curl_close($curl);
  172. if(!empty($err)){
  173. $this->error = $err;
  174. return false;
  175. }
  176. return $return_str;
  177. }
  178. /**
  179. * 写入值
  180. * @param $url
  181. * @return mixed
  182. */
  183. function Get($url) {
  184. $curl = curl_init();
  185. curl_setopt($curl, CURLOPT_URL, $url);
  186. curl_setopt($curl, CURLOPT_HEADER, false);
  187. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  188. $return_str = curl_exec($curl);
  189. $err = "";
  190. if(empty($return_str)){
  191. $err = "系统错误:".curl_error($curl);
  192. }
  193. curl_close($curl);
  194. if(!empty($err)){
  195. $this->error = $err;
  196. return false;
  197. }
  198. return $return_str;
  199. }
  200. /**
  201. * 写入日志
  202. * @param type $word
  203. * @param type $name
  204. */
  205. function log_result($word, $name) {
  206. $fp = fopen($name . "_log.txt", "a");
  207. flock($fp, LOCK_EX);
  208. fwrite($fp, $word . "\n");
  209. flock($fp, LOCK_UN);
  210. fclose($fp);
  211. }
  212. }