<?php
header('Content-Type:application/json; charset=utf-8');
define ("TOKEN","mdy");//将 mdy 替换为

include_once "checksignature.php";//判断请求来源与请求类型

//$encodingAesKey = "";
//$appId = "";

$textUrl = "";//转发用户发送消息的接口网址
$subscribeUrl = "";//转发用户关注取关事件
$eventUrl = "";//转发用户点击菜单按钮事件

$ReqData = isset($GLOBALS["HTTP_RAW_POST_DATA"])?$GLOBALS["HTTP_RAW_POST_DATA"]:file_get_contents("php://input");
$xmlDATA = simplexml_load_string($ReqData,'SimpleXMLElement',LIBXML_NOCDATA);
$jsonDATA = json_decode(json_encode($xmlDATA,JSON_FORCE_OBJECT,JSON_UNESCAPED_UNICODE),true);

if ($jsonDATA["MsgType"] == "text") {
    
        $postUrl = $textUrl;
    
} else {
    
        switch ($jsonDATA["Event"]) {
            
            case "subscribe":
                $postUrl = $subscribeUrl;
            break;
            case "unsubscribe":
                $postUrl = $subscribeUrl;
            break;
            default:
                $postUrl = $eventUrl;
            break;
            
        }
}

            $ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, $postUrl);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDATA);
			curl_exec($ch);
		    echo "";
		    
    $requestLog = array (
                "signature" => $signature,
                "timestamp" => $timestamp,
                "nonce" => $nonce,
                "echostr" => $echostr,
                "encrypt_type" => $encrypt_type,
                "msg_signature" => $msg_signature,
                "encodingAesKey" => $encodingAesKey,
                "text" => $text,
                "MsgId" => $MsgId,
                "jsonDATA" => $jsonDATA
                );
    //请求日志写在 request.log 中            
    $json = json_encode($requestLog,JSON_UNESCAPED_UNICODE);
    $file = fopen("request.log", "a");
    fwrite($file, date('Y-m-d H:i:s',time())." ".$jsonDATA."\n");
    die ("");       
                



