if ($mysql->num_row == 0) { throw new Exception('App config not found'); } } $configs = array(); foreach ($mysql->data as $config) { $configs[$config['name']] = $config['value']; } $config = array('schema' => $schema, 'appid' => $configs['appid'], 'secret' => $configs['app_secret'], 'encrypt_token' => $configs['encrypt_token'], 'encodingAESKey' => $configs['encodingAESKey'], 'app_token' => null, 'app_token_expire' => null); # init Wechat API $wechat = new WeChat($config); // GET variables: timestamp, nonce, msg_signature, encrypt_type extract($_GET); if (isset($echostr)) { // for wechat API validation if ($wechat->validateAPI($echostr, $signature, $timestamp, $nonce)) { die($echostr); } else { throw new Exception('API validate failed'); } } // get POST data, request message from WeChat $raw_data = isset($GLOBALS["HTTP_RAW_POST_DATA"]) ? $GLOBALS["HTTP_RAW_POST_DATA"] : ''; if (!empty($raw_data)) { # decrypt message $dataArr = array('raw_data' => $raw_data, 'encrypt_type' => isset($encrypt_type) && $encrypt_type == 'aes' ? "aes" : "raw", 'msg_signature' => $msg_signature, 'timestamp' => $timestamp, 'nonce' => $nonce); $result = $wechat->decryptMessage($dataArr); if ($result['hasError']) { // decryption failed die; }