위챗 관심 자동 회신 메시지

<?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;
		}
	}
}

?>

위챗의 인터페이스 파일 wxsample.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

좋은 웹페이지 즐겨찾기