<?php // 晕大海 2014.09.26 define("TOKEN", "weixin"); $wechatObj = new wechatCallbackapiTest(); if(!isset($_GET["echostr"])){ $wechatObj->responseMsg(); }else{ $wechatObj->valid(); } class wechatCallbackapiTest { /* 验证入口 */ public function valid() { $echoStr = $_GET["echostr"]; if($this->checkSignature()){ echo $echoStr; exit; } } /* 接受请求 */ public function responseMsg() { $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; if (!empty($postStr)){ libxml_disable_entity_loader(true); $postObj = simplexml_load_string($postStr, ‘SimpleXMLElement‘, LIBXML_NOCDATA); $RX_TYPE = trim($postObj->MsgType); $result = self::receivemsg($postObj, $RX_TYPE); self::log($postObj); }else { echo ""; exit; } } /* 写入日志 */ public function log($postObj, $content=‘‘){ @$fp = fopen("log.html","a"); if(empty($content)){ $fileData = ‘时间:‘.date(‘Y-m-d H:i:s‘, time()).‘ 请求类型:‘.$postObj->MsgType.‘ 事件:‘.$postObj->Event.‘ 开发者:‘.$postObj->ToUserName.‘ 发送方帐号:‘.$postObj->FromUserName.‘</br>‘; }else{ $fileData = ‘时间:‘.date(‘Y-m-d H:i:s‘, time()).‘ 内容:‘.$content.‘</br>‘; } fwrite($fp,$fileData); fclose($fp); } /* 自动文本回复 * 请求类型:$postObj->MsgType * 请求事件:$postObj->Event * 开发者:$postObj->ToUserName * 回复账号OpenID:$postObj->FromUserName */ private function receivemsg($postObj, $RX_TYPE=‘text‘){ $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> <FuncFlag>0</FuncFlag> </xml>"; $msgType = "text"; if($postObj->Event == "subscribe"){ $keyword = ‘content_subscribe‘; }elseif($postObj->Event == "unsubscribe"){ $keyword = ‘content_unsubscribe‘; }else{ $keyword = trim($postObj->Content); if(empty($keyword)){ $keyword = ‘content_kong‘; } } $url = "http://*****/msg.php?keyword=$keyword"; $contentStr = file_get_contents($url); $contentStr = $contentStr.‘,‘.$postObj->MsgType; $resultStr = sprintf($textTpl, $postObj->FromUserName, $postObj->ToUserName, time(), $msgType, $contentStr); echo $resultStr; } /* 验证方法 */ private function checkSignature() { 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); sort($tmpArr, SORT_STRING); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ){ return true; }else{ return false; } } } ?>
微信的接口文件 wx_sample.php
<?php // 晕大海 2014.09.26 header("Content-type: text/html; charset=utf-8"); $keyword = $_GET["keyword"]; $wechatObj = new msg(); $wechatObj->index($keyword); class msg { public function index($keyword) { $contentStr = "Welcome to wechat world!"; if($keyword == ‘1‘){ $contentStr = ‘成功了‘; }elseif($keyword == ‘2‘){ $contentStr = ‘你很二‘; }elseif($keyword == ‘你好‘){ $contentStr = ‘222222‘; }elseif($keyword == ‘content_subscribe‘){ $contentStr = ‘欢迎关注‘; }elseif($keyword == ‘content_unsubscribe‘){ $contentStr = ‘‘; } echo $contentStr; } }
通过关键词返回不同信息文件 msg.php
时间:2014-09-26 23:10:14 请求类型:text 事件: 开发者:发送方帐号:</br>
日志文件,log.html 方面记录接口的问题
代码地址和说明:
http://www.yundahai.com/php/liang_8_50_1.html
http://www.yundahai.com/php/liang_8_49_1.html
时间: 2024-10-14 19:48:32