<?phpdefine("TOKEN", "******");$wechatObj = new wechatCallbackapiTest();//$wechatObj->valid();//$wechatObj->respond();class wechatCallbackapiTest{ public function valid() { $echoStr = $_GET["echostr"]; //valid signature , option if($this->checkSignature()){ echo $echoStr; exit; } } private function checkSignature() { // you must define TOKEN by yourself if (!defined("TOKEN")) { throw new Exception('TOKEN is not defined!'); } $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $token = TOKEN; $tmpArr = array($token, $timestamp, $nonce); // use SORT_STRING rule sort($tmpArr, SORT_STRING); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ){ return true; }else{ return false; } }}//下面是微信的xml信息 $source = $GLOBALS['HTTP_RAW_POST_DATA']; $source1 = "<xml> <ToUserName><![CDATA[gh_2eebd1c1d92e]]></ToUserName> <FromUserName><![CDATA[o8KDujlmvJYFxNe26jmtquaX05Rg]]></FromUserName> <CreateTime>1356076016</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[崗頂]]></Content> </xml>"; $params = !empty($source) && is_string($source) ? @simplexml_load_string($source, 'SimpleXMLElement', LIBXML_NOCDATA) : null; $FromUserName = trim($params->FromUserName); switch (trim($params->MsgType)) { case 'text': $content = trim($params->Content); if ($content) { output($FromUserName, $content, true); } break; case 'event': if ($params->Event == "SCAN") {//掃碼 if (preg_match("#^1$#", $params->EventKey)) {//傳入了用戶數字id // $content = trim($params->EventKey); //$result1 = array(); //$result2 = array(); //file_put_contents("txt.log",var_export($params,true),FILE_APPEND); //$content = trim($params->EventKey); $content="恭喜您得到優惠券!戳連接領取<a href='http://url.cn/48Ylmju'>百度外賣新手大禮包</a>"; output($FromUserName, $content, true); } if (preg_match("#^2$#", $params->EventKey)) {//傳入了用戶數字id // $content = trim($params->EventKey); //$result1 = array(); //$result2 = array(); //file_put_contents("txt.log",var_export($params,true),FILE_APPEND); //$content = trim($params->EventKey); $content="恭喜您得到優惠券!戳連接領取<a href='https://dorado.xiaojukeji.com/share.html?a=9717&b=24102&c=24100&s=148488051956077276&m=MTg1ODg2NTYzODU=&ts=1484880519&fc=d00ed4f8e3fd1546d661ea3dcf9513c6&t=QR&d=0'>滴滴出行打車券</a>"; output($FromUserName, $content, true); } } if ($params->Event == "subscribe") { $content="歡迎您使用每天拼吧!超值紅包大禮奉上1.送你280元投資紅包賺大錢,<a href='https://lantouzi.com/union/hello?pcode=hi86xq&hmsr=hi86xq&hmmd=cpc&hmpl=zonghe'>立刻領取>></a>2.美團外賣15元超值紅包,<a href='http://url.cn/48YrgMy'>立刻領取>></a>3.蘑菇街送你20元現金禮包,<a href='http://url.cn/48Yp71Y'>立刻領取>></a>4.惟品會專享30元紅包,<a href='http://url.cn/48YmcyL'>立刻領取>></a>------------------每天拼吧給您提供各種紅包優惠券,包括滴滴,百度,美團,餓了麼,惟品會,蘑菇街,懶投資等等,推送超高性價比的商品信息,定時分享最省錢的網購攻略。關注每天拼吧,咱們爲你精選最優商品,在網購路上作個精明小能手!"; output($FromUserName, $content, true); } break; default : break; }function output($ToUserName, $Content, $short = false){ $output = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[gh_4fdbdd0ce2ca]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> </xml>"; $Content = sprintf($output, $ToUserName, time(), "text", $Content); //$Content = trim($Content); echo $Content; exit();}