wx4.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. define("TOKEN", "k8h5qu8znxxxxxxjazm76"); //填写自己的token,跟小程序后台填的一样即可
  3. //秘钥
  4. //05hkM8UIrrXoSGrJmCdFuU1mIeHDeyLdxTPd21fh5zQ
  5. if (isset($_GET['echostr'])) { //校验服务器地址URL
  6. valid();
  7. } else {
  8. responseMsg();
  9. }
  10. function valid()
  11. {
  12. $echoStr = $_GET["echostr"];
  13. if (checkSignature()) {
  14. header('content-type:text');
  15. echo $echoStr;
  16. exit;
  17. } else {
  18. echo $echoStr . '+++' . TOKEN;
  19. exit;
  20. }
  21. }
  22. function checkSignature()
  23. {
  24. $signature = $_GET["signature"];
  25. $timestamp = $_GET["timestamp"];
  26. $nonce = $_GET["nonce"];
  27. $token = TOKEN;
  28. $tmpArr = array($token, $timestamp, $nonce);
  29. sort($tmpArr, SORT_STRING);
  30. $tmpStr = implode($tmpArr);
  31. $tmpStr = sha1($tmpStr);
  32. if ($tmpStr == $signature) {
  33. return true;
  34. } else {
  35. return false;
  36. }
  37. }
  38. function responseMsg()
  39. {
  40. // $imgurl = "/upload/20200901/71c6b50cac917f080c2adb7d98817813.png"; //公众号二维码,相对路径,修改为自己的
  41. // $media_id = getMediaId($imgurl); //获取图片消息的media_id
  42. // print_r($media_id);die;
  43. $postStr = file_get_contents('php://input'); //此处推荐使用file_get_contents('php://input')获取后台post过来的数据
  44. if (!empty($postStr) && is_string($postStr)) {
  45. $postArr = json_decode($postStr, true);
  46. if (!empty($postArr['MsgType']) && $postArr['Content'] == "1") { //用户发送1,回复公众号二维码
  47. $fromUsername = $postArr['FromUserName']; //发送者openid
  48. $imgurl = "/upload/20200905/2320bba10b60c48a41156e281828610a.png"; //公众号二维码,相对路径,修改为自己的
  49. $media_id = getMediaId($imgurl); //获取图片消息的media_id
  50. $data = array(
  51. "touser" => $fromUsername,
  52. "msgtype" => "image",
  53. "image" => array("media_id" => $media_id)
  54. );
  55. $json = json_encode($data, JSON_UNESCAPED_UNICODE); //php5.4+
  56. requestAPI($json);
  57. } elseif (!empty($postArr['MsgType']) && $postArr['Content'] == "2") { //用户发送2,回复图文链接
  58. $fromUsername = $postArr['FromUserName']; //发送者openid
  59. $data = array(
  60. "touser" => $fromUsername,
  61. "msgtype" => "link",
  62. "link" => array( //修改下面几项为自己的
  63. "title" => '最火壁纸',
  64. "description" => '2020最火手机壁纸',
  65. "url" => 'https://bizhi.tarfar.com/',
  66. "thumb_url" => 'https://bizhi.tarfar.com/wp-content/uploads/2020/03/logo.png',
  67. )
  68. );
  69. $json = json_encode($data, JSON_UNESCAPED_UNICODE); //php5.4+
  70. requestAPI($json);
  71. } elseif (!empty($postArr['MsgType']) && $postArr['Content'] == "3") { //用户发送3,回复文字
  72. $fromUsername = $postArr['FromUserName']; //发送者openid
  73. $content = '你好,回复1关注公众号,回复2获取官网链接'; //修改为自己需要的文字
  74. $data = array(
  75. "touser" => $fromUsername,
  76. "msgtype" => "text",
  77. "text" => array("content" => $content)
  78. );
  79. $json = json_encode($data, JSON_UNESCAPED_UNICODE); //php5.4+
  80. requestAPI($json);
  81. } elseif (!empty($postArr['MsgType']) && $postArr['MsgType'] == 'image') { //用户发送图片,这里示例为回复他公众号二维码
  82. $fromUsername = $postArr['FromUserName']; //发送者openid
  83. $imgurl = "/300-300.png"; //公众号二维码,相对路径,修改为自己的
  84. $media_id = getMediaId($imgurl); //获取图片消息的media_id
  85. $data = array(
  86. "touser" => $fromUsername,
  87. "msgtype" => "image",
  88. "image" => array("media_id" => $media_id)
  89. );
  90. $json = json_encode($data, JSON_UNESCAPED_UNICODE); //php5.4以上版本才可使用
  91. requestAPI($json);
  92. } elseif ($postArr['MsgType'] == 'event' && $postArr['Event'] == 'user_enter_tempsession') { //用户进入客服后马上回复,现在已失效,需要用户先发过消息
  93. $fromUsername = $postArr['FromUserName']; //发送者openid
  94. $imgurl = "/upload/20200905/2320bba10b60c48a41156e281828610a.png"; //公众号二维码,相对路径,修改为自己的
  95. $media_id = getMediaId($imgurl); //获取图片消息的media_id
  96. $data = array(
  97. "touser" => $fromUsername,
  98. "msgtype" => "image",
  99. "image" => array("media_id" => $media_id)
  100. );
  101. $json = json_encode($data, JSON_UNESCAPED_UNICODE); //php5.4+
  102. requestAPI($json);
  103. } elseif ($postArr['MsgType'] !== 'event') { //用户发送其他内容,引导加客服
  104. $fromUsername = $postArr['FromUserName']; //发送者openid
  105. $imgurl = "/miniapp300-300.png"; //客服微信二维码,相对路径
  106. $media_id = getMediaId($imgurl); //获取图片消息的media_id
  107. $data = array(
  108. "touser" => $fromUsername,
  109. "msgtype" => "image",
  110. "image" => array("media_id" => $media_id)
  111. );
  112. $json = json_encode($data, JSON_UNESCAPED_UNICODE); //php5.4+
  113. requestAPI($json);
  114. } else {
  115. exit;
  116. }
  117. } else {
  118. echo "empty";
  119. exit;
  120. }
  121. }
  122. function requestAPI($json)
  123. {
  124. $access_token = get_accessToken();
  125. /*
  126. * POST发送https请求客服接口api
  127. */
  128. $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" . $access_token;
  129. //以'json'格式发送post的https请求
  130. $curl = curl_init();
  131. curl_setopt($curl, CURLOPT_URL, $url);
  132. curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
  133. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  134. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
  135. if (!empty($json)) {
  136. curl_setopt($curl, CURLOPT_POSTFIELDS, $json);
  137. }
  138. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  139. //curl_setopt($curl, CURLOPT_HTTPHEADER, $headers );
  140. $output = curl_exec($curl);
  141. if (curl_errno($curl)) {
  142. echo 'Errno' . curl_error($curl);//捕抓异常
  143. }
  144. curl_close($curl);
  145. if ($output == 0) {
  146. echo 'success';
  147. exit;
  148. }
  149. }
  150. /* 调用微信api,获取access_token,有效期7200s*/
  151. function get_accessToken()
  152. {
  153. $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx3cfbc521f6537439&secret=c2ed0c450d8071eae35e683a054f6049'; //替换成自己的小程序id和secret
  154. @$weixin = file_get_contents($url);
  155. @$jsondecode = json_decode($weixin);
  156. @$array = get_object_vars($jsondecode);
  157. $token = $array['access_token'];
  158. return $token;
  159. }
  160. //获取上传图片的media_id
  161. function getMediaId($imgurl)
  162. {
  163. $token = get_accessToken();
  164. $url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token={$token}&type=image";
  165. // echo $url;
  166. $ch1 = curl_init();
  167. $timeout = 10;
  168. $real_path = "{$_SERVER['DOCUMENT_ROOT']}$imgurl";//自动转成图片文件绝对路径,如果图片发送失败,检查PHP的$_SERVER['DOCUMENT_ROOT'的配置
  169. // echo $real_path;
  170. $data = array("media" => new CURLFile("{$real_path}"));//php5.6(含)以上版本使用此方法
  171. // print_r($data);
  172. curl_setopt($ch1, CURLOPT_URL, $url);
  173. curl_setopt($ch1, CURLOPT_POST, 1);
  174. curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
  175. curl_setopt($ch1, CURLOPT_CONNECTTIMEOUT, $timeout);
  176. curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, FALSE);
  177. curl_setopt($ch1, CURLOPT_SSL_VERIFYHOST, false);
  178. curl_setopt($ch1, CURLOPT_POSTFIELDS, $data);
  179. $result = curl_exec($ch1);
  180. //print_r( $result);
  181. curl_close($ch1);
  182. if ($result) {
  183. $result = json_decode($result, true);
  184. return $result['media_id'];
  185. } else {
  186. return null;
  187. }
  188. }