微信回复(二)

<?php


/*echo $_GET['echostr'];
exit;*/


//接收xml数据
$postStr = $GLOBALS['HTTP_RAW_POST_DATA'];
//把xml数据转化成对象
$postObj = simplexml_load_string($postStr);
//接收post的值
$fromuserName = $postObj->FromUserName;
$touserName = $postObj->ToUserName;
$time = time();
$msgType = $postObj->MsgType;
$keywords = $postObj->Content;


//自动回复
$textTpl = "<xml>
            <ToUserName><![CDATA[%s]]></ToUserName>
            <FromUserName><![CDATA[%s]]></FromUserName>
            <CreateTime>%s</CreateTime>
            <MsgType><![CDATA[%s]]></MsgType>
            <Content><![CDATA[%s]]></Content>
            </xml>";

//关键词自动回复
if($postObj->MsgType == "text" ){
    if($postObj->Content == '1'){
        $content = '111!!!!';
        $resultful = sprintf($textTpl,$fromuserName,$touserName,$time,$msgType,$content);
        echo $resultful;
    }else if($postObj->Content == '2'){

        $content = '222!!!!';
        $resultful = sprintf($textTpl,$fromuserName,$touserName,$time,$msgType,$content);
        echo $resultful;
    }else{
        $content = 'hello world!';
        $resultful = sprintf($textTpl,$fromuserName,$touserName,$time,$msgType,$content);
        echo $resultful;
        
    }
        
    
}else if ($postObj->MsgType == 'image'){
        $msgType = 'text';
        $content = 'output image';
        $resultful = sprintf($textTpl,$fromuserName,$touserName,$time,$msgType,$content);
        echo $resultful;
}else if ($postObj->MsgType == 'voice'){
        $msgType = 'text';
        $content = 'output voice';
        $resultful = sprintf($textTpl,$fromuserName,$touserName,$time,$msgType,$content);
        echo $resultful;

}else if ($postObj->MsgType == 'video'){
        $msgType = 'text';
        $content = 'output video';
        $resultful = sprintf($textTpl,$fromuserName,$touserName,$time,$msgType,$content);
        echo $resultful;

}else if ($postObj->MsgType == 'music'){
        
        $msgType = 'text';
        $content = 'output music';
        $resultful = sprintf($textTpl,$fromuserName,$touserName,$time,$msgType,$content);
        echo $resultful;

}else {
        $content = 'output tuwen';
        $resultful = sprintf($textTpl,$fromuserName,$touserName,$time,$msgType,$content);
        echo $resultful;
}

?>
原文地址:https://www.cnblogs.com/healy/p/6735830.html