1. <?php
  2. ini_set ( 'date.timezone', 'Asia/Shanghai' );
  3. define ( 'IN_ASK2', TRUE );
  4. $http_type = ((isset ( $_SERVER ['HTTPS'] ) && $_SERVER ['HTTPS'] == 'on') || (isset ( $_SERVER ['HTTP_X_FORWARDED_PROTO'] ) && $_SERVER ['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'https://';
  5. if (is_https ()) {
  6. define ( 'SITE_URL', 'https://' . $_SERVER ['HTTP_HOST'] . '/' );
  7. } else {
  8. define ( 'SITE_URL', 'http://' . $_SERVER ['HTTP_HOST'] . '/' );
  9. }
  10.  
  11. define ( 'ENVIRONMENT', isset ( $_SERVER ['CI_ENV'] ) ? $_SERVER ['CI_ENV'] : 'production' );
  12. define ( 'FC_PATH', dirname ( dirname ( dirname ( __FILE__ ) ) ) . DIRECTORY_SEPARATOR );
  13. define ( 'BASEPATH', FC_PATH . 'system' );
  14. require FC_PATH . 'lib/db_mysqli.php';
  15. $wechatObj = new wechatCallbackapiTest ();
  16.  
  17. if (! isset ( $_GET ['echostr'] )) {
  18. $wechatObj->responseMsg ();
  19. } else {
  20. $wechatObj->valid ();
  21. }
  22. //判断是否是https
  23. function is_https() {
  24. if (! empty ( $_SERVER ['HTTPS'] ) && strtolower ( $_SERVER ['HTTPS'] ) !== 'off') {
  25. return TRUE;
  26. } elseif (isset ( $_SERVER ['HTTP_X_FORWARDED_PROTO'] ) && $_SERVER ['HTTP_X_FORWARDED_PROTO'] === 'https') {
  27. return TRUE;
  28. } elseif (! empty ( $_SERVER ['HTTP_FRONT_END_HTTPS'] ) && strtolower ( $_SERVER ['HTTP_FRONT_END_HTTPS'] ) !== 'off') {
  29. return TRUE;
  30. }
  31.  
  32. return FALSE;
  33. }
  34. class wechatCallbackapiTest {
  35. var $db;
  36. var $token;
  37. function wechatCallbackapiTest() {
  38.  
  39. $this->init_db ();
  40.  
  41. $this->token = $this->getoken ();
  42. }
  43. function init_db() {
  44. require FC_PATH . 'application' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'database.php';
  45. $dbconfig = $db ['default'];
  46. $db = new db ();
  47. define ( 'DB_TABLEPRE', $dbconfig ['dbprefix'] );
  48. $config = array ();
  49. $config ['hostname'] = $dbconfig ['hostname'];
  50. $config ['username'] = $dbconfig ['username'];
  51. $config ['password'] = $dbconfig ['password'];
  52. $config ['database'] = $dbconfig ['database'];
  53. $config ['charset'] = $dbconfig ['char_set'];
  54. $config ['autoconnect'] = 1;
  55. $config ['dbport'] = 3306;
  56. $config ['debug'] = true;
  57. $db->open ( $config );
  58. $this->db = $db;
  59. }
  60. function getoken() {
  61. $wxtoken = $this->db->fetch_first ( "SELECT * FROM " . DB_TABLEPRE . "setting where k='wxtoken' limit 0,1" );
  62. return trim ( $wxtoken ['v'] );
  63. }
  64. //验证签名
  65. public function valid() {
  66.  
  67. $echoStr = $_GET ["echostr"];
  68. $signature = $_GET ["signature"];
  69. $timestamp = $_GET ["timestamp"];
  70. $nonce = $_GET ["nonce"];
  71. $token = $this->token;
  72. $tmpArr = array ($token, $timestamp, $nonce );
  73. sort ( $tmpArr, SORT_STRING );
  74. $tmpStr = implode ( $tmpArr );
  75. $tmpStr = sha1 ( $tmpStr );
  76. if ($tmpStr == $signature) {
  77. echo $echoStr;
  78. exit ();
  79. }
  80. }
  81.  
  82. //响应消息
  83. public function responseMsg() {
  84. $postStr = $GLOBALS ["HTTP_RAW_POST_DATA"]? $GLOBALS ["HTTP_RAW_POST_DATA"]:file_get_contents('php://input');
  85. if (! empty ( $postStr )) {
  86. $this->logger ( "R \r\n" . $postStr );
  87. $postObj = simplexml_load_string ( $postStr, 'SimpleXMLElement', LIBXML_NOCDATA );
  88. $RX_TYPE = trim ( $postObj->MsgType );
  89. if (($postObj->MsgType == "event") && ($postObj->Event == "subscribe" || $postObj->Event == "unsubscribe")) {
  90. //过滤关注和取消关注事件
  91. } else {
  92.  
  93. }
  94.  
  95. //消息类型分离
  96. switch ($RX_TYPE) {
  97. case "event" :
  98. $result = $this->receiveEvent ( $postObj );
  99. break;
  100. case "text" :
  101.  
  102. $result = $this->receiveText ( $postObj );
  103.  
  104. break;
  105. case "image" :
  106. $result = $this->receiveImage ( $postObj );
  107. break;
  108. case "location" :
  109. $result = $this->receiveLocation ( $postObj );
  110. break;
  111. case "voice" :
  112. $result = $this->receiveVoice ( $postObj );
  113. break;
  114. case "video" :
  115. $result = $this->receiveVideo ( $postObj );
  116. break;
  117. case "link" :
  118. $result = $this->receiveLink ( $postObj );
  119. break;
  120. default :
  121. $result = "unknown msg type: " . $RX_TYPE;
  122. break;
  123. }
  124. $this->logger ( "T \r\n" . $result );
  125.  
  126. echo $result;
  127. } else {
  128. echo "";
  129. exit ();
  130. }
  131. }
  132.  
  133. //接收事件消息
  134. private function receiveEvent($object) {
  135. $content = "";
  136. switch ($object->Event) {
  137. case "subscribe" :
  138. $site = $this->db->fetch_first ( "SELECT * FROM " . DB_TABLEPRE . "weixin_info limit 0,1" );
  139. $content = $site ['msg'];
  140. $content .= (! empty ( $object->EventKey )) ? ("\n来自二维码场景 " . str_replace ( "qrscene_", "", $object->EventKey )) : "";
  141. break;
  142. case "unsubscribe" :
  143. $content = "取消关注";
  144. break;
  145. case "CLICK" :
  146. $content = $this->getcontent ( $object->EventKey );
  147. break;
  148. case "VIEW" :
  149. $content = "跳转链接 " . $object->EventKey;
  150. break;
  151. case "SCAN" :
  152. $content = "扫描场景 " . $object->EventKey;
  153. break;
  154. case "LOCATION" :
  155. // $content = "上传位置:纬度 ".$object->Latitude.";经度 ".$object->Longitude;
  156. break;
  157. case "scancode_waitmsg" :
  158. if ($object->ScanCodeInfo->ScanType == "qrcode") {
  159. $content = "扫码带提示:类型 二维码 结果:" . $object->ScanCodeInfo->ScanResult;
  160. } else if ($object->ScanCodeInfo->ScanType == "barcode") {
  161. $codeinfo = explode ( ",", strval ( $object->ScanCodeInfo->ScanResult ) );
  162. $codeValue = $codeinfo [1];
  163. $content = "扫码带提示:类型 条形码 结果:" . $codeValue;
  164. } else {
  165. $content = "扫码带提示:类型 " . $object->ScanCodeInfo->ScanType . " 结果:" . $object->ScanCodeInfo->ScanResult;
  166. }
  167. break;
  168. case "scancode_push" :
  169. $content = "扫码推事件";
  170. break;
  171. case "pic_sysphoto" :
  172. $content = "系统拍照";
  173. break;
  174. case "pic_weixin" :
  175. $content = "相册发图:数量 " . $object->SendPicsInfo->Count;
  176. break;
  177. case "pic_photo_or_album" :
  178. $content = "拍照或者相册:数量 " . $object->SendPicsInfo->Count;
  179. break;
  180. case "location_select" :
  181. $content = "发送位置:标签 " . $object->SendLocationInfo->Label;
  182. break;
  183. default :
  184. // $content = "receive a new event: ".$object->Event;
  185. break;
  186. }
  187.  
  188. if($object->Event=='subscribe'||$object->Event=='SCAN'){
  189. $myopenid = sprintf($object->FromUserName);
  190. $mytoken = sprintf($object->EventKey);
  191. $user = $this->db->fetch_first ( "SELECT * FROM " . DB_TABLEPRE . "user where openid='$myopenid' limit 0,1" );
  192.  
  193. if($object->Event=='SCAN'){
  194. $type=1;
  195. $content = '';
  196. }else{
  197. $content = $site ['msg'];
  198.  
  199. $type=0;
  200. $mytoken = substr($mytoken,-9);
  201. }
  202. if(empty($user)){
  203. $uid=0;
  204. $content .= "请先注册 ";
  205. }else{
  206. $uid=$user['uid'];
  207. if(empty($user['phone'])){
  208. $content .= "请验证手机号";
  209. }else{
  210.  
  211. $content .= "欢迎登陆 ";
  212.  
  213. }
  214.  
  215. }
  216. $time = time();
  217. $this->db->query ( "INSERT INTO " . DB_TABLEPRE . "user_loginjilu(openid,uid,token,type,time) values ('$myopenid','$uid','$mytoken','$type',$time)" );
  218. $infoid = $this->db->insert_id ();
  219. // file_put_contents("text.txt", var_export($infoid,true)."\r\n",FILE_APPEND);
  220. }
  221.  
  222. if (is_array ( $content )) {
  223. if (isset ( $content [0] ['PicUrl'] )) {
  224. $result = $this->transmitNews ( $object, $content );
  225. } else if (isset ( $content ['MusicUrl'] )) {
  226. $result = $this->transmitMusic ( $object, $content );
  227. }
  228. } else {
  229. if ($content == "") {
  230. $site = $this->db->fetch_first ( "SELECT * FROM " . DB_TABLEPRE . "weixin_info limit 0,1" );
  231. $content = $site ['unword'];
  232. }
  233.  
  234. $result = $this->transmitText ( $object, $content );
  235. }
  236.  
  237. return $result;
  238. }
  239.  
  240. private function getcontent($keyword, $object = null) {
  241. // runlog ( 'test', $keyword );
  242. $keys = array ();
  243. $kcontent = array ();
  244. $content = '';
  245. $query = $this->db->query ( "SELECT * FROM " . DB_TABLEPRE . "weixin_keywords order by id desc LIMIT 0,1000" );
  246. while ( $key = $this->db->fetch_array ( $query ) ) {
  247. //精准匹配
  248. if ($key ['showtype'] == 1) {
  249. if ($keyword == $key ['txtname']) {
  250. //系统关键词
  251. if ($key ['txttype'] == 1) {
  252. switch (trim ( $key ['txtcontent'] )) {
  253. case '#最新问题#' :
  254. $content = $this->newquestion ();
  255. if (count ( $content ) <= 0) {
  256. $content = "没有最新问题推荐哟";
  257. }
  258. break;
  259. case '#热门问题#' :
  260. $content = $this->hotquestion ();
  261. if (count ( $content ) <= 0) {
  262. $content = "没有热门问题推荐哟";
  263. }
  264. break;
  265. case '#最新文章#' :
  266. $content = $this->newblog ();
  267. if (count ( $content ) <= 0) {
  268. $content = "没有最新文章推荐哟";
  269. }
  270. break;
  271. case '#站长推荐#' :
  272. $content = $this->hotblog ();
  273. if (count ( $content ) <= 0) {
  274. $content = "没有站长推荐的文章哟";
  275. }
  276. break;
  277. case '#附近的人#' :
  278. break;
  279. case '#附近的问题#' :
  280. break;
  281.  
  282. }
  283. } else {
  284. if (! empty ( $key ['title'] ) && $key ['title'] != '') {
  285.  
  286. $sql = $this->db->query ( "SELECT * FROM " . DB_TABLEPRE . "weixin_keywords where txtname='$keyword' order by id desc LIMIT 0,9" );
  287.  
  288. while ( $topic = $this->db->fetch_array ( $sql ) ) {
  289.  
  290. if (strstr ( $topic ['wburl'], 'http:' )) {
  291. $kcontent [] = array ("Title" => $topic ['title'], "Description" => $topic ['txtcontent'], "PicUrl" => SITE_URL . $topic ['fmtu'], "Url" => $topic ['wburl'] );
  292.  
  293. } else {
  294. $kcontent [] = array ("Title" => $topic ['title'], "Description" => $topic ['txtcontent'], "PicUrl" => SITE_URL . $topic ['fmtu'], "Url" => SITE_URL . 'index.php?topic/getone/' . $topic ['wzid'] . '.html' );
  295.  
  296. }
  297.  
  298. }
  299.  
  300. } else {
  301. $content = $key ['txtcontent'];
  302. }
  303.  
  304. }
  305. if (count ( $kcontent ) > 0) {
  306. $content = $kcontent;
  307. }
  308. break;
  309. }
  310. } else {
  311. //模糊匹配
  312. if (strstr ( $keyword, $key ['txtname'] )) {
  313. if ($key ['txttype'] == 1) {
  314. switch (trim ( $key ['txtcontent'] )) {
  315. case '#最新问题#' :
  316. $content = $this->newquestion ();
  317. if (count ( $content ) <= 0) {
  318. $content = "没有最新问题推荐哟";
  319. }
  320. break;
  321. case '#热门问题#' :
  322. $content = $this->hotquestion ();
  323. if (count ( $content ) <= 0) {
  324. $content = "没有最新问题推荐哟";
  325. }
  326. break;
  327. case '#最新文章#' :
  328. $content = $this->newblog ();
  329. if (count ( $content ) <= 0) {
  330. $content = "没有最新文章推荐哟";
  331. }
  332. break;
  333. case '#站长推荐#' :
  334. $content = $this->hotblog ();
  335. if (count ( $content ) <= 0) {
  336. $content = "没有站长推荐的文章哟";
  337. }
  338. break;
  339. case '#附近的人#' :
  340. break;
  341. case '#附近的问题#' :
  342. break;
  343. }
  344. } else {
  345. if (! empty ( $key ['title'] ) && $key ['title'] != '') {
  346.  
  347. $sql = $this->db->query ( "SELECT * FROM " . DB_TABLEPRE . "weixin_keywords where txtname='$keyword' order by id desc LIMIT 0,9" );
  348.  
  349. while ( $topic = $this->db->fetch_array ( $sql ) ) {
  350. if (strstr ( $topic ['wburl'], 'http:' )) {
  351. $kcontent [] = array ("Title" => $topic ['title'], "Description" => $topic ['txtcontent'], "PicUrl" => SITE_URL . $topic ['fmtu'], "Url" => $topic ['wburl'] );
  352.  
  353. } else {
  354. $kcontent [] = array ("Title" => $topic ['title'], "Description" => $topic ['txtcontent'], "PicUrl" => SITE_URL . $topic ['fmtu'], "Url" => SITE_URL . 'index.php?topic/getone/' . $topic ['wzid'] . '.html' );
  355.  
  356. }
  357. }
  358.  
  359. } else {
  360.  
  361. $content = $key ['txtcontent'];
  362. }
  363. }
  364. if (count ( $kcontent ) > 0) {
  365. $content = $kcontent;
  366. }
  367. break;
  368. }
  369. }
  370. }
  371. if ($keyword == "签到" || $keyword == "打卡") {
  372. $content = "签到记录已经收到";
  373. }
  374. if ($keyword == "账号绑定" || $content == "账号绑定") {
  375. $openid = $object->FromUserName;
  376. $getone = $this->db->fetch_first ( "SELECT * FROM " . DB_TABLEPRE . "user where openid='$openid' limit 0,1" );
  377. if ($getone == null) {
  378. $url = SITE_URL . "index.php?account/bind/$openid";
  379. $content = "<a href='$url'>" . $getone ['username'] . "点击进入账号绑定</a>";
  380. } else {
  381.  
  382. $content = $getone ['username'] . "您已经绑定账号了!";
  383. }
  384.  
  385. }
  386. if ($keyword == 'openid') {
  387. $content = "您的openid:" . $object->FromUserName;
  388. }
  389. return $content;
  390. }
  391. //接收文本消息
  392. private function receiveText($object) {
  393. $keyword = trim ( $object->Content );
  394.  
  395. $content = "";
  396. $content = $this->getcontent ( $keyword, $object );
  397. $type = '';
  398. $firststr = substr ( $keyword, 0, 1 );
  399. $laststr = substr ( $keyword, strlen ( $keyword ) - 1, 1 );
  400. if ($laststr == "#" && $firststr == "#") {
  401. $type = 'topic'; //表示文章检索
  402. }
  403. if ($laststr == "$" && $firststr == "$") {
  404. $type = 'question'; //表示文章检索
  405. }
  406. // //检索内容
  407. switch ($type) {
  408. case 'topic' :
  409. $topickeyword = trim ( $keyword, "#" );
  410. $content = array ();
  411. $query = $this->db->query ( "SELECT * FROM " . DB_TABLEPRE . "topic where title like '%$topickeyword%' order by id desc LIMIT 0,4" );
  412. while ( $topic = $this->db->fetch_array ( $query ) ) {
  413.  
  414. //$topic['viewtime'] = tdate($topic['viewtime']);
  415. $index = strpos ( $topic ['image'], 'http' );
  416. if ($index == 0) {
  417. $content [] = array ("Title" => $topic ['title'], "Description" => "", "PicUrl" => $topic ['image'], "Url" => SITE_URL . 'index.php?topic/getone/' . $topic ['id'] );
  418. } else {
  419. $content [] = array ("Title" => $topic ['title'], "Description" => "", "PicUrl" => SITE_URL . $topic ['image'], "Url" => SITE_URL . 'index.php?topic/getone/' . $topic ['id'] );
  420. }
  421.  
  422. }
  423. break;
  424. case 'question' :
  425. $topickeyword = trim ( $keyword, "$" );
  426. $content = array ();
  427. $query = $this->db->query ( "SELECT * FROM " . DB_TABLEPRE . "question where title like '%$topickeyword%' order by id desc LIMIT 0,4" );
  428. while ( $question = $this->db->fetch_array ( $query ) ) {
  429.  
  430. //$topic['viewtime'] = tdate($topic['viewtime']);
  431. $imgsrc = $question ['description'];
  432. if ($imgsrc == null || $imgsrc == '') {
  433. $imgsrc = SITE_URL . "static/css/default/avatar.gif"; //get_avatar_dir($question['authorid']);
  434. }
  435. $question ['image'] = $imgsrc;
  436.  
  437. $content [] = array ("Title" => $question ['title'], "Description" => "", "PicUrl" => $question ['image'], "Url" => SITE_URL . 'index.php?q-' . $question ['id'] . '.html' );
  438.  
  439. }
  440. break;
  441. }
  442.  
  443. if ($content == "") {
  444. $content = array ();
  445. $query = $this->db->query ( "SELECT * FROM " . DB_TABLEPRE . "question where title like '%$keyword%' order by id desc LIMIT 0,4" );
  446. while ( $question = $this->db->fetch_array ( $query ) ) {
  447.  
  448. //$topic['viewtime'] = tdate($topic['viewtime']);
  449. $imgsrc = $question ['description'];
  450. if ($imgsrc == null || $imgsrc == '') {
  451. $imgsrc = SITE_URL . "static/css/default/avatar.gif"; //get_avatar_dir($question['authorid']);
  452. }
  453. $question ['image'] = $imgsrc;
  454.  
  455. $content [] = array ("Title" => $question ['title'], "Description" => "", "PicUrl" => $question ['image'], "Url" => SITE_URL . 'index.php?q-' . $question ['id'] . '.html' );
  456.  
  457. }
  458. $query = $this->db->query ( "SELECT * FROM " . DB_TABLEPRE . "topic where title like '%$keyword%' order by id desc LIMIT 0,4" );
  459. while ( $topic = $this->db->fetch_array ( $query ) ) {
  460.  
  461. //$topic['viewtime'] = tdate($topic['viewtime']);
  462. $index = strpos ( $topic ['image'], 'http' );
  463. if ($index == 0) {
  464. $content [] = array ("Title" => $topic ['title'], "Description" => "", "PicUrl" => $topic ['image'], "Url" => SITE_URL . 'index.php?topic/getone/' . $topic ['id'] . '.html' );
  465. } else {
  466. $content [] = array ("Title" => $topic ['title'], "Description" => "", "PicUrl" => SITE_URL . $topic ['image'], "Url" => SITE_URL . 'index.php?topic/getone/' . $topic ['id'] . '.html' );
  467. }
  468.  
  469. }
  470.  
  471. if (count ( $content ) == 0) {
  472. $unword = $this->db->fetch_first ( "SELECT * FROM " . DB_TABLEPRE . "setting where k='unword' limit 0,1" );
  473. $_content = trim ( $unword ['v'] );
  474.  
  475. $content = empty ( $_content ) ? "小编不知道你在说啥" : $_content;
  476. }
  477.  
  478. }
  479. // if($content=='我圣诞快乐'){
  480. // @require '../../lib/wxpay/hongbao/pay.php';
  481. //$packet = new Packet();
  482. ////
  483. //////调取支付方法
  484. // $result=$packet->_route('wxpacket',array('openid'=>$object->FromUserName));
  485. ////
  486. // switch ($result){
  487. // case 'SUCCESS':
  488. // $content="圣诞快乐,送了你一个红包,赶快领取吧!";
  489. // break;
  490. // default:
  491. // $content="红包领取失败,再接再励!".$result;
  492. // break;
  493. // }
  494. ////
  495. // }
  496.  
  497. if (is_array ( $content )) {
  498. if (isset ( $content [0] )) {
  499. $result = $this->transmitNews ( $object, $content );
  500. } else if (isset ( $content ['MusicUrl'] )) {
  501. $result = $this->transmitMusic ( $object, $content );
  502. }
  503. } else {
  504. $result = $this->transmitText ( $object, $content );
  505. }
  506. return $result;
  507. }
  508.  
  509. //接收图片消息
  510. private function receiveImage($object) {
  511. $content = array ("MediaId" => $object->MediaId );
  512. $result = $this->transmitImage ( $object, $content );
  513. return $result;
  514. }
  515.  
  516. //接收位置消息
  517. private function receiveLocation($object) {
  518. $content = "你发送的是位置,经度为:" . $object->Location_Y . ";纬度为:" . $object->Location_X . ";缩放级别为:" . $object->Scale . ";位置为:" . $object->Label;
  519. $result = $this->transmitText ( $object, $content );
  520. return $result;
  521. }
  522.  
  523. //接收语音消息
  524. private function receiveVoice($object) {
  525. if (isset ( $object->Recognition ) && ! empty ( $object->Recognition )) {
  526. $content = "你刚才说的是:" . $object->Recognition;
  527. // $object->content=$object->Recognition;
  528. //$this->receiveText($object);
  529. $result = $this->transmitText ( $object, $content );
  530. } else {
  531. $content = array ("MediaId" => $object->MediaId );
  532. $result = $this->transmitVoice ( $object, $content );
  533. }
  534. return $result;
  535. }
  536.  
  537. //接收视频消息
  538. private function receiveVideo($object) {
  539. $content = array ("MediaId" => $object->MediaId, "ThumbMediaId" => $object->ThumbMediaId, "Title" => "", "Description" => "" );
  540. $result = $this->transmitVideo ( $object, $content );
  541. return $result;
  542. }
  543.  
  544. //接收链接消息
  545. private function receiveLink($object) {
  546. $content = "你发送的是链接,标题为:" . $object->Title . ";内容为:" . $object->Description . ";链接地址为:" . $object->Url;
  547. $result = $this->transmitText ( $object, $content );
  548. return $result;
  549. }
  550.  
  551. //回复文本消息
  552. private function transmitText($object, $content) {
  553. if (! isset ( $content ) || empty ( $content )) {
  554. return "";
  555. }
  556.  
  557. $xmlTpl = "<xml>
  558. <ToUserName><![CDATA[%s]]></ToUserName>
  559. <FromUserName><![CDATA[%s]]></FromUserName>
  560. <CreateTime>%s</CreateTime>
  561. <MsgType><![CDATA[text]]></MsgType>
  562. <Content><![CDATA[%s]]></Content>
  563. </xml>";
  564. $result = sprintf ( $xmlTpl, $object->FromUserName, $object->ToUserName, time (), $content );
  565.  
  566. return $result;
  567. }
  568.  
  569. //回复图文消息
  570. private function transmitNews($object, $newsArray) {
  571. if (! is_array ( $newsArray )) {
  572. return "";
  573. }
  574. $itemTpl = " <item>
  575. <Title><![CDATA[%s]]></Title>
  576. <Description><![CDATA[%s]]></Description>
  577. <PicUrl><![CDATA[%s]]></PicUrl>
  578. <Url><![CDATA[%s]]></Url>
  579. </item>
  580. ";
  581. $item_str = "";
  582. foreach ( $newsArray as $item ) {
  583. $item_str .= sprintf ( $itemTpl, $item ['Title'], $item ['Description'], $item ['PicUrl'], $item ['Url'] );
  584. }
  585. $xmlTpl = "<xml>
  586. <ToUserName><![CDATA[%s]]></ToUserName>
  587. <FromUserName><![CDATA[%s]]></FromUserName>
  588. <CreateTime>%s</CreateTime>
  589. <MsgType><![CDATA[news]]></MsgType>
  590. <ArticleCount>%s</ArticleCount>
  591. <Articles>
  592. $item_str </Articles>
  593. </xml>";
  594.  
  595. $result = sprintf ( $xmlTpl, $object->FromUserName, $object->ToUserName, time (), count ( $newsArray ) );
  596. return $result;
  597. }
  598.  
  599. //回复音乐消息
  600. private function transmitMusic($object, $musicArray) {
  601. if (! is_array ( $musicArray )) {
  602. return "";
  603. }
  604. $itemTpl = "<Music>
  605. <Title><![CDATA[%s]]></Title>
  606. <Description><![CDATA[%s]]></Description>
  607. <MusicUrl><![CDATA[%s]]></MusicUrl>
  608. <HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
  609. </Music>";
  610.  
  611. $item_str = sprintf ( $itemTpl, $musicArray ['Title'], $musicArray ['Description'], $musicArray ['MusicUrl'], $musicArray ['HQMusicUrl'] );
  612.  
  613. $xmlTpl = "<xml>
  614. <ToUserName><![CDATA[%s]]></ToUserName>
  615. <FromUserName><![CDATA[%s]]></FromUserName>
  616. <CreateTime>%s</CreateTime>
  617. <MsgType><![CDATA[music]]></MsgType>
  618. $item_str
  619. </xml>";
  620.  
  621. $result = sprintf ( $xmlTpl, $object->FromUserName, $object->ToUserName, time () );
  622. return $result;
  623. }
  624.  
  625. //回复图片消息
  626. private function transmitImage($object, $imageArray) {
  627. $itemTpl = "<Image>
  628. <MediaId><![CDATA[%s]]></MediaId>
  629. </Image>";
  630.  
  631. $item_str = sprintf ( $itemTpl, $imageArray ['MediaId'] );
  632.  
  633. $xmlTpl = "<xml>
  634. <ToUserName><![CDATA[%s]]></ToUserName>
  635. <FromUserName><![CDATA[%s]]></FromUserName>
  636. <CreateTime>%s</CreateTime>
  637. <MsgType><![CDATA[image]]></MsgType>
  638. $item_str
  639. </xml>";
  640.  
  641. $result = sprintf ( $xmlTpl, $object->FromUserName, $object->ToUserName, time () );
  642. return $result;
  643. }
  644.  
  645. //回复语音消息
  646. private function transmitVoice($object, $voiceArray) {
  647. $itemTpl = "<Voice>
  648. <MediaId><![CDATA[%s]]></MediaId>
  649. </Voice>";
  650.  
  651. $item_str = sprintf ( $itemTpl, $voiceArray ['MediaId'] );
  652. $xmlTpl = "<xml>
  653. <ToUserName><![CDATA[%s]]></ToUserName>
  654. <FromUserName><![CDATA[%s]]></FromUserName>
  655. <CreateTime>%s</CreateTime>
  656. <MsgType><![CDATA[voice]]></MsgType>
  657. $item_str
  658. </xml>";
  659.  
  660. $result = sprintf ( $xmlTpl, $object->FromUserName, $object->ToUserName, time () );
  661. return $result;
  662. }
  663.  
  664. //回复视频消息
  665. private function transmitVideo($object, $videoArray) {
  666. $itemTpl = "<Video>
  667. <MediaId><![CDATA[%s]]></MediaId>
  668. <ThumbMediaId><![CDATA[%s]]></ThumbMediaId>
  669. <Title><![CDATA[%s]]></Title>
  670. <Description><![CDATA[%s]]></Description>
  671. </Video>";
  672.  
  673. $item_str = sprintf ( $itemTpl, $videoArray ['MediaId'], $videoArray ['ThumbMediaId'], $videoArray ['Title'], $videoArray ['Description'] );
  674.  
  675. $xmlTpl = "<xml>
  676. <ToUserName><![CDATA[%s]]></ToUserName>
  677. <FromUserName><![CDATA[%s]]></FromUserName>
  678. <CreateTime>%s</CreateTime>
  679. <MsgType><![CDATA[video]]></MsgType>
  680. $item_str
  681. </xml>";
  682.  
  683. $result = sprintf ( $xmlTpl, $object->FromUserName, $object->ToUserName, time () );
  684. return $result;
  685. }
  686.  
  687. //回复多客服消息
  688. private function transmitService($object) {
  689. $xmlTpl = "<xml>
  690. <ToUserName><![CDATA[%s]]></ToUserName>
  691. <FromUserName><![CDATA[%s]]></FromUserName>
  692. <CreateTime>%s</CreateTime>
  693. <MsgType><![CDATA[transfer_customer_service]]></MsgType>
  694. </xml>";
  695. $result = sprintf ( $xmlTpl, $object->FromUserName, $object->ToUserName, time () );
  696. return $result;
  697. }
  698.  
  699. //回复第三方接口消息
  700. private function relayPart3($url, $rawData) {
  701. $headers = array ("Content-Type: text/xml; charset=utf-8" );
  702. $ch = curl_init ();
  703. curl_setopt ( $ch, CURLOPT_URL, $url );
  704. curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
  705. curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
  706. curl_setopt ( $ch, CURLOPT_POST, 1 );
  707. curl_setopt ( $ch, CURLOPT_POSTFIELDS, $rawData );
  708. $output = curl_exec ( $ch );
  709. curl_close ( $ch );
  710. return $output;
  711. }
  712.  
  713. //字节转Emoji表情
  714. function bytes_to_emoji($cp) {
  715. if ($cp > 0x10000) { # 4 bytes
  716. return chr ( 0xF0 | (($cp & 0x1C0000) >> 18) ) . chr ( 0x80 | (($cp & 0x3F000) >> 12) ) . chr ( 0x80 | (($cp & 0xFC0) >> 6) ) . chr ( 0x80 | ($cp & 0x3F) );
  717. } else if ($cp > 0x800) { # 3 bytes
  718. return chr ( 0xE0 | (($cp & 0xF000) >> 12) ) . chr ( 0x80 | (($cp & 0xFC0) >> 6) ) . chr ( 0x80 | ($cp & 0x3F) );
  719. } else if ($cp > 0x80) { # 2 bytes
  720. return chr ( 0xC0 | (($cp & 0x7C0) >> 6) ) . chr ( 0x80 | ($cp & 0x3F) );
  721. } else { # 1 byte
  722. return chr ( $cp );
  723. }
  724. }
  725.  
  726. //日志记录
  727. private function logger($log_content) {
  728. if (isset ( $_SERVER ['HTTP_APPNAME'] )) { //SAE
  729. sae_set_display_errors ( false );
  730. sae_debug ( $log_content );
  731. sae_set_display_errors ( true );
  732. } else if ($_SERVER ['REMOTE_ADDR'] != "127.0.0.1") { //LOCAL
  733. $max_size = 1000000;
  734. $log_filename = "log.xml";
  735. if (file_exists ( $log_filename ) and (abs ( filesize ( $log_filename ) ) > $max_size)) {
  736. unlink ( $log_filename );
  737. }
  738. file_put_contents ( $log_filename, date ( 'Y-m-d H:i:s' ) . " " . $log_content . "\r\n", FILE_APPEND );
  739. }
  740. }
  741. //最新博客
  742. function newblog() {
  743. $content = array ();
  744.  
  745. $newtopic = file_get_contents ( SITE_URL . 'index.php?api_article/clist' );
  746.  
  747. $de_json = json_decode ( $newtopic, TRUE );
  748.  
  749. $count_json = count ( $de_json );
  750. for($i = 0; $i < $count_json; $i ++) {
  751.  
  752. $content [] = array ("Title" => $de_json [$i] ['Title'], "Description" => $de_json [$i] ['Description'], "PicUrl" => $de_json [$i] ['PicUrl'], "Url" => $de_json [$i] ['Url'] );
  753.  
  754. }
  755.  
  756. return $content;
  757. }
  758. //最热博客
  759. function hotblog() {
  760. $content = array ();
  761.  
  762. $newtopic = file_get_contents ( SITE_URL . 'index.php?api_article/hotalist' );
  763.  
  764. $de_json = json_decode ( $newtopic, TRUE );
  765.  
  766. $count_json = count ( $de_json );
  767. for($i = 0; $i < $count_json; $i ++) {
  768.  
  769. $content [] = array ("Title" => $de_json [$i] ['Title'], "Description" => $de_json [$i] ['Description'], "PicUrl" => $de_json [$i] ['PicUrl'], "Url" => $de_json [$i] ['Url'] );
  770.  
  771. }
  772.  
  773. return $content;
  774. }
  775. //最新问题
  776. function newquestion() {
  777. $content = array ();
  778.  
  779. $newquestion = file_get_contents ( SITE_URL . 'index.php?api_article/newqlist' );
  780.  
  781. $de_json = json_decode ( $newquestion, TRUE );
  782.  
  783. $count_json = count ( $de_json );
  784. for($i = 0; $i < $count_json; $i ++) {
  785.  
  786. $content [] = array ("Title" => $de_json [$i] ['title'], "Description" => '', "PicUrl" => $de_json [$i] ['avatar'], "Url" => $de_json [$i] ['url'] );
  787. }
  788.  
  789. return $content;
  790. }
  791. //热门问题
  792. function hotquestion() {
  793. $content = array ();
  794. if (is_https ()) {
  795. ! define ( 'SITE_URL' ) && define ( 'SITE_URL', 'https://' . $_SERVER ['HTTP_HOST'] . '/' );
  796. } else {
  797. ! define ( 'SITE_URL' ) && define ( 'SITE_URL', 'http://' . $_SERVER ['HTTP_HOST'] . '/' );
  798. }
  799.  
  800. $hotquestion = file_get_contents ( SITE_URL . 'index.php?api_article/hotqlist' );
  801. $de_json = json_decode ( $hotquestion, TRUE );
  802.  
  803. $count_json = count ( $de_json );
  804. for($i = 0; $i < $count_json; $i ++) {
  805.  
  806. $content [] = array ("Title" => $de_json [$i] ['title'], "Description" => '', "PicUrl" => $de_json [$i] ['avatar'], "Url" => $de_json [$i] ['url'] );
  807. }
  808.  
  809. return $content;
  810. }
  811. }
  812. ?>

php 微信模板消息发送的更多相关文章

  1. PHP实现微信模板消息发送给指定用户

    使用公众号的模板消息功能,必须是认证过的服务号,需要发送微信号的openid,同一微信号在不同公众号下的openid是不同的,在公众号下是唯一的,获取不到微信号 进入公众平台  功能->模板消息 ...

  2. PHP微信模板消息发送

    <?php class Wxtemplate extends Base { function __construct() { $this->appid = config('WXAPP_AP ...

  3. 【原创分享·微信支付】C# MVC 微信支付之微信模板消息推送

    微信支付之微信模板消息推送                    今天我要跟大家分享的是“模板消息”的推送,这玩意呢,你说用途嘛,那还是真真的牛逼呐.原因在哪?就是因为它是依赖微信生存的呀,所以他能不 ...

  4. C# MVC 微信支付之微信模板消息推送

    微信支付之微信模板消息推送                    今天我要跟大家分享的是"模板消息"的推送,这玩意呢,你说用途嘛,那还是真真的牛逼呐.原因在哪?就是因为它是依赖微信 ...

  5. 前后端分离djangorestframework—— 接入微信模板消息推送

    微信 什么是微信也不多说,跟前面的支付宝一样的 微信支付 微信支付也有个沙箱环境,沙箱环境官方文档 由文档中那句很显眼的话所得,即使是测试环境也需要真实的商户号,所以这个就没法想支付宝那样用沙箱账号来 ...

  6. java开发微信模板消息推送

    发布时间:2018-12-12   技术:springboot+maven   概述 该demo主要涉及微信模板消息推送功能, 详细 代码下载:http://www.demodashi.com/dem ...

  7. 5分钟连续出现某现象+微信模板消息提醒 PHP

    需求场景:用电插座电流连续出现5次电流过高(大于 3A)后停止用电服务,前四次发送电流过高提醒,最后一次发送结束用电服务提醒 思路: Redis  key 设为:插座编号+user户编号  value ...

  8. 应用jfinal发送微信模板消息的一个bug

    严格来讲,这不是一个bug,只是我们应用的方式不对.微信发送模板消息的方法是: HttpUtils.post(sendApiUrl + AccessTokenApi.getAccessTokenStr ...

  9. ThinkPHP3.2.3发送微信模板消息

    一.开通模板消息功能 所有服务号都可以在功能->添加功能插件处看到申请模板消息功能的入口,但只有认证后的服务号才可以申请模板消息的使用权限并获得该权限:需要选择公众账号服务所处的2个行业,每月可 ...

随机推荐

  1. 使用kubeadm平滑地升级kubenetes集群(v1.10.2到v1.10.3)

    写在前面 kubernetes是目前最炙手火热的容器管理.调度.编排平台,不仅拥有全面的生态系统,而且还开源.开放自治,谁贡献的多,谁的话语权就大,云提供商都有专门的工程师来从事kubernetes开 ...

  2. 关于raft算法

    列出一些比较好的学习资料, 可以经常翻一番,加深印象 0 raft官方git 1  raft算法动画演示 2    Raft 为什么是更易理解的分布式一致性算法 3  raft一致性算法 4  Raf ...

  3. Python 闭包小记

    闭包就是能够读取其他函数内部变量的函数.例如在javascript中,只有函数内部的子函数才能读取局部变量,所以闭包可以理解成“定义在一个函数内部的函数“.在本质上,闭包是将函数内部和函数外部连接起来 ...

  4. GC参考手册 —— GC 算法(基础篇)

    本章简要介绍GC的基本原理和相关技术, 下一章节再详细讲解GC算法的具体实现.各种垃圾收集器的实现细节虽然并不相同,但总体而言,垃圾收集器都专注于两件事情: 查找所有存活对象 抛弃其他的部分,即死对象 ...

  5. 面向对象之七大基本原则(javaScript)

    1. 前言 2. 单一职责 3. 开闭原则 4. 里氏替换 5. 依赖倒置 6. 接口隔离 7. 迪米特法则 8. 组合聚合复用原则 9. 总结 1. 前言 面向对象编程有自己的特性与原则,如果对于面 ...

  6. java jdk 8反编译工具JD-GUI、procyon-decompiler、luyten、crf下载使用简介

    本文对常用的反编译工具进行简单介绍 JD-GUI.procyon-decompiler.luyten.crf   反编译工具分类 JD-GUI JDK7以及之前可以使用   JD-GUI,如果版本&g ...

  7. SLAM+语音机器人DIY系列:(二)ROS入门——8.理解roslaunch在大型项目中的作用

    摘要 ROS机器人操作系统在机器人应用领域很流行,依托代码开源和模块间协作等特性,给机器人开发者带来了很大的方便.我们的机器人“miiboo”中的大部分程序也采用ROS进行开发,所以本文就重点对ROS ...

  8. 树莓派播放视频的播放器omxplayer

    omxplyer为树莓派量身定做的一款GPU硬件加速的播放器,很好的解决了树莓派cpu计算力不足的缺点.(播放时cpu一定都不烫手) 1.安装方法: CTRL + ALT + T 调出终端命令行输入 ...

  9. C#工具:加密解密帮助类

    using System; using System.IO; using System.Security.Cryptography; using System.Text; //加密字符串,注意strE ...

  10. int-Integer-String之间的转换方式

    1.int和Integer之间的转换:   1) int----->Integer ①自动装箱 ②Integer的构造方法 ③调用Integer的静态方法:static Integer valu ...