WeChat.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/4/10 0010
  6. * Time: 上午 10:34
  7. */
  8. // 字符编码
  9. header("Content-Type:text/html; charset=utf-8");
  10. // 微信接口类
  11. class WeChat
  12. {
  13. private static $appid;
  14. private static $appsecret;
  15. function __construct(){
  16. self::$appid = 'wx41e478e592f9e04a'; // 开发者ID(AppID)
  17. self::$appsecret = 'fc165de4aa5cd24b628baa30b100e146'; // 开发者密码(AppSecret)
  18. }
  19. // 微信授权地址
  20. public static function getAuthorizeUrl($url){
  21. $url_link = urlencode($url);
  22. return "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . self::$appid . "&redirect_uri={$url_link}&response_type=code&scope=snsapi_base&state=1#wechat_redirect";
  23. }
  24. // 获取TOKEN
  25. public static function getToken(){
  26. $urla = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . self::$appid . "&secret=" . self::$appsecret;
  27. $outputa = self::curlGet($urla);
  28. $result = json_decode($outputa, true);
  29. return $result['access_token'];
  30. }
  31. /**
  32. * getUserInfo 获取用户信息
  33. * @param string $code 微信授权code
  34. * @param string $weiwei_token Token
  35. * @return array
  36. */
  37. public static function getUserInfo($code, $weiwei_token){
  38. $access_token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . self::$appid . "&secret=" . self::$appsecret . "&code={$code}&grant_type=authorization_code";
  39. $access_token_json = self::curlGet($access_token_url);
  40. $access_token_array = json_decode($access_token_json, true);
  41. $openid = $access_token_array['openid'];
  42. $new_access_token = $weiwei_token;
  43. //全局access token获得用户基本信息
  44. $userinfo_url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token={$new_access_token}&openid={$openid}";
  45. $userinfo_json = self::curlGet($userinfo_url);
  46. $userinfo_array = json_decode($userinfo_json, true);
  47. return $userinfo_array;
  48. }
  49. /**
  50. * pushMessage 发送自定义的模板消息
  51. * @param array $data 模板数据
  52. $data = [
  53. 'openid' => '', 用户openid
  54. 'url' => '', 跳转链接
  55. 'template_id' => '', 模板id
  56. 'data' => [ // 消息模板数据
  57. 'first' => ['value' => urlencode('黄旭辉'),'color' => "#743A3A"],
  58. 'keyword1' => ['value' => urlencode('男'),'color'=>'blue'],
  59. 'keyword2' => ['value' => urlencode('1993-10-23'),'color' => 'blue'],
  60. 'remark' => ['value' => urlencode('我的模板'),'color' => '#743A3A']
  61. ]
  62. ];
  63. * @param string $topcolor 模板内容字体颜色,不填默认为黑色
  64. * @return array
  65. */
  66. public static function pushMessage($data = [],$topcolor = '#0000'){
  67. $template = [
  68. 'touser' => $data['openid'],
  69. 'template_id' => $data['template_id'],
  70. 'url' => $data['url'],
  71. 'topcolor' => $topcolor,
  72. 'data' => $data['data']
  73. ];
  74. $json_template = json_encode($template);
  75. $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . self::getToken();
  76. $result = self::curlPost($url, urldecode($json_template));
  77. $resultData = json_decode($result, true);
  78. return $resultData;
  79. }
  80. /**
  81. * addLog 日志记录
  82. * @param string $log_content 日志内容
  83. */
  84. public static function addLog($log_content = ''){
  85. $data = "";
  86. $data .= "DATE: [ " . date('Y-m-d H:i:s') . " ]\r\n";
  87. $data .= "INFO: " . $log_content . "\r\n\r\n";
  88. file_put_contents('/wechat.log', $data, FILE_APPEND);
  89. }
  90. /**
  91. * 发送get请求
  92. * @param string $url 链接
  93. * @return bool|mixed
  94. */
  95. private static function curlGet($url){
  96. $curl = curl_init();
  97. curl_setopt($curl, CURLOPT_URL, $url);
  98. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  99. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
  100. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  101. $output = curl_exec($curl);
  102. if(curl_errno($curl)){
  103. return 'ERROR ' . curl_error($curl);
  104. }
  105. curl_close($curl);
  106. return $output;
  107. }
  108. /**
  109. * 发送post请求
  110. * @param string $url 链接
  111. * @param string $data 数据
  112. * @return bool|mixed
  113. */
  114. private static function curlPost($url, $data = null){
  115. $curl = curl_init();
  116. curl_setopt($curl, CURLOPT_URL, $url);
  117. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  118. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  119. if(!empty($data)){
  120. curl_setopt($curl, CURLOPT_POST, 1);
  121. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  122. }
  123. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  124. $output = curl_exec($curl);
  125. curl_close($curl);
  126. return $output;
  127. }
  128. }