You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

763 lines
30 KiB

  1. <?php
  2. class WeChat{
  3. private $_appid;
  4. private $_appsecret;
  5. private $_accessToken;
  6. public $debug=DEBUG;
  7. public $MsgType='text';
  8. public $msg=array();
  9. /* //QRCode类型
  10. const QRCODE_TYPE_TEMP=1;
  11. const QRCODE_TYPE_LIMIT=2;
  12. const QRCODE_TYPE_LIMIT_STR=3; */
  13. public function __construct($appid,$appsecret){
  14. $this->_appid=$appid;
  15. $this->_appsecret=$appsecret;
  16. if (isset($_GET['echostr'])) {
  17. $this->valid();
  18. }
  19. if (ALWAYSCHECK==true){
  20. $check=$this->checkSignature();
  21. if (!$check){
  22. $this->writelog("checkSignature()--非法服务器");
  23. exit;
  24. }
  25. }
  26. $this->_accessToken= $this->getAccessToken();
  27. }
  28. public function getAccessToken(){
  29. // access_token 应该全局存储与更新,以下代码以写入到文件中做示例
  30. $data = json_decode($this->get_php_file("access_token.php"));
  31. if ($data->expire_time < time()) {
  32. // 如果是企业号用以下URL获取access_token
  33. // $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$this->appId&corpsecret=$this->appSecret";
  34. $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->_appid}&secret={$this->_appsecret}";
  35. $res = json_decode($this->_requestGet($url));
  36. $access_token = $res->access_token;
  37. $this->writelog("url获取的token:".$access_token);
  38. if ($access_token) {
  39. $data->expire_time = time() + 3600;
  40. $data->access_token = $access_token;
  41. $this->set_php_file("access_token.php", json_encode($data));
  42. $this->writelog("生成access_token:".$access_token);
  43. }
  44. } else {
  45. $access_token = $data->access_token;
  46. $this->writelog("文件中读取的token:".$access_token);
  47. }
  48. return $access_token;
  49. }
  50. private function get_php_file($filename) {
  51. $dir=__DIR__."/../Log/";
  52. if (!is_dir($dir)){
  53. mkdir($dir);
  54. }
  55. $token_file=$dir.$filename;
  56. return trim(substr(file_get_contents($token_file), 15));
  57. }
  58. private function set_php_file($filename, $content) {
  59. $dir=__DIR__."/../Log/";
  60. if (!is_dir($dir)){
  61. mkdir($dir);
  62. }
  63. $token_file=$dir.$filename;
  64. $fp = fopen($token_file, "w");
  65. fwrite($fp, "<?php exit();?>" . $content);
  66. fclose($fp);
  67. }
  68. private function _requestGet($url,$ssl=true){
  69. $curl=curl_init();
  70. curl_setopt($curl, CURLOPT_URL, $url);
  71. /* $user_agent=isset($_SERVER['HTTP_USER_AGENT'])?$_SERVER['HTTP_USER_AGENT']:'';
  72. curl_setopt($curl, CURLOPT_USERAGENT, $user_agent); */
  73. curl_setopt($curl, CURLOPT_AUTOREFERER, true);
  74. if ($ssl){
  75. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  76. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
  77. }
  78. curl_setopt($curl, CURLOPT_HEADER, false);
  79. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  80. $response=curl_exec($curl);
  81. if ($response===false){
  82. $this-> writelog('_requestGet()--返回值错误');
  83. return false;
  84. }
  85. return $response;
  86. }
  87. private function _requestPost($url,$data,$ssl=true){
  88. $curl=curl_init();
  89. curl_setopt($curl, CURLOPT_URL, $url);
  90. $user_agent=isset($_SERVER['HTTP_USER_AGENT'])? $_SERVER['HTTP_USER_AGENT']:'';
  91. curl_setopt($curl, CURLOPT_USERAGENT, $user_agent);
  92. curl_setopt($curl, CURLOPT_AUTOREFERER, true);
  93. if ($ssl){
  94. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  95. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);
  96. }
  97. curl_setopt($curl, CURLOPT_POST, true);
  98. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  99. curl_setopt($curl, CURLOPT_HEADER, false);
  100. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  101. $response=curl_exec($curl);
  102. if ($response===false){
  103. $this->writelog('_requestPost()--返回值错误');
  104. return false;
  105. }
  106. //$error=json_decode($response,true);
  107. $this->writelog("url:".$url);
  108. $this->writelog("curl返回值为".$response);
  109. return $response;
  110. }
  111. public function valid()
  112. {
  113. $echoStr = $_GET["echostr"];
  114. //valid signature , option
  115. if($this->checkSignature()){
  116. $this->writelog( "valid()--第一次验证成功");
  117. echo $echoStr;
  118. exit;
  119. }else {
  120. $this->writelog( "valid()--第一次验证,非法服务器");
  121. exit();
  122. }
  123. }
  124. private function checkSignature()
  125. {
  126. // you must define TOKEN by yourself
  127. if (!defined("TOKEN")) {
  128. throw new Exception('TOKEN is not defined!');
  129. }
  130. $signature = isset($_GET["signature"]) ? $_GET["signature"]:"";
  131. $timestamp = isset($_GET["timestamp"]) ? $_GET["timestamp"] : "";
  132. $nonce = isset($_GET["nonce"]) ? $_GET["nonce"] : "";
  133. $token = TOKEN;
  134. $tmpArr = array($token, $timestamp, $nonce);
  135. // use SORT_STRING rule
  136. sort($tmpArr, SORT_STRING);
  137. $tmpStr = implode( $tmpArr );
  138. $tmpStr = sha1( $tmpStr );
  139. if( $tmpStr == $signature ){
  140. return true;
  141. }else{
  142. return false;
  143. }
  144. }
  145. private function writelog($activation){
  146. $dir="./Log";
  147. if (!is_dir($dir)){
  148. mkdir($dir);
  149. }
  150. $filename=date("Y-m-d").".txt";
  151. $open=fopen($dir."/".$filename,"a");
  152. fwrite($open,date("Y-m-d H:i:s")."\t".$activation."\r\n");
  153. fclose($open);
  154. }
  155. //获取消息
  156. public function responseMsg()
  157. {
  158. //get post data, May be due to the different environments
  159. //$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
  160. $postStr = file_get_contents("php://input");
  161. if ($this->debug){
  162. $this->writelog("function:responseMsg--接收到的消息为--".$postStr);
  163. }
  164. //extract post data
  165. if (!empty($postStr)){
  166. /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
  167. the best way is to check the validity of xml by yourself */
  168. libxml_disable_entity_loader(true);
  169. $this->msg = (array)simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  170. foreach ($this->msg as $key=>$v){
  171. $this->writelog("function:responseMsg--接收到的消息为--".$key);
  172. $this->writelog("function:responseMsg--接收到的消息为--".$v);
  173. }
  174. $this->MsgType=strtolower($this->msg['MsgType']);
  175. return $this->msg;
  176. }else {
  177. $this->writelog("消息为空");
  178. return false;
  179. }
  180. }
  181. public function eventMsg($array){
  182. $result = "";
  183. switch ($array['Event']){
  184. case 'subscribe':
  185. $pdo=conn();
  186. $sql_update = " INSERT INTO wechat_user(`OPENID`,`IS_REGISTER`,`FROM_MP`) values('{$array['FromUserName']}',1,1) ON DUPLICATE KEY UPDATE IS_REGISTER = 1 ";
  187. $selectDate = $pdo->exec($sql_update);
  188. $pdo2=conn_ctsdata();
  189. $selectDate = $pdo2->exec($sql_update);
  190. /*
  191. $contentStr="Hi,『蜘蛛出行』每天为无数乘客提供便捷的交通出行服务,快来尝试一下吧!
  192. 如果您已经预订了车票,请按以下格式回复“#订单号#预定时预留的手机号”进行订单绑定。
  193. 绑定后可在『更多服务』—『行程』中查看巴士定位、车牌号码和司机电话等信息。
  194. (#号需输入,不可跳过,订单号请查找蜘蛛出行为您发送的手机短信)";
  195. $textTpl="
  196. <xml>
  197. <ToUserName><![CDATA[%s]]></ToUserName>
  198. <FromUserName><![CDATA[%s]]></FromUserName>
  199. <CreateTime>%s</CreateTime>
  200. <MsgType><![CDATA[%s]]></MsgType>
  201. <Content><![CDATA[%s]]></Content>
  202. </xml>";
  203. $msgType="text";
  204. $result= sprintf($textTpl, $array['FromUserName'], $array['ToUserName'], time(), $msgType, $contentStr);
  205. return $result;
  206. */
  207. $data_media_id='{"media_id":"99eQLZw0myQ1ZvOS4rjpsl7AN87CEwwqLRGrk9vSa90"}';
  208. $url="https://api.weixin.qq.com/cgi-bin/material/get_material?access_token=".$this->_accessToken;
  209. $response=$this->_requestPost($url, $data_media_id);
  210. $this->writelog("返回--".$response);
  211. $media=json_decode($response,true);
  212. $mediaArray=$media['news_item'];
  213. $content = array();
  214. foreach ($mediaArray as $k=>$v){
  215. if ($k==0 || $k==1)
  216. $content[] = array("Title"=>$v['title'], "Description"=>$v['digest'], "PicUrl"=>$v['thumb_url'], "Url" =>$v['url']);
  217. elseif($k==2){
  218. $content[] = array("Title"=>$v['title'], "Description"=>$v['digest'], "PicUrl"=>$v['thumb_url'], "Url" =>$v['content_source_url']);
  219. }
  220. else
  221. $content[] = array("Title"=>$v['title'], "Description"=>$v['digest'], "PicUrl"=>$v['thumb_url'], "Url" =>$v['content_source_url']);
  222. }
  223. $this->writelog("返回--".json_encode($content));
  224. if(is_array($content)){
  225. if (isset($content[0]['PicUrl'])){
  226. $result = $this->transmitNews($array, $content);
  227. $this->writelog(json_encode($result));
  228. return $result;
  229. }
  230. }
  231. if (!empty($array['EventKey'])){
  232. /*
  233. $id=substr($array['EventKey'],strpos($array['EventKey'],"qrscene_")+8);
  234. $openid=$array['FromUserName'];
  235. $time=date("Y-m-d H:i:s");
  236. $pdo = new PDO("sqlsrv:Server=".HOST.";Database=".DB, USER, PASSWORD);//5.6版本pdo连接sqlsrv
  237. $sql="SELECT id FROM qrcodeDetail WHERE qrcodeId=".$id." AND openId='".$openid."'";
  238. $selectDate=$pdo->query($sql);
  239. $selectRes=$selectDate->fetchAll(PDO::FETCH_ASSOC);
  240. if (empty($selectRes)){
  241. $sql1="INSERT INTO qrcodeDetail values($id,'$openid','subscribe','$time')";
  242. $this->writelog("eventMsg function : subscribe:result id=".$id."--sql1-".$sql1);
  243. $result=$pdo->exec($sql1);
  244. }
  245. $sql2="SELECT media_id,url FROM qrcode WHERE id=".$id;
  246. $selectDate2=$pdo->query($sql2);
  247. $selectRes2=$selectDate2->fetchAll(PDO::FETCH_ASSOC);
  248. if (!empty($selectRes2)){
  249. $media_id= $selectRes2[0]['media_id'];
  250. $Rurl= $selectRes2[0]['url'];
  251. }else {
  252. //$media_id="AuXDjP791-NMcyJajUxBLzhsCmTK5evvwTjASqBwmv8";
  253. $media_id="AuXDjP791-NMcyJajUxBL5N9VCy5YnysR-dDWT2XFWg";
  254. $Rurl="http://xmwxc.zhizhuchuxing.cn/bookingHomePage/mdidiDetail.html?prod_code=NSPTSM01";
  255. }*/
  256. $media_id="99eQLZw0myQ1ZvOS4rjpsl7AN87CEwwqLRGrk9vSa90";
  257. $data='{"media_id":"'.$media_id.'"}';
  258. $url="https://api.weixin.qq.com/cgi-bin/material/get_material?access_token=".$this->_accessToken;
  259. $response=$this->_requestPost($url, $data);
  260. $this->writelog("返回--".$response);
  261. $media=json_decode($response,true);
  262. $mediaArray=$media['news_item'];
  263. $content = array();
  264. foreach ($mediaArray as $k=>$v){
  265. if ($k==0 || $k==1)
  266. $content[] = array("Title"=>$v['title'], "Description"=>$v['digest'], "PicUrl"=>$v['thumb_url'], "Url" =>$v['url']);
  267. else
  268. $content[] = array("Title"=>$v['title'], "Description"=>$v['digest'], "PicUrl"=>$v['thumb_url'], "Url" =>$v['content_source_url']);
  269. }
  270. $this->writelog("返回--".json_encode($content));
  271. if(is_array($content)){
  272. if (isset($content[0]['PicUrl'])){
  273. $result = $this->transmitNews($array, $content);
  274. }
  275. }
  276. }else{
  277. //$data='{"media_id":"AuXDjP791-NMcyJajUxBLw7rLLVx_s0V9I0l_sS92c0"}';
  278. /* $data='{"media_id":"AuXDjP791-NMcyJajUxBL5N9VCy5YnysR-dDWT2XFWg"}';
  279. $url="https://api.weixin.qq.com/cgi-bin/material/get_material?access_token=".$this->_accessToken;
  280. $response=$this->_requestPost($url, $data);
  281. $this->writelog("返回--".$response);
  282. $media=json_decode($response,true);
  283. $mediaArray=$media['news_item'];
  284. $content = array();
  285. foreach ($mediaArray as $k=>$v){
  286. if ($k==0 || $k==1)
  287. $content[] = array("Title"=>$v['title'], "Description"=>$v['digest'], "PicUrl"=>$v['thumb_url'], "Url" =>$v['url']);
  288. else
  289. $content[] = array("Title"=>$v['title'], "Description"=>$v['digest'], "PicUrl"=>$v['thumb_url'], "Url" =>$v['content_source_url']);
  290. }
  291. $this->writelog("返回--".json_encode($content));
  292. if(is_array($content)){
  293. if (isset($content[0]['PicUrl'])){
  294. $result = $this->transmitNews($array, $content);
  295. }
  296. } */
  297. $data_media_id='{"media_id":"99eQLZw0myQ1ZvOS4rjpsl7AN87CEwwqLRGrk9vSa90"}';
  298. $url="https://api.weixin.qq.com/cgi-bin/material/get_material?access_token=".$this->_accessToken;
  299. $response=$this->_requestPost($url, $data_media_id);
  300. $this->writelog("返回--".$response);
  301. $media=json_decode($response,true);
  302. $mediaArray=$media['news_item'];
  303. $content = array();
  304. foreach ($mediaArray as $k=>$v){
  305. if ($k==0 || $k==1)
  306. $content[] = array("Title"=>$v['title'], "Description"=>$v['digest'], "PicUrl"=>$v['thumb_url'], "Url" =>$v['url']);
  307. elseif($k==2){
  308. $content[] = array("Title"=>$v['title'], "Description"=>$v['digest'], "PicUrl"=>$v['thumb_url'], "Url" =>$v['content_source_url']);
  309. }
  310. else
  311. $content[] = array("Title"=>$v['title'], "Description"=>$v['digest'], "PicUrl"=>$v['thumb_url'], "Url" =>$v['content_source_url']);
  312. }
  313. $this->writelog("返回--".json_encode($content));
  314. if(is_array($content)){
  315. if (isset($content[0]['PicUrl'])){
  316. $result = $this->transmitNews($array, $content);
  317. $this->writelog(json_encode($result));
  318. return $result;
  319. }
  320. }
  321. }
  322. break;
  323. case 'SCAN':
  324. if (isset($array['EventKey'])){
  325. /*
  326. $pdo = new PDO("sqlsrv:Server=".HOST.";Database=".DB, USER, PASSWORD);//5.6版本pdo连接sqlsrv
  327. $id=$array['EventKey'];
  328. $openid=$array['FromUserName'];
  329. $time=date("Y-m-d H:i:s");
  330. $sql="SELECT id FROM qrcodeDetail WHERE qrcodeId=".$id." AND openId='".$openid."'";
  331. $selectDate=$pdo->query($sql);
  332. $selectRes=$selectDate->fetchAll(PDO::FETCH_ASSOC);
  333. if (empty($selectRes)){
  334. $sql1="INSERT INTO qrcodeDetail values($id,'$openid','SCAN','$time')";
  335. $this->writelog("eventMsg function : SCAN:result id=".$id."--sql1-".$sql1);
  336. $result=$pdo->exec($sql1);
  337. }*/
  338. /* $this->writelog("eventMsg function : SCAN:result id=".$id."---".json_encode($result));
  339. $errinfo=$pdo->errorInfo();
  340. if ($errinfo[0] !='00000')
  341. $this->writelog("eventMsg function : SCAN:error id=".$id."---".$errinfo[2]);
  342. else{
  343. $res=$result->fetchAll(PDO::FETCH_ASSOC);
  344. $this->writelog("eventMsg function : SCAN:res id=".$id."---".json_encode($res));
  345. $headUrl=$res[0]['url'];
  346. $this->writelog("eventMsg function : SCAN:headUrl id=".$id."---".$headUrl);
  347. header("Location: $headUrl");exit;
  348. } */
  349. // $data='{"media_id":"AuXDjP791-NMcyJajUxBLw7rLLVx_s0V9I0l_sS92c0"}';
  350. /*
  351. $sql2="SELECT media_id,url FROM qrcode WHERE id=".$id;
  352. $selectDate2=$pdo->query($sql2);
  353. $selectRes2=$selectDate2->fetchAll(PDO::FETCH_ASSOC);
  354. if (!empty($selectRes2)){
  355. $media_id= $selectRes2[0]['media_id'];
  356. $Rurl= $selectRes2[0]['url'];
  357. }else {
  358. //$media_id="AuXDjP791-NMcyJajUxBLzhsCmTK5evvwTjASqBwmv8";
  359. $media_id="AuXDjP791-NMcyJajUxBL5N9VCy5YnysR-dDWT2XFWg";
  360. $Rurl="http://xmwxc.zhizhuchuxing.cn/bookingHomePage/mdidiDetail.html?prod_code=NSPTSM01";
  361. }*/
  362. $pdo=conn();
  363. $sql_update = " INSERT INTO wechat_user(`OPENID`,`IS_REGISTER`,`FROM_MP`) values('{$array['FromUserName']}',1,1) ON DUPLICATE KEY UPDATE IS_REGISTER = 1 ";
  364. $selectDate = $pdo->exec($sql_update);
  365. $pdo2=conn_ctsdata();
  366. $selectDate = $pdo2->exec($sql_update);
  367. /*
  368. $contentStr="Hi,『蜘蛛出行』每天为无数乘客提供便捷的交通出行服务,快来尝试一下吧!
  369. 如果您已经预订了车票,请按以下格式回复“#订单号#预定时预留的手机号”进行订单绑定。
  370. 绑定后可在『更多服务』—『行程』中查看巴士定位、车牌号码和司机电话等信息。
  371. (#号需输入,不可跳过,订单号请查找蜘蛛出行为您发送的手机短信)";
  372. $textTpl="
  373. <xml>
  374. <ToUserName><![CDATA[%s]]></ToUserName>
  375. <FromUserName><![CDATA[%s]]></FromUserName>
  376. <CreateTime>%s</CreateTime>
  377. <MsgType><![CDATA[%s]]></MsgType>
  378. <Content><![CDATA[%s]]></Content>
  379. </xml>";
  380. $msgType="text";
  381. $result= sprintf($textTpl, $array['FromUserName'], $array['ToUserName'], time(), $msgType, $contentStr);
  382. return $result;
  383. */
  384. $data_media_id='{"media_id":"99eQLZw0myQ1ZvOS4rjpsl7AN87CEwwqLRGrk9vSa90"}';
  385. $url="https://api.weixin.qq.com/cgi-bin/material/get_material?access_token=".$this->_accessToken;
  386. $response=$this->_requestPost($url, $data_media_id);
  387. $this->writelog("返回--".$response);
  388. $media=json_decode($response,true);
  389. $mediaArray=$media['news_item'];
  390. $content = array();
  391. foreach ($mediaArray as $k=>$v){
  392. if ($k==0 || $k==1)
  393. $content[] = array("Title"=>$v['title'], "Description"=>$v['digest'], "PicUrl"=>$v['thumb_url'], "Url" =>$v['url']);
  394. elseif($k==2){
  395. $content[] = array("Title"=>$v['title'], "Description"=>$v['digest'], "PicUrl"=>$v['thumb_url'], "Url" =>$v['content_source_url']);
  396. }
  397. else
  398. $content[] = array("Title"=>$v['title'], "Description"=>$v['digest'], "PicUrl"=>$v['thumb_url'], "Url" =>$v['content_source_url']);
  399. }
  400. $this->writelog("返回--".json_encode($content));
  401. if(is_array($content)){
  402. if (isset($content[0]['PicUrl'])){
  403. $result = $this->transmitNews($array, $content);
  404. $this->writelog(json_encode($result));
  405. return $result;
  406. }
  407. }
  408. /*
  409. $data='{"media_id":"'.$media_id.'"}';
  410. $url="https://api.weixin.qq.com/cgi-bin/material/get_material?access_token=".$this->_accessToken;
  411. $response=$this->_requestPost($url, $data);
  412. $this->writelog("返回--".$response);
  413. $media=json_decode($response,true);
  414. $mediaArray=$media['news_item'];
  415. $content = array();
  416. foreach ($mediaArray as $k=>$v){
  417. if ($k==0 || $k==1)
  418. $content[] = array("Title"=>$v['title'], "Description"=>$v['digest'], "PicUrl"=>$v['thumb_url'], "Url" =>$v['url']);
  419. elseif($k==2){
  420. $content[] = array("Title"=>$v['title'], "Description"=>$v['digest'], "PicUrl"=>$v['thumb_url'], "Url" =>$v['content_source_url']);
  421. }
  422. else
  423. $content[] = array("Title"=>$v['title'], "Description"=>$v['digest'], "PicUrl"=>$v['thumb_url'], "Url" =>$v['content_source_url']);
  424. }
  425. $this->writelog("返回--".json_encode($content));
  426. if(is_array($content)){
  427. if (isset($content[0]['PicUrl'])){
  428. $result = $this->transmitNews($array, $content);
  429. }
  430. }*/
  431. }
  432. break;
  433. case 'CLICK':
  434. $pdo=conn();
  435. $sql_update = " INSERT INTO wechat_user(`OPENID`,`IS_REGISTER`,`FROM_MP`) values('{$array['FromUserName']}',1,1) ON DUPLICATE KEY UPDATE IS_REGISTER = 1 ";
  436. $selectDate = $pdo->exec($sql_update);
  437. $pdo2=conn_ctsdata();
  438. $selectDate = $pdo2->exec($sql_update);
  439. /*
  440. $contentStr="Hi,『蜘蛛出行』每天为无数乘客提供便捷的交通出行服务,快来尝试一下吧!
  441. 如果您已经预订了车票,请按以下格式回复“#订单号#预定时预留的手机号”进行订单绑定。
  442. 绑定后可在『更多服务』—『行程』中查看巴士定位、车牌号码和司机电话等信息。
  443. (#号需输入,不可跳过,订单号请查找蜘蛛出行为您发送的手机短信)";
  444. $textTpl="
  445. <xml>
  446. <ToUserName><![CDATA[%s]]></ToUserName>
  447. <FromUserName><![CDATA[%s]]></FromUserName>
  448. <CreateTime>%s</CreateTime>
  449. <MsgType><![CDATA[%s]]></MsgType>
  450. <Content><![CDATA[%s]]></Content>
  451. </xml>";
  452. $msgType="text";
  453. $result= sprintf($textTpl, $array['FromUserName'], $array['ToUserName'], time(), $msgType, $contentStr);
  454. return $result;
  455. */
  456. if (isset($array['EventKey']) && $array['EventKey']=='Push-Photo-List'){
  457. $data='{"media_id":"99eQLZw0myQ1ZvOS4rjpsl7AN87CEwwqLRGrk9vSa90"}';
  458. $url="https://api.weixin.qq.com/cgi-bin/material/get_material?access_token=".$this->_accessToken;
  459. $response=$this->_requestPost($url, $data);
  460. $this->writelog("CLICK返回response--".$response);
  461. $media=json_decode($response,true);
  462. $mediaArray=$media['news_item'];
  463. $content = array();
  464. foreach ($mediaArray as $k=>$v){
  465. if ($k==0 || $k==1)
  466. $content[] = array("Title"=>$v['title'], "Description"=>$v['digest'], "PicUrl"=>$v['thumb_url'], "Url" =>$v['url']);
  467. else
  468. $content[] = array("Title"=>$v['title'], "Description"=>$v['digest'], "PicUrl"=>$v['thumb_url'], "Url" =>$v['content_source_url']);
  469. }
  470. $this->writelog("CLICK返回content--".json_encode($content));
  471. if(is_array($content)){
  472. if (isset($content[0]['PicUrl'])){
  473. $result = $this->transmitNews($array, $content);
  474. }
  475. }
  476. } else if( isset($array['EventKey']) && $array['EventKey']=='XMWX_QIJIANDIAN' ) {
  477. $contentStr="佰廉官网\nhttp://m.xmwtt.com\n天猫旗舰店\nhttp://t.cn/RZ9LO29\n去哪儿旗舰店\nhttp://t.cn/R7PGfpP";
  478. $textTpl="
  479. <xml>
  480. <ToUserName><![CDATA[%s]]></ToUserName>
  481. <FromUserName><![CDATA[%s]]></FromUserName>
  482. <CreateTime>%s</CreateTime>
  483. <MsgType><![CDATA[%s]]></MsgType>
  484. <Content><![CDATA[%s]]></Content>
  485. </xml>";
  486. $msgType="text";
  487. $result= sprintf($textTpl, $array['FromUserName'], $array['ToUserName'], time(), $msgType, $contentStr);
  488. return $result;
  489. } else if( isset($array['EventKey']) && $array['EventKey']=='XMWX_TOUSUJIANYI' ) {
  490. $contentStr="投诉与建议\n李先生:0592-330114";
  491. $textTpl="
  492. <xml>
  493. <ToUserName><![CDATA[%s]]></ToUserName>
  494. <FromUserName><![CDATA[%s]]></FromUserName>
  495. <CreateTime>%s</CreateTime>
  496. <MsgType><![CDATA[%s]]></MsgType>
  497. <Content><![CDATA[%s]]></Content>
  498. </xml>";
  499. $msgType="text";
  500. $result= sprintf($textTpl, $array['FromUserName'], $array['ToUserName'], time(), $msgType, $contentStr);
  501. return $result;
  502. }
  503. break;
  504. }
  505. /*
  506. $data='{"type":"news","offset":0,"count":20}';
  507. $url="https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=".$this->_accessToken;
  508. $response=$this->_requestPost($url, $data);
  509. $this->writelog("返回--".$response); */
  510. $this->writelog("回复用户event--".json_encode($result));
  511. return $result;
  512. }
  513. //把openid存入数据库
  514. public function insertOpenid($data,$type){
  515. $openid=$data['FromUserName'];
  516. if ($type =='subscribe'){
  517. $sql="insert into wx_user(openid) values('{$openid}')";
  518. }else if($type =='unsubscribe'){
  519. $sql="delete from wx_user where openid='{$openid}'";
  520. }
  521. $result='';
  522. try {
  523. $pdo = new PDO("sqlsrv:Server=".HOST.";Database=".DB, USER, PASSWORD);//5.6版本pdo连接sqlsrv
  524. //$pdo=new PDO("mysql:host=".HOST.";dbname=".DB,USER,PASSWORD); //7.0版本pdo连接mysql
  525. $result=$pdo->exec($sql);
  526. $errinfo=$pdo->errorInfo();
  527. if ($errinfo[0] !='00000')
  528. $this->writelog($errinfo[2]);
  529. } catch (PDOException $e) {
  530. $this->writelog($e->getMessage());
  531. }
  532. if ($result){
  533. return true;
  534. }else {
  535. return false;
  536. }
  537. }
  538. //有用户关注的时候推送多图文消息
  539. private function transmitNews($array, $newsArray)
  540. {
  541. if(!is_array($newsArray)){
  542. return;
  543. }
  544. $itemTpl = "<item>
  545. <Title><![CDATA[%s]]></Title>
  546. <Description><![CDATA[%s]]></Description>
  547. <PicUrl><![CDATA[%s]]></PicUrl>
  548. <Url><![CDATA[%s]]></Url>
  549. </item>";
  550. $item_str = "";
  551. foreach ($newsArray as $item){
  552. $item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']);
  553. }
  554. $xmlTpl = "<xml>
  555. <ToUserName><![CDATA[%s]]></ToUserName>
  556. <FromUserName><![CDATA[%s]]></FromUserName>
  557. <CreateTime>%s</CreateTime>
  558. <MsgType><![CDATA[news]]></MsgType>
  559. <ArticleCount>%s</ArticleCount>
  560. <Articles>
  561. $item_str</Articles>
  562. </xml>";
  563. $result = sprintf($xmlTpl, $array['FromUserName'], $array['ToUserName'], time(), count($newsArray));
  564. return $result;
  565. }
  566. private function transmitImage( $array ) {
  567. $xmlTpl = "<xml>
  568. <ToUserName><![CDATA[%s]]></ToUserName>
  569. <FromUserName><![CDATA[%s]]></FromUserName>
  570. <CreateTime>%s</CreateTime>
  571. <MsgType><![CDATA[image]]></MsgType>
  572. <Image>
  573. <MediaId><![CDATA[%s]]></MediaId>
  574. </Image>
  575. </xml>";
  576. $result = sprintf($xmlTpl, $array['FromUserName'], $array['ToUserName'], time(), $array['media_id']);
  577. return $result;
  578. }
  579. private function transmitVoice( $array ) {
  580. $xmlTpl = "<xml>
  581. <ToUserName><![CDATA[%s]]></ToUserName>
  582. <FromUserName><![CDATA[%s]]></FromUserName>
  583. <CreateTime>%s</CreateTime>
  584. <MsgType><![CDATA[voice]]></MsgType>
  585. <Image>
  586. <MediaId><![CDATA[%s]]></MediaId>
  587. </Image>
  588. </xml>";
  589. $result = sprintf($xmlTpl, $array['FromUserName'], $array['ToUserName'], time(), $array['media_id']);
  590. return $result;
  591. }
  592. private function transmitVideo( $array ) {
  593. $xmlTpl = "<xml>
  594. <ToUserName><![CDATA[%s]]></ToUserName>
  595. <FromUserName><![CDATA[%s]]></FromUserName>
  596. <CreateTime>%s</CreateTime>
  597. <MsgType><![CDATA[video]]></MsgType>
  598. <Video>
  599. <MediaId><![CDATA[%s]]></MediaId>
  600. <Title><![CDATA[%s]]></Title>
  601. <Description><![CDATA[%s]]></Description>
  602. </Video>
  603. </xml>";
  604. $result = sprintf($xmlTpl, $array['FromUserName'], $array['ToUserName'], time(), $array['media_id'], $array["title"], $array["description"]);
  605. return $result;
  606. }
  607. //获取文本
  608. public function textMsg($data){
  609. $Content=trim($data['Content']);
  610. $textTpl="
  611. <xml>
  612. <ToUserName><![CDATA[%s]]></ToUserName>
  613. <FromUserName><![CDATA[%s]]></FromUserName>
  614. <CreateTime>%s</CreateTime>
  615. <MsgType><![CDATA[%s]]></MsgType>
  616. <Content><![CDATA[%s]]></Content>
  617. </xml>";
  618. $msgType = "text";
  619. $word=$data['Content'];
  620. /*if ($word=='寻路'){
  621. $this->writelog("textMsg有调用:关键字是面试");
  622. $data_media_id='{"media_id":"AuXDjP791-NMcyJajUxBL8LcnGB8gKQTImk6XJK53uU"}';
  623. $url="https://api.weixin.qq.com/cgi-bin/material/get_material?access_token=".$this->_accessToken;
  624. $response=$this->_requestPost($url, $data_media_id);
  625. $this->writelog("返回--".$response);
  626. $media=json_decode($response,true);
  627. $mediaArray=$media['news_item'];
  628. $content = array();
  629. foreach ($mediaArray as $k=>$v){
  630. if ($k==0 || $k==1)
  631. $content[] = array("Title"=>$v['title'], "Description"=>$v['digest'], "PicUrl"=>$v['thumb_url'], "Url" =>$v['url']);
  632. elseif($k==2){
  633. $content[] = array("Title"=>$v['title'], "Description"=>$v['digest'], "PicUrl"=>$v['thumb_url'], "Url" =>$v['content_source_url']);
  634. }
  635. else
  636. $content[] = array("Title"=>$v['title'], "Description"=>$v['digest'], "PicUrl"=>$v['thumb_url'], "Url" =>$v['content_source_url']);
  637. }
  638. $this->writelog("返回--".json_encode($content));
  639. if(is_array($content)){
  640. if (isset($content[0]['PicUrl'])){
  641. $result = $this->transmitNews($data, $content);
  642. $this->writelog(json_encode($result));
  643. return $result;
  644. }
  645. }
  646. } else if ($word=='活动'){
  647. $data["media_id"] = "AuXDjP791-NMcyJajUxBL2MpGN_djcS5OUH-Y5njOMA";
  648. $result = $this->transmitImage($data);
  649. $this->writelog(json_encode($result));
  650. return $result;
  651. } else if( $word=='测试定制巴士' ){
  652. $contentStr="http://xmwxc.zhizhuchuxing.cn/ZZDZ/dzbs_login.php";
  653. $resultStr = sprintf($textTpl, $data['FromUserName'], $data['ToUserName'], time(), $msgType, $contentStr);
  654. return $resultStr;
  655. }else if( $word=='订单绑定' || $word=='绑定订单' || $word=='绑定' || $word=='绑订单' ){
  656. $contentStr="Hi,『佰廉国旅』每天为无数乘客提供便捷的交通出行服务,快来尝试我们为您准备的新功能吧!
  657. 如果您已经预订了车票,请按以下格式回复“#订单号#预定时预留的手机号”进行订单绑定,绑定后可在『更多服务』—『行程』中查看巴士定位、车牌号码和司机电话等信息。
  658. (#号需输入,不可跳过,订单号请查找佰廉国旅为您发送的手机短信)
  659. 如果您还没有预订车票,请戳左下角『出行预订』!";
  660. $resultStr = sprintf($textTpl, $data['FromUserName'], $data['ToUserName'], time(), $msgType, $contentStr);
  661. $this->writelog($resultStr);
  662. return $resultStr;
  663. }else if(substr($word,0,1) =='#'){
  664. $word=substr($word, 1);
  665. $wordArr=explode("#", $word);
  666. if (count($wordArr) !=2 || $wordArr[0]=='' || $wordArr[1] ==''){
  667. $contentStr="输入格式错误";
  668. }
  669. else if(!is_numeric($wordArr[0]) || !is_numeric($wordArr[1]) ){
  670. $contentStr="订单号和手机号必须整数";
  671. }else{
  672. $pdo=conn();
  673. $sql="CALL DRIVER_WEICHAT_BOND(".$wordArr[0].",".$wordArr[1].",'".$data['FromUserName']."')";
  674. $this->writelog("订单绑定".$sql);
  675. $result=$pdo->query($sql);
  676. $rowset=$result->fetchAll(PDO::FETCH_ASSOC);
  677. $result->closeCursor();
  678. $update_sql = "UPDATE order_main SET MEMBER_ID = (SELECT id FROM wechat_user WHERE OPENID = '{$data['FromUserName']}' ) WHERE order_id = {$wordArr[0]} OR parent_order_id = {$wordArr[0]}";
  679. $pdo_update = conn();
  680. $pdo_update->exec($update_sql);
  681. if ($rowset[0]['code']==0){
  682. $contentStr="绑定成功";
  683. }else{
  684. $contentStr=$rowset[0]['info'];
  685. }
  686. }
  687. $resultStr = sprintf($textTpl, $data['FromUserName'], $data['ToUserName'], time(), $msgType, $contentStr);
  688. $this->writelog($resultStr);
  689. return $resultStr;
  690. } else{
  691. $contentStr="Hi,『蜘蛛出行』每天为无数乘客提供便捷的交通出行服务,快来尝试我们为您准备的新功能吧!
  692. 如果您已经预订了车票,请按以下格式回复“#订单号#预定时预留的手机号”进行订单绑定,绑定后可在『更多服务』—『行程』中查看巴士定位、车牌号码和司机电话等信息。
  693. (#号需输入,不可跳过,订单号请查找蜘蛛出行为您发送的手机短信)
  694. 如果您还没有预订车票,请戳左下角『出行预订』!";
  695. $resultStr = sprintf($textTpl, $data['FromUserName'], $data['ToUserName'], time(), $msgType, $contentStr);
  696. return $resultStr;
  697. }*/
  698. //$contentStr=$this->simsimiHttp($Content);
  699. }
  700. private function simsimiHttp($msg){
  701. $url="http://www.xiaodoubi.com/simsimiapi.php?msg=".$msg;
  702. $res = file_get_contents($url);
  703. file_put_contents("./demo.txt",date("Y-m-d H:i:s")." ".$res.PHP_EOL,FILE_APPEND);
  704. if (strpos($res,"xiaodouqqcom") !== false){
  705. $res="无法回答,请不要发一下奇怪的问题或字符😒";
  706. }
  707. if (strpos($msg,"主人") !== false){
  708. $res="我的主人就是你呀";
  709. }
  710. return $res;
  711. }
  712. private function checkAccessToken($check){
  713. if (isset($check['errcode']) && $check['errcode'] ==40001){
  714. $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->_appid}&secret={$this->_appsecret}";
  715. $result=$this->_requestGet($url);
  716. if(!$result){
  717. $this->writelog("获取token出错");
  718. return false;
  719. }
  720. $result_obj=json_decode($result);
  721. file_put_contents($token_file, $result_obj->access_token);
  722. $this->writelog("url获取的token:".$result_obj->access_token);
  723. return $result_obj->access_token;
  724. }
  725. }
  726. }
  727. ?>