WxMiniServer.class.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace Common\Server;
  3. class WxMiniServer
  4. {
  5. private $appId = '';
  6. private $appSecret = '';
  7. /**
  8. * 初始化配置
  9. * @param $appId
  10. * @param $appSecret
  11. */
  12. public function __construct($appId, $appSecret) {
  13. $this->appId = $appId;
  14. $this->appSecret = $appSecret;
  15. }
  16. /**
  17. * 获取微信用户信息
  18. * @param $js_code
  19. * @return type
  20. */
  21. public function get_user_info($js_code) {
  22. $url = 'https://api.weixin.qq.com/sns/jscode2session';
  23. $data = array(
  24. 'appid' => $this->appId,
  25. 'secret' => $this->appSecret,
  26. 'js_code' => $js_code,
  27. 'grant_type' => 'authorization_code',
  28. );
  29. $user_info = $this->sendGet($url, $data);
  30. return $user_info;
  31. }
  32. /**
  33. * 发送模板消息
  34. * @param $openid
  35. * @param $form_id
  36. * @param $message_data
  37. * @return type
  38. */
  39. public function set_message($openid, $form_id, $message_data) {
  40. $appname = \Common\Common\share\WxShareServer::APPNAME;
  41. $wx_share_server = new \Common\Common\share\WxShareServer($this->appId, $this->appSecret, $appname);
  42. $access_token = $wx_share_server->getAccessToken();
  43. $url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=" . $access_token;
  44. $data = array(
  45. 'touser' => $openid,
  46. 'template_id' => MINI_APP_SIGN_EVENT,
  47. 'form_id' => $form_id,
  48. 'data' => $message_data,
  49. );
  50. $data_json = array();
  51. $data_json['data'] = json_encode($data);
  52. $result = $this->sendPost($url, $data_json);
  53. return $result;
  54. }
  55. /**
  56. * 发送get请求
  57. * @param type $url
  58. * @param type $data
  59. * @return type 失败返回false
  60. */
  61. protected function sendGet($url, $data = null) {
  62. $status = false;
  63. if (empty($url)) {
  64. echo '亲,URL都没有,你要GET到哪去?';
  65. $status = false;
  66. return $status;
  67. }
  68. if (!empty($data)) {
  69. $get_data = '';
  70. foreach ($data as $k => $v) {
  71. $get_data .= "{$k}={$v}&";
  72. }
  73. $get_data = trim($get_data, '&');
  74. }
  75. if (!empty($get_data)) {
  76. $url .= '?' . $get_data;
  77. }
  78. $result = file_get_contents($url);
  79. if (!empty($result)) {
  80. $result = $this->jsonDecode($result);
  81. }
  82. return $result;
  83. }
  84. /**
  85. *
  86. * @param type $json
  87. * @return boolean
  88. */
  89. protected function jsonDecode($json) {
  90. if (empty($json)) {
  91. echo '亲,json都没有,你想解析个蛋?';
  92. $status = false;
  93. return $status;
  94. }
  95. $json = strval($json);
  96. $arr = json_decode(trim($json, chr(239) . chr(187) . chr(191)), true); //删除bom头
  97. return $arr;
  98. }
  99. /**
  100. * 发送post请求
  101. * @param type $url
  102. * @param type $data
  103. * @return type
  104. */
  105. protected function sendPost($url, $data = null) {
  106. $post_data = implode('&', $data);
  107. $ch = curl_init();
  108. curl_setopt($ch, CURLOPT_POST, 1);
  109. curl_setopt($ch, CURLOPT_URL, $url);
  110. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  111. ob_start();
  112. curl_exec($ch);
  113. $result = ob_get_contents();
  114. ob_end_clean();
  115. return $result;
  116. }
  117. }