【项目需求】
通过微信提供的接口,实现微信公众号与后端的应用程序数据交互、消息响应等功能。
【项目疑难点】
- 理解接口工作方式,统一接口API,响应速度、安全性等
【代码举例】
WeixinApi.class.php 微信公众号接口基类
- <?php
-
- define(‘WXAPI_ERR_CONFIG‘, 1001);
- define(‘WXAPI_ERR_HTTP‘, 1002);
- define(‘WXAPI_ERR_LOGIN‘, 1003);
- define(‘WXAPI_ERR_FETCH_DATA‘, 1004);
- define(‘WXAPI_ERR_MISS_RESPONSE‘, 1005);
- define(‘WXAPI_ERR_BAD_SIGNATURE‘, 1006);
- define(‘WXAPI_ERR_BAD_DECRYPT‘, 1007);
- define(‘WXAPI_ERR_BAD_ENCRYPT‘, 1008);
- define(‘WXAPI_ERR_ACCESS_TOKEN‘, 1009);
-
- define(‘WXAPI_LOG_EMERG‘, ‘EMERG‘);
- define(‘WXAPI_LOG_ALERT‘, ‘ALERT‘);
- define(‘WXAPI_LOG_CRIT‘, ‘CRIT‘);
- define(‘WXAPI_LOG_ERR‘, ‘ERR‘);
- define(‘WXAPI_LOG_WARN‘, ‘WARN‘);
- define(‘WXAPI_LOG_NOTICE‘, ‘NOTIC‘);
- define(‘WXAPI_LOG_INFO‘, ‘INFO‘);
- define(‘WXAPI_LOG_DEBUG‘, ‘DEBUG‘);
- define(‘WXAPI_LOG_EXCEPTION‘, ‘EXCEPTION‘);
-
- define(‘WXAPI_ACCESS_TOKEN_EXPIRE‘, 7100);
- define(‘WXAPI_ACCESS_TOKEN_LIMIT‘, 2000);
- define(‘WXAPI_JSAPI_TICKET_EXPIRE‘, 7100);
- define(‘WXAPI_JSAPI_TICKET_LIMIT‘, 2000);
- define(‘WXAPI_QRCODE_MIN_SCENE‘, 1);
- define(‘WXAPI_QRCODE_MAX_SCENE‘, 2147483647);
- define(‘WXAPI_QRCODE_MAX_LIMIT_SCENE‘, 100000);
- define(‘WXAPI_QRCODE_EXPIRE‘, 1800);
- define(‘WXAPI_GROUP_MIN_CUSTOM_ID‘, 100);
-
- define(‘WXAPI_ARGOT_WHO_AM_I‘, ‘show me your name‘);
- define(‘WXAPI_ARGOT_DESTORY_SESSION‘, ‘let me out‘);
-
- class WeixinApi{
-
- protected static $_instance = array();
-
-
-
- protected $_cache = false;
-
-
- protected $_debug = false;
-
-
- public $Config;
-
-
- protected $_error = NULL;
-
- public function __construct($Config=NULL){
- $this->Config = is_object($Config)?$Config:self::instance(‘WeixinApi_Config‘);
- $this->_cache = $this->Config->Cache;
- }
-
-
- public static function instance($class,$args=array()) {
- $identify = $class.md5(serialize($args));
- if(!isset(WeixinApi::$_instance[$identify])) {
- if(!class_exists($class)){
- require $class . ".class.php";
- }
-
- if(class_exists($class)){
- $arg_str = ‘‘;
- if($args && is_array($args)){
- foreach ($args as $i=>$arg){
-
-
- if(is_object($arg) || is_array($arg)){
- $arg_param_name = ‘arg_param‘ . $i;
- $$arg_param_name = $arg;
- $arg_str .= ", \${$arg_param_name}";
- }else{
- $arg_str .= ", ‘" . addcslashes($arg, "‘") . "‘";
- }
- }
-
- if($arg_str){
- $arg_str = substr($arg_str, 2);
- }
-
- }elseif($args && is_object($args)){
-
- $arg_param_name = ‘arg_param‘;
- $$arg_param_name = $args;
- $arg_str = "\${$arg_param_name}";
-
- }elseif($args){
- $arg_str = "‘" . addcslashes($args, "‘") . "‘";
- }
-
- $code = "return new " . $class . "(" . $arg_str . ");";
- $o = eval($code);
-
- if(!$o){
- return WeixinApi::throw_exception(
- "Cann‘t init class instanse: $class"
- , WXAPI_ERR_CONFIG
- , array(‘class‘ => $class, ‘args‘ => $args)
- , __FILE__, __LINE);
- }
- WeixinApi::$_instance[$identify] = $o;
- }
- else{
- return WeixinApi::throw_exception(
- "Cann‘t found class: $class file."
- , WXAPI_ERR_CONFIG
- , array(‘class‘ => $class, ‘args‘ => $args)
- , __FILE__, __LINE__);
- }
- }
- return self::$_instance[$identify];
- }
-
- public static function throw_exception($message, $code=NULL, $data=NULL, $file=NULL, $line=NULL){
- if(!class_exists(‘WeixinApi_Exception‘)){
- require ‘WeixinApi_Exception.class.php‘;
- }
-
-
-
- throw new WeixinApi_Exception($message, $code, $data, $file, $line);
-
-
-
- }
-
- protected function _throw_exception($message, $code=NULL, $data=NULL, $file=NULL, $line=NULL){
- try{
- WeixinApi::throw_exception($message, $code, $data, $file, $line);
- }catch(Exception $e){
-
- $this->_setError($e->getMessage());
- $this->_log($e->__toString(), WXAPI_LOG_ERR);
-
-
- if($code==WXAPI_ERR_CONFIG){
- throw $e;
- }else{
- return false;
- }
- }
- }
-
- public function getError(){
- return is_array($this->_error)?implode(‘,‘, $this->_error):$this->_error;
- }
-
-
- protected function _setError($error){
- $this->_error[] = $error;
- }
-
- public function __get($n){
- if(isset($this->$n)){
- return $this->$n;
- }else if(in_array($n, array(‘Http‘, ‘Cache‘, ‘Log‘))){
- if(‘Http‘==$n && !$this->Config->$n){
- return $this->_throw_exception("$n is not setted in your config"
- , WXAPI_ERR_CONFIG
- , array(‘class‘=>$n)
- , __FILE__, __LINE__
- );
- }elseif(!$this->Config->$n){
-
-
- return false;
- }
-
- if(is_object($this->Config->$n)){
- return $this->Config->$n;
- }elseif(is_array($this->Config->$n)){
- list($callback, $params) = $this->Config->$n;
- if(!is_array($params)){
- $params = array($params);
- }
- return call_user_func_array($callback, $params);
- }else{
- return $this->$n = WeixinApi::instance($this->Config->$n);
- }
- }else{
- return false;
- }
- }
-
- protected function _check_http_url($url){
- if(strcasecmp(‘http‘, substr($url, 0, 4))){
- $url = $this->Config->ApiGateway . $url;
- }
-
- return $url;
- }
-
- protected function _check_http_ssl($url){
- if(!strcasecmp(‘https://‘, substr($url, 0, 8))){
- $this->Http->setSsl();
-
-
-
-
-
-
-
-
- defined(‘CURL_SSLVERSION_TLSv1‘) || define(‘CURL_SSLVERSION_TLSv1‘, 1);
- $this->Http->setOpt(CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
- }
-
- return $url;
- }
-
- protected function _check_http_data($data){
- return $data;
- }
-
-
- public function get($url, $data = null, $check=true) {
- if ($check) {
- $url = $this->_check_http_url ( $url );
- $url = $this->_check_http_ssl ( $url );
- $data = $this->_check_http_data ( $data );
- }
-
- if(!($return = $this->Http->get($url, $data)) && ($error=$this->Http->getError())){
- return $this->_throw_exception(
- $error
- , WXAPI_ERR_HTTP
- , array(‘url‘ => $url, ‘data‘ => $data, ‘method‘ => ‘get‘, ‘response‘ => $return)
- , __FILE__, __LINE__);
- }
-
-
- return $return;
- }
-
-
- public function post($url, $data, $check=true) {
- if ($check) {
- $url = $this->_check_http_url ( $url );
- $url = $this->_check_http_ssl ( $url );
- $data = $this->_check_http_data ( $data );
- }
-
-
- if(!($return = $this->Http->plainPost($url, $data)) && ($error=$this->Http->getError())){
- return $this->_throw_exception(
- $error
- , WXAPI_ERR_HTTP
- , array(‘url‘ => $url, ‘data‘ => $data, ‘method‘ => ‘post‘, ‘response‘ => $return)
- , __FILE__, __LINE__);
- }
-
- return $return;
- }
-
- public function setHttpOption($opt, $val=NULL){
- if(!$opt){
- return false;
- }
-
- $options = array();
- if(!is_array($opt)){
- $options = array($opt=>$val);
- }else{
- $options = $opt;
- }
-
- foreach($options as $opt=>$val){
- $this->Http->setOpt(constant($opt), $val);
- }
- }
-
-
- protected function _run_callback($callback, $extra_params=array(), &$callbackObject=NULL) {
- $extra_params = is_array ( $extra_params ) ? $extra_params : ($extra_params ? array (
- $extra_params
- ) : array ());
-
- $params = $extra_params;
-
- if(is_object($callback)){
- return $this->_throw_exception(
- "Object callback must set method"
- , SCRIPT_ERR_CONFIG
- , array(‘callback‘=>$callback)
- , __FILE__, __LINE__
- );
- }
- else if (is_array ( $callback )) {
- $func = $callback [0];
- if (! empty ( $callback [1] )) {
- if (is_array ( $callback [1] )) {
- $params = array_merge ( $extra_params, $callback [1] );
- } else {
- $params [] = $callback [1];
- }
- }
-
- if (is_object ( $func )) {
- $callbackObject = $func;
-
- return call_user_method_array ( $callback [1], $callback [0], $extra_params );
- } elseif (is_object ( $callback [0] [0] )) {
- $callbackObject = $callback [0] [0];
- return call_user_method_array ( $callback [0] [1], $callback [0] [0], $params);
- }
- } else {
- $func = $callback;
- }
-
- if(is_array($func) && is_array($func[0])){
- $call = call_user_func_array($func[0][0], is_array($func[0][1])?$func[0][1]:array($func[0][1]));
- if($call===false){
- return false;
- }
-
- $func = array($call, $func[1]);
- }
-
- if(is_array($func) && is_object($func[0])){
- $callbackObject = $func[0];
- }
-
- return call_user_func_array ( $func, $params);
- }
-
-
- public function cache($cache=true){
- $this->_cache = $cache;
- return $this;
- }
-
- public function debug($debug=true){
- $this->_debug = $debug;
- return $this;
- }
-
-
- protected function _cache($cache_id, $cache_data=NULL, $cache_expire=NULL){
- if($this->Config->Cache){
-
- if($cache_id && (!is_null($cache_data) && $cache_data!==false && $cache_expire!==false)
- && $this->Config->CacheSaveIndex
- && strcasecmp($cache_id, $this->Config->CacheSaveIndex)
- ){
- $index_cache_id = $this->Config->CacheSaveIndex;
- $index_cache_expire = 315360000;
-
-
- if(!($index_cache_data=$this->_cache($index_cache_id))){
- $index_cache_data = array();
- }
-
-
- $now_time = time();
- foreach($index_cache_data as $k=>$d){
- if($d && $d[‘expire‘] && $d[‘created‘] && ($d[‘created‘]+$d[‘expire‘])<$now_time){
- unset($index_cache_data[$k]);
- }
- }
-
- $index_cache_data[$cache_id] = array(
- ‘created‘ => $now_time,
- ‘expire‘ => $cache_expire,
- );
-
-
-
-
- $succ = $this->_cache($index_cache_id, $index_cache_data, $index_cache_expire);
- $this->_log("Save cache id: " . $cache_id . ‘ ‘ . ($succ?‘Succ‘:‘Failed‘) . ‘!‘, WXAPI_LOG_DEBUG);
- }
-
- return $this->_run_callback($this->Config->Cache, array($cache_id, $cache_data, $cache_expire));
- }else{
- return false;
- }
- }
-
- protected function _cache_id($url, $data = NULL, $cache = NULL) {
- if ($cache && $cache!==true && !is_numeric($cache)){
- if(is_string ( $cache )) {
- $cache_id = $cache;
- } elseif (is_array ( $cache ) && isset($cache[‘cache_id‘])) {
- $cache_id = $cache [‘cache_id‘];
- } elseif (is_object ( $cache ) && isset($cache[‘cache_id‘])) {
- $cache_id = $cache->cache_id;
- }
-
-
-
- }
-
- if (!$cache_id) {
- $param = ‘‘;
- if ($data && is_array ( $data )) {
- $param .= http_build_query ( $data );
- } else {
- $param .= $data;
- }
- $cache_id = md5 ( $this->Config->AppId . $url . $param );
-
- }
-
- return $cache_id;
- }
-
- protected function _cache_expire($url, $data=NULL, $cache=NULL){
- if(!$cache){
- return 0;
- }elseif(is_numeric($cache) && $cache>0){
- $cache_expire = $cache;
- }elseif (is_array($cache) && isset($cache[‘cache_expire‘])){
- $cache_expire = $cache[‘cache_expire‘];
- }elseif (is_object($cache) && isset($cache->cache_expire)){
- $cache_expire = $cache->cache_expire;
- }
-
- return $cache_expire?$cache_expire:$this->Config->CacheExpire;
- }
-
-
- protected function _cache_refresh($url, $data=NULL, $cache=NULL){
- $cache_refresh = false;
-
- if ($cache && $cache!==true && !is_numeric($cache)){
- if (is_array ( $cache ) && isset($cache[‘cache_refresh‘])) {
- $cache_refresh = $cache [‘cache_refresh‘];
- } elseif (is_object ( $cache ) && isset($cache[‘cache_refresh‘])) {
- $cache_refresh = $cache->cache_refresh;
- }
- }
-
- return $cache_refresh;
- }
-
-
- protected function _log($message, $level=WXAPI_LOG_INFO){
- if($this->Config->Log){
- static $aLogLevelMaps = array(
- WXAPI_LOG_EMERG => 0,
- WXAPI_LOG_ALERT => 1,
- WXAPI_LOG_CRIT => 2,
- WXAPI_LOG_ERR => 3,
- WXAPI_LOG_WARN => 4,
- WXAPI_LOG_NOTICE => 5,
- WXAPI_LOG_INFO => 6,
- WXAPI_LOG_DEBUG => 7,
- );
-
- if($this->Config->LogLevel && $aLogLevelMaps[$level]>$aLogLevelMaps[$this->Config->LogLevel]){
- return false;
- }
-
- return $this->_run_callback($this->Config->Log, array($message, $level));
- }else{
- return false;
- }
- }
-
-
-
- protected function _logpay($message, $level=WXAPI_LOG_INFO){
- if($this->Config->PayLog){
- static $aLogLevelMaps = array(
- WXAPI_LOG_EMERG => 0,
- WXAPI_LOG_ALERT => 1,
- WXAPI_LOG_CRIT => 2,
- WXAPI_LOG_ERR => 3,
- WXAPI_LOG_WARN => 4,
- WXAPI_LOG_NOTICE => 5,
- WXAPI_LOG_INFO => 6,
- WXAPI_LOG_DEBUG => 7,
- );
-
- if($this->Config->PayLogLevel && $aLogLevelMaps[$level]>$aLogLevelMaps[$this->Config->PayLogLevel]){
- return false;
- }
-
- return $this->_run_callback($this->Config->PayLog, array($message, $level));
- }else{
- return false;
- }
- }
-
-
- protected function _isMediaId($mediaid){
-
-
- if(preg_match(‘/\.[a-z0-9]{1,4}$/i‘, $mediaid)){
- return false;
- }else{
- return true;
- }
- }
-
-
- public function clearCache(){
-
- $this->_log("START Clear Cache...", WXAPI_LOG_INFO);
- if(!$this->Config->Cache || !$this->Config->CacheSaveIndex){
- $this->_log("Skipped, Cache or Save Cache index is disabled!", WXAPI_LOG_INFO);
- return false;
- }
-
-
- $index_cache_id = $this->Config->CacheSaveIndex;
- if(!($index_cache_data=$this->_cache($index_cache_id))){
- $this->_log("Skipped, Cache Index is Empty!", WXAPI_LOG_INFO);
- return false;
- }
-
- $clear_succ = true;
-
- foreach($index_cache_data as $cache_id=>$d){
- $succ = $this->_cache($cache_id, false, false);
- $this->_log("Delete cache id: " . $cache_id . " " . ($succ?‘Succ‘:‘Failed‘) . ‘!‘, WXAPI_LOG_DEBUG);
-
- $clear_succ = $succ && $clear_succ;
- }
-
-
- $succ = $this->_cache($index_cache_id, false, false);
- $clear_succ = $succ && $clear_succ;
-
- $this->_log("Delete Index Cache Id: " . $index_cache_id . " " . ($succ?‘Succ‘:‘Failed‘) . ‘!‘, WXAPI_LOG_INFO);
-
- $this->_log("END Clear Cache, " . ($clear_succ?‘Succ‘:‘Failed‘) . ‘!‘, WXAPI_LOG_INFO);
-
- return $clear_succ;
- }
- }
WeixinReceive.class.php 微信接口接收类
- <?php
-
- class WeixinReceive extends WeixinApi{
- protected $_rawget = NULL;
- protected $_rawpost = NULL;
-
- protected $_postData = NULL;
- protected $_getData = NULL;
-
- protected $_postObj = NULL;
- protected $_getObj = NULL;
-
- protected $_responseMsg;
- protected $_responseObj;
-
-
- protected $_msgEncodingMode = NULL;
-
-
- protected $_msgEncodingKey = NULL;
-
-
- protected $_msgEncrypt = NULL;
-
-
- protected $_msgDecrypt = NULL;
-
-
- protected $_msgData = NULL;
-
-
-
-
- protected function _checkSignature($getData)
- {
- $signature = $getData[‘signature‘];
- $timestamp = $getData[‘timestamp‘];
- $nonce = $getData[‘nonce‘];
-
- $token = $this->Config->AppToken;
- $tmpArr = array($token, $timestamp, $nonce);
- sort($tmpArr, SORT_STRING);
- $tmpStr = implode( $tmpArr );
- $tmpStr = sha1( $tmpStr );
-
- if( $tmpStr == $signature ){
- return true;
- }else{
- return false;
- }
- }
-
-
- protected function _checkEncodingMode($getData, $postData){
- if(!is_null($this->_msgEncodingMode)){
- return $this->_msgEncodingMode;
- }
-
- if(empty($getData[‘encrypt_type‘]) || !strcasecmp($getData[‘encrypt_type‘], ‘raw‘)){
- $this->_msgEncodingMode = WXAPI_APP_ENCODING_CLEAR;
- }elseif(strlen($getData[‘msg_signature‘]) && !strcasecmp($getData[‘encrypt_type‘], ‘aes‘)){
- if(!empty($postData[‘MsgType‘]) && !empty($postData[‘FromUserName‘])){
- $this->_msgEncodingMode = WXAPI_APP_ENCODING_COMPAT;
- }else{
- $this->_msgEncodingMode = WXAPI_APP_ENCODING_SECURE;
- }
- }else{
- $this->_msgEncodingMode = false;
- }
-
- return $this->_msgEncodingMode;
- }
-
- protected function _postData(){
- if(!is_null($this->_postData)){
- return $this->_postData;
- }
-
- $this->_rawpost = file_get_contents("php://input");
-
- if(!empty($this->_rawpost)){
- $postObj = simplexml_load_string(trim($this->_rawpost), ‘SimpleXMLElement‘, LIBXML_NOCDATA);
-
- $this->_postData = WeixinApi_Kit::get_object_vars_final($postObj);
-
-
-
-
- $this->_postObj = (object) $this->_postData;
- }else{
- $this->_postData = false;
- $this->_postObj = false;
- }
-
- return $this->_postData;
-
- }
-
- protected function _getData(){
- if(!is_null($this->_getData)){
- return $this->_getData;
- }
-
- $this->_rawget = $_GET;
- if ($this->_rawget) {
- $getData = array (
- ‘signature‘ => $_GET ["signature"],
- ‘timestamp‘ => $_GET ["timestamp"],
- ‘nonce‘ => $_GET ["nonce"]
- );
-
- if (isset ( $_GET [‘echostr‘] )) { $getData [‘echostr‘] = $_GET [‘echostr‘]; }
- if (isset ( $_GET [‘encrypt_type‘] )) { $getData [‘encrypt_type‘] = $_GET [‘encrypt_type‘]; }
- if (isset ( $_GET [‘msg_signature‘] )) { $getData [‘msg_signature‘] = $_GET [‘msg_signature‘]; }
-
- $this->_getData = $getData;
-
-
- $this->_getObj = ( object ) $getData;
- }else{
- $this->_getData = false;
-
- $this->_getObj = false;
- }
-
- return $this->_getData;
- }
-
-
- public function run($responseObj=NULL){
- $request_url = WeixinApi_Kit::get_request_url();
- $client_ip = WeixinApi_Kit::get_client_ip();
-
- $this->_log("--------------------------------------------------------");
- $this->_log("Received new request from {$client_ip}", WXAPI_LOG_INFO);
- $this->_log("Request URL: {$request_url}", WXAPI_LOG_INFO);
-
- $this->_log("Get: " . print_r($_GET, true), WXAPI_LOG_DEBUG);
- $this->_log("Post: " . print_r($_POST, true), WXAPI_LOG_DEBUG);
-
- $getData = $this->_getData();
-
-
- if(!$getData || !$this->_checkSignature($getData)){
-
-
- $this->_log("Bad Request, Check Signature Failed!", WXAPI_LOG_ERR);
- return false;
- }
-
- $postData = $this->_postData();
-
-
- if(false==$postData){
- $this->_log("Msg Body is Empty!", WXAPI_LOG_ERR);
- return false;
- }
-
- $this->_log ( "rawPost: " . $this->_rawpost, WXAPI_LOG_DEBUG );
- $this->_log ( "postData: " . print_r ( $postData, true ), WXAPI_LOG_DEBUG );
-
-
- $encodingMode = $this->_checkEncodingMode($getData, $postData);
- if(false==$encodingMode){
- $this->_log("Check Msg Encoding Mode Failed!", WXAPI_LOG_ERR);
- return false;
- }
-
- $this->_log("MSG Encoding Mode is: " . $encodingMode, WXAPI_LOG_DEBUG);
-
-
- switch($encodingMode){
- case WXAPI_APP_ENCODING_SECURE:
- if(false===$this->_decodeMessage()){
- $this->_log("Bad Request, Decode Message Failed!", WXAPI_LOG_ERR);
- return false;
- }else{
- $this->_log("Decode Message Succ!", WXAPI_LOG_INFO);
- }
- break;
-
- case WXAPI_APP_ENCODING_COMPAT:
- if(false===$this->_decodeMessage()){
- $this->_log("Decode Message Failed!", WXAPI_LOG_ERR);
- }else{
- $this->_log("Decode Message Succ!", WXAPI_LOG_INFO);
- }
-
- break;
-
- default:
-
- break;
- }
-
- if (empty ( $responseObj )) {
- $responseObj = $this->Config->Response;
- }
-
-
- $response = $this->_responseMsg = $this->_response ( $responseObj );
-
- if ($response === false) {
- $this->_log ( "No Reponse Sent!", WXAPI_LOG_INFO );
-
-
- $this->_saveMessage ();
-
- return false;
- }
-
-
- echo $response;
- flush ();
-
-
- $this->_log ( "Succ! Send Response: " . $response, WXAPI_LOG_INFO );
-
-
- $this->_saveMessage ();
-
-
- $this->_saveResponse ( $this->_responseObj );
-
- return true;
- }
-
- protected function _response($responseObj){
- if(is_object($responseObj)){
- $callback = array($responseObj, ‘run‘);
- }else{
- $callback = $responseObj;
- }
-
- return $this->_run_callback($callback, array($this), $this->_responseObj);
- }
-
-
- protected function _saveMessage(){
- if($this->Config->SaveMessage){
- return $this->_run_callback($this->Config->SaveMessage, array($this));
- }else{
- return false;
- }
- }
-
-
- protected function _saveResponse($responseObj){
- if($this->Config->SaveResponse){
- return $this->_run_callback($this->Config->SaveResponse, array($this, $responseObj));
- }else{
- return false;
- }
- }
-
- public function __get($c){
- if(substr($c, 0, 1)!=‘_‘){
- if(in_array($c, array(‘Http‘))){
- return parent::__get($c);
- }else{
- $n = ‘_‘ . $c;
- return $this->$n;
- }
- }
- }
-
- public function __isset($c){
- if(substr($c, 0, 1)!=‘_‘){
- if(in_array($c, array(‘Http‘))){
- return parent::__isset($c);
- }else{
- $n = ‘_‘ . $c;
- return isset($this->$n);
- }
- }
- }
-
-
- public function parse_openid(){
- if(isset($this->_postObj->FromUserName) && !empty($this->_postObj->FromUserName)){
- return $this->_postObj->FromUserName;
- }else{
- return null;
- }
- }
-
-
- protected function _decryptMsg($msg_encrypt, $encodingkey=NULL)
- {
- $AESKey = base64_decode(($encodingkey?$encodingkey:$this->Config->AppEncodingAESKey) . "=");
-
-
- $ciphertext_dec = base64_decode($msg_encrypt);
-
- $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, ‘‘, MCRYPT_MODE_CBC, ‘‘);
- if(false===$module){
- return $this->_throw_exception(
- "Cann‘t open an encryption descriptor"
- , WXAPI_ERR_BAD_ENCRYPT
- , $msg_encrypt
- , __FILE__, __LINE__
- );
- }
-
- $iv = substr($AESKey, 0, 16);
- $init = mcrypt_generic_init($module, $AESKey, $iv);
- if(false===$init){
- return $this->_throw_exception(
- "Cann‘t initialize buffers for encryption"
- , WXAPI_ERR_BAD_ENCRYPT
- , array(‘msg_encrypt‘ => $msg_encrypt, ‘mcrypt_generic_init return‘ => $init)
- , __FILE__, __LINE__
- );
- }elseif(-3==$init){
- return $this->_throw_exception(
- "the key length was incorrect"
- , WXAPI_ERR_BAD_ENCRYPT
- , array(‘msg_encrypt‘ => $msg_encrypt, ‘mcrypt_generic_init return‘ => $init)
- , __FILE__, __LINE__
- );
- }elseif(-4==$init){
- return $this->_throw_exception(
- "there was a memory allocation problem"
- , WXAPI_ERR_BAD_ENCRYPT
- , array(‘msg_encrypt‘ => $msg_encrypt, ‘mcrypt_generic_init return‘ => $init)
- , __FILE__, __LINE__
- );
- }elseif($init<0){
- return $this->_throw_exception(
- "an unknown error occurred when initialize buffers for encryption"
- , WXAPI_ERR_BAD_ENCRYPT
- , array(‘msg_encrypt‘ => $msg_encrypt, ‘mcrypt_generic_init return‘ => $init)
- , __FILE__, __LINE__
- );
- }
-
-
- $decrypted = mdecrypt_generic($module, $ciphertext_dec);
- mcrypt_generic_deinit($module);
- mcrypt_module_close($module);
-
- if(!$decrypted){
- return "";
- }
-
-
- $result = WeixinApi_Kit::pkcs7_decode( $decrypted, 32 );
-
- if (strlen ( $result ) < 16){
- return "";
- }
-
- $content = substr ( $result, 16, strlen ( $result ) );
- $len_list = unpack ( "N", substr ( $content, 0, 4 ) );
- $xml_len = $len_list [1];
- $xml_content = substr ( $content, 4, $xml_len );
-
- return $xml_content;
- }
-
-
- protected function _getMsgEncrypt(){
- if($this->_msgEncrypt){
- return $this->_msgEncrypt;
- }
-
- if(!empty($this->_getData[‘echostr‘])){
- $this->_msgEncrypt = $this->_getData[‘echostr‘];
- }else{
- $this->_msgEncrypt = $this->_postData[‘Encrypt‘];
- }
-
- return $this->_msgEncrypt;
- }
-
- protected function _msgData() {
- if (! is_null ( $this->_msgData )) {
- return $this->_msgData;
- }
-
- $this->_msgData = false;
-
- $msg_encrypt = $this->_getMsgEncrypt ();
- if ($msg_encrypt) {
- if(!empty($this->_getData[‘echostr‘])){
- $this->_msgData = array();
- }else{
- $xml_content = false;
-
- $encodingkey = $this->Config->AppEncodingAESKey;
- if($encodingkey){
- $xml_content = $this->_decryptMsg ( $msg_encrypt, $encodingkey );
- if($xml_content){
- $this->_msgEncodingKey = $encodingkey;
- $this->_log("AES Key: Decode Succ! ", WXAPI_LOG_DEBUG);
- }else{
- $this->_log("AES Key: Decode Failed!", WXAPI_LOG_DEBUG);
- }
- }else{
- $this->_log("Encoding AES Key is empty", WXAPI_LOG_DEBUG);
- }
-
-
- if(!$xml_content && ($encodingkey = $this->Config->AppEncodingOLDKey)){
- $xml_content = $this->_decryptMsg ( $msg_encrypt, $encodingkey );
-
- $this->_log("Try to apply OLD Key", WXAPI_LOG_DEBUG);
-
- if($xml_content){
- $this->_msgEncodingKey = $encodingkey;
- $this->_log("OLD Key: Decode Succ! ", WXAPI_LOG_DEBUG);
- }else{
- $this->_log("OLD Key: Decode Failed!", WXAPI_LOG_DEBUG);
- }
- }
-
- if($xml_content){
- $this->_msgDecrypt = $xml_content;
-
- import ( ‘COM.GZNC.WeixinApi.WeixinApi_Kit‘ );
- $postObj = simplexml_load_string ( $xml_content, ‘SimpleXMLElement‘, LIBXML_NOCDATA );
- $this->_msgData = WeixinApi_Kit::get_object_vars_final ( $postObj );
-
- $this->_log(‘Decoded MSG XML: ‘ . $this->_msgDecrypt, WXAPI_LOG_DEBUG);
- $this->_log(‘Decoded MSG DATA: ‘ . print_r($this->_msgData, true), WXAPI_LOG_DEBUG);
- }
- }
- }
-
- return $this->_msgData;
- }
-
- protected function _decodeMessage(){
- if(false===$this->_msgData()){
- return false;
- }
-
-
- $this->_postData = array_merge($this->_postData, $this->_msgData);
- $this->_postObj = (object) $this->_postData;
-
- return true;
- }
-
-
- }
WeixinResponse.class.php 微信接口响应类
- <?php
-
- class WeixinResponse extends WeixinApi{
- const AS_ECHO = ‘ECHO‘;
- const AS_EMPTY = ‘EMPTY‘;
- const AS_COMMAND = ‘COMMAND‘;
- const AS_SUBSCRIBE = ‘SUBSCRIBE‘;
- const AS_UNSUBSCRIBE = ‘UNSUBSCRIBE‘;
- const AS_SCAN = ‘SCAN‘;
- const AS_CLICK = ‘CLICK‘;
- const AS_VIEW = ‘VIEW‘;
- const AS_SCANCODE_PUSH = ‘SCANCODE_PUSH‘;
- const AS_SCANCODE_WAITMSG = ‘AS_SCANCODE_WAITMSG‘;
- const AS_PIC_SYSPHOTO = ‘pic_sysphoto‘;
- const AS_PIC_PHOTO_OR_ALBUM = ‘PIC_PHOTO_OR_ALBUM‘;
- const AS_PIC_WEIXIN = ‘pic_weixin‘;
- const AS_LOCATION_SELECT = ‘location_select‘;
- const AS_LOCATION = ‘LOCATION‘;
- const AS_MESSAGE = ‘MESSAGE‘;
- const AS_MASSSENDJOBFINISH = ‘MASSSENDJOBFINISH‘;
- const AS_TEMPLATESENDJOBFINISH = ‘TEMPLATESENDJOBFINISH‘;
-
- const AS_UNKNOWN = ‘UNKNOWN‘;
-
- protected $_responseType;
- protected $_responseKey;
- protected $_responseContent;
- protected $_responseMessage;
- protected $_responseMedia;
-
-
- public function run($receiveObj){
- try{
- return $this->_dispatchResponse($receiveObj);
- }catch (Exception $e){
- return false;
- }
- }
-
-
- protected function _dispatchResponse($receiveObj){
-
- if($this->_isEcho($receiveObj)){
- $this->_responseType = WeixinResponse::AS_ECHO;
- $this->_responseKey = ‘‘;
- $this->_responseContent = $this->_responseEcho($receiveObj);
- $msgFormat = ‘raw‘;
- }elseif($this->_isEmpty($receiveObj)){
- $this->_responseType = WeixinResponse::AS_EMPTY;
- $this->_responseKey = ‘‘;
- $this->_responseContent = $this->_responseEmpty($receiveObj);
- $msgFormat = ‘raw‘;
- }elseif(($key=$this->_isView($receiveObj))){
- $this->_responseType = WeixinResponse::AS_VIEW;
- $this->_responseKey = $key===true?‘‘:$key;
- $this->_responseContent = $this->_responseView($receiveObj);
- $msgFormat = ‘xml‘;
- }
-
-
-
- elseif(($key=$this->_isSubscribe($receiveObj))){
- $this->_responseType = WeixinResponse::AS_SUBSCRIBE;
- $this->_responseKey = $key===true?‘‘:$key;
- $this->_responseContent = $this->_responseSubscribe($receiveObj);
- $msgFormat = ‘xml‘;
- }elseif(($key=$this->_isUnsubscribe($receiveObj))){
- $this->_responseType = WeixinResponse::AS_UNSUBSCRIBE;
- $this->_responseKey = $key===true?‘‘:$key;
- $this->_responseContent = $this->_responseUnsubscribe($receiveObj);
- $msgFormat = ‘xml‘;
- }elseif(($key=$this->_isClick($receiveObj))){
- $this->_responseType = WeixinResponse::AS_CLICK;
- $this->_responseKey = $key===true?‘‘:$key;
- $this->_responseContent = $this->_responseClick($receiveObj);
- $msgFormat = ‘xml‘;
- }elseif(($key=$this->_isScanCodePush($receiveObj))){
- $this->_responseType = WeixinResponse::AS_SCANCODE_PUSH;
- $this->_responseKey = $key===true?‘‘:$key;
- $this->_responseContent = $this->_responseScancodePush($receiveObj);
- $msgFormat = ‘xml‘;
- }elseif(($key=$this->_isScanCodeWaitMsg($receiveObj))){
- $this->_responseType = WeixinResponse::AS_SCANCODE_WAITMSG;
- $this->_responseKey = $key===true?‘‘:$key;
- $this->_responseContent = $this->_responseScanCodeWaitMsg($receiveObj);
- $msgFormat = ‘xml‘;
- }elseif(($key=$this->_isPicSysPhoto($receiveObj))){
- $this->_responseType = WeixinResponse::AS_PIC_SYSPHOTO;
- $this->_responseKey = $key===true?‘‘:$key;
- $this->_responseContent = $this->_responsePicSysPhoto($receiveObj);
- $msgFormat = ‘xml‘;
- }elseif(($key=$this->_isPicPhotoOrAlbum($receiveObj))){
- $this->_responseType = WeixinResponse::AS_PIC_PHOTO_OR_ALBUM;
- $this->_responseKey = $key===true?‘‘:$key;
- $this->_responseContent = $this->_responsePicPhotoOrAlbum($receiveObj);
- $msgFormat = ‘xml‘;
- }elseif(($key=$this->_isPicWeixin($receiveObj))){
- $this->_responseType = WeixinResponse::AS_PIC_WEIXIN;
- $this->_responseKey = $key===true?‘‘:$key;
- $this->_responseContent = $this->_responsePicWeixin($receiveObj);
- $msgFormat = ‘xml‘;
- }elseif(($key=$this->_isLocationSelect($receiveObj))){
- $this->_responseType = WeixinResponse::AS_LOCATION_SELECT;
- $this->_responseKey = $key===true?‘‘:$key;
- $this->_responseContent = $this->_responseLocationSelect($receiveObj);
- $msgFormat = ‘xml‘;
- }elseif(($key=$this->_isScan($receiveObj))){
- $this->_responseType = WeixinResponse::AS_SCAN;
- $this->_responseKey = $key===true?‘‘:$key;
- $this->_responseContent = $this->_responseScan($receiveObj);
- $msgFormat = ‘xml‘;
- }elseif(($key=$this->_isLocation($receiveObj))){
- $this->_responseType = WeixinResponse::AS_LOCATION;
- $this->_responseKey = $key===true?‘‘:$key;
- $this->_responseContent = $this->_responseLocation($receiveObj);
- $msgFormat = ‘xml‘;
- }elseif(($key=$this->_isMassSendJobFinish($receiveObj))){
- $this->_responseType = WeixinResponse::AS_MASSSENDJOBFINISH;
- $this->_responseKey = $key===true?‘‘:$key;
- $this->_responseContent = $this->_responseMassSendJobFinish($receiveObj);
- $msgFormat = ‘xml‘;
- }elseif(($key=$this->_isTemplateSendJobFinish($receiveObj))){
- $this->_responseType = WeixinResponse::AS_TEMPLATESENDJOBFINISH;
- $this->_responseKey = $key===true?‘‘:$key;
- $this->_responseContent = $this->_responseTemplateSendJobFinish($receiveObj);
- $msgFormat = ‘xml‘;
- }
-
-
- elseif(($key=$this->_isCommand($receiveObj))){
- $this->_responseType = WeixinResponse::AS_COMMAND;
- $this->_responseKey = $key===true?‘‘:$key;
- $this->_responseContent = $this->_responseCommand($receiveObj);
- $msgFormat = ‘xml‘;
- }elseif(($key=$this->_isMessage($receiveObj))){
- $this->_responseType = WeixinResponse::AS_MESSAGE;
- $this->_responseKey = $key===true?‘‘:$key;
- $this->_responseContent = $this->_responseMessage($receiveObj);
- $msgFormat = ‘xml‘;
- }
-
-
- else{
- $this->_responseType = WeixinResponse::AS_UNKNOWN;
- $this->_responseKey = ‘‘;
- $this->_responseContent = $this->_responseUnknown($receiveObj);
- $msgFormat = ‘raw‘;
- }
-
- if($this->_responseContent===false){
-
- return false;
- }
-
- $this->_log("ResponseType: " . $this->_responseType, WXAPI_LOG_DEBUG);
- $this->_log("ResponseKey: " . $this->_responseKey, WXAPI_LOG_DEBUG);
- $this->_log("ResponseContent: " . print_r($this->_responseContent, true), WXAPI_LOG_DEBUG);
-
- $this->_responseMessage = $this->_createResponseMessage(
- $receiveObj,
- $this->_responseContent,
- $msgFormat
- );
-
- $this->_log("Generated Response Message: " . print_r($this->_responseMessage, true), WXAPI_LOG_DEBUG);
-
- return $this->_responseMessage[‘MsgContent‘];
- }
-
-
- protected function _isEmpty($receiveObj){
-
-
-
- $postObj = $receiveObj->postObj;
- return empty($postObj)?true:false;
- }
-
-
- protected function _isEcho($receiveObj){
- return isset($receiveObj->getObj->echostr) && $receiveObj->getObj->echostr;
- }
-
-
- protected function _isCommand($receiveObj){
- $aMsgTypes = array(
- ‘text‘
- );
-
- if(!in_array($receiveObj->postObj->MsgType, $aMsgTypes, false)){
- return false;
- }
-
- $command = trim($receiveObj->postObj->Content);
- if(($c=$this->Config->Command) && !empty($c[$command])){
- return $command;
- }else{
- return false;
- }
- }
-
-
- protected function _isEvent($receiveObj){
- return !strcasecmp($receiveObj->postObj->MsgType, ‘event‘);
- }
-
-
- protected function _isSubscribe($receiveObj){
- if($this->_isEvent($receiveObj) && !strcasecmp($receiveObj->postObj->Event, ‘subscribe‘)){
- return isset($receiveObj->postObj->EventKey) && ($key=(string)$receiveObj->postObj->EventKey)?
- $key:true;
- }else{
- return false;
- }
- }
-
-
- protected function _isUnsubscribe($receiveObj){
- return $this->_isEvent($receiveObj) && !strcasecmp($receiveObj->postObj->Event, ‘unsubscribe‘);
- }
-
-
- protected function _isScan($receiveObj){
- if($this->_isEvent($receiveObj) && !strcasecmp($receiveObj->postObj->Event, ‘scan‘)){
- return array(
- ‘EventKey‘ => $receiveObj->postObj->EventKey,
- ‘Ticket‘ => $receiveObj->postObj->Ticket,
- );
- }else{
- return false;
- }
- }
-
-
- protected function _isLocation($receiveObj){
- if(!strcasecmp($receiveObj->postObj->MsgType, ‘location‘)){
- return array(
- ‘Latitude‘ => $receiveObj->postObj->Location_Y,
- ‘Longitude‘ => $receiveObj->postObj->Location_X,
- ‘Precision‘ => $receiveObj->postObj->Scale,
- ‘Label‘ => $receiveObj->postObj->Label,
- );
- }elseif($this->_isEvent($receiveObj) && !strcasecmp($receiveObj->postObj->Event, ‘LOCATION‘)){
- return array(
- ‘Latitude‘ => $receiveObj->postObj->Latitude,
- ‘Longitude‘ => $receiveObj->postObj->Longitude,
- ‘Precision‘ => $receiveObj->postObj->Precision,
- ‘Label‘ => ‘‘,
- );
- }else{
- return false;
- }
- }
-
-
- protected function _isClick($receiveObj){
- if($this->_isEvent($receiveObj) && !strcasecmp($receiveObj->postObj->Event, ‘CLICK‘)){
- return $receiveObj->postObj->EventKey;
- }else{
- return false;
- }
- }
-
-
- protected function _isView($receiveObj){
- if($this->_isEvent($receiveObj) && !strcasecmp($receiveObj->postObj->Event, ‘VIEW‘)){
- return $receiveObj->postObj->EventKey;
- }else{
- return false;
- }
- }
-
-
- protected function _isScanCodePush($receiveObj){
- if($this->_isEvent($receiveObj) && !strcasecmp($receiveObj->postObj->Event, ‘scancode_push‘)){
- return $receiveObj->postObj->EventKey;
- }else{
- return false;
- }
- }
-
-
- protected function _isScanCodeWaitMsg($receiveObj){
- if($this->_isEvent($receiveObj) && !strcasecmp($receiveObj->postObj->Event, ‘scancode_waitmsg‘)){
- return $receiveObj->postObj->EventKey;
- }else{
- return false;
- }
- }
-
-
- protected function _isPicSysPhoto($receiveObj){
- if($this->_isEvent($receiveObj) && !strcasecmp($receiveObj->postObj->Event, ‘pic_sysphoto‘)){
- return $receiveObj->postObj->EventKey;
- }else{
- return false;
- }
- }
-
-
- protected function _isPicPhotoOrAlbum($receiveObj){
- if($this->_isEvent($receiveObj) && !strcasecmp($receiveObj->postObj->Event, ‘pic_photo_or_album‘)){
- return $receiveObj->postObj->EventKey;
- }else{
- return false;
- }
- }
-
-
- protected function _isPicWeixin($receiveObj){
- if($this->_isEvent($receiveObj) && !strcasecmp($receiveObj->postObj->Event, ‘pic_weixin‘)){
- return $receiveObj->postObj->EventKey;
- }else{
- return false;
- }
- }
-
-
- protected function _isLocationSelect($receiveObj){
- if($this->_isEvent($receiveObj) && !strcasecmp($receiveObj->postObj->Event, ‘location_select‘)){
- return $receiveObj->postObj->EventKey;
- }else{
- return false;
- }
- }
-
-
- protected function _isMessage($receiveObj){
- $aMsgTypes = array(
- ‘text‘, ‘image‘, ‘voice‘, ‘video‘, ‘link‘,‘shortvideo‘
- );
-
- return in_array($receiveObj->postObj->MsgType, $aMsgTypes, false)?$receiveObj->postObj->MsgType:false;
- }
-
-
- protected function _isMassSendJobFinish($receiveObj){
- if($this->_isEvent($receiveObj) && !strcasecmp($receiveObj->postObj->Event, ‘MASSSENDJOBFINISH‘)){
- return $receiveObj->postObj->MsgID;
-
- }else{
- return false;
- }
- }
-
-
- protected function _isTemplateSendJobFinish($receiveObj){
- if($this->_isEvent($receiveObj) && !strcasecmp($receiveObj->postObj->Event, ‘TEMPLATESENDJOBFINISH‘)){
- return $receiveObj->postObj->MsgID;
- }else{
- return false;
- }
- }
-
-
- protected function _createResponseMessage($receiveObj, $responseContent, $msgFormat=‘xml‘){
-
- if(is_array($responseContent) && !empty($responseContent[‘Callback‘])){
- $data = $this->_run_callback($responseContent[‘Callback‘], array($receiveObj, $this));
-
- if($data===false){
- $this->_log("Run Callback : " . print_r($responseContent[‘Callback‘], true) . " Failed", WXAPI_LOG_ERR);
- return false;
- }
-
- if(is_array($data)){
- $t = $data;
- $responseContent = array(
- ‘MsgType‘ => $t[‘MsgType‘],
- ‘Content‘ => $t[‘Content‘],
- );
- }else{
- $responseContent[‘Content‘] = $data;
- if($responseContent[‘MsgType‘]==‘callback‘){
- $responseContent[‘MsgType‘] = ‘text‘;
- }
- }
- }
-
- if(is_string($responseContent)){
- $responseContent = array(
- ‘MsgType‘ => ‘text‘,
- ‘Content‘ => $responseContent,
- );
- }elseif(!$responseContent[‘MsgType‘]){
- $responseContent[‘MsgType‘] = ‘text‘;
- }
-
- if(!$responseContent[‘Content‘] && !strlen($responseContent[‘Content‘])
- && strcasecmp(‘transfer_customer_service‘, $responseContent[‘MsgType‘])
- ){
- return false;
- }
-
-
- if($msgFormat==‘xml‘){
- $responseContent = $this->_preprocessResponseMedia($responseContent);
- }
-
- $msgContentOutput = self::generateMessage($receiveObj->postData[‘FromUserName‘], $receiveObj->postData[‘ToUserName‘], $responseContent);
-
-
- switch($receiveObj->msgEncodingMode){
-
- case WXAPI_APP_ENCODING_COMPAT:
-
- if(empty($receiveObj->msgEncodingKey)){
- $msgEncoding = WXAPI_APP_ENCODING_CLEAR;
-
- $this->_log("Encoding Response Msg in Clear Mode", WXAPI_LOG_DEBUG);
- break;
- }
-
-
- case WXAPI_APP_ENCODING_SECURE:
- $msgContentOriginal = $msgContentOutput;
- $msgContentOutput = self::_encrypt_response($msgContentOutput, $receiveObj->msgEncodingKey);
- $msgEncoding = WXAPI_APP_ENCODING_SECURE;
-
- $this->_log("Encoding Response Msg in Secure Mode", WXAPI_LOG_DEBUG);
- break;
-
-
- case WXAPI_APP_ENCODING_CLEAR:
- default:
- $msgEncoding = WXAPI_APP_ENCODING_CLEAR;
-
- $this->_log("Encoding Response Msg In Clear Mode", WXAPI_LOG_DEBUG);
- break;
- }
-
- return array(
- ‘MsgType‘ => $responseContent[‘MsgType‘],
- ‘MsgFormat‘ => $msgFormat,
- ‘MsgContent‘ => $msgFormat==‘xml‘?$msgContentOutput: $responseContent[‘Content‘],
- ‘MsgOriginal‘ => $msgContentOriginal?$msgContentOriginal:NULL,
- ‘MsgEncoding‘ => $msgEncoding,
- ‘RawContent‘ => $responseContent[‘Content‘]
- );
- }
-
-
- protected function _responseError($receiveObj){
- return "Error";
- }
-
-
- protected function _responseUnknown($receiveObj){
-
- }
-
-
- protected function _responseEcho($receiveObj){
- return $receiveObj->getObj->echostr;
- }
-
-
- protected function _responseEmpty($receiveObj){
- return "Empty";
- }
-
-
- protected function _responseCommand($receiveObj){
- $command = trim($receiveObj->postObj->Content);
- $settings = $this->Config->getConfig(‘Command‘);
- if(!isset($settings[$command])){
- return $this->_throw_exception("Command {$command} not configured", WXAPI_ERR_MISS_RESPONSE, ‘‘, __FILE__, __LINE__);
- }
- $content = $settings[$command];
- return $content;
- }
-
-
- protected function _responseEvent($receiveObj){
- $event = strtolower($receiveObj->postObj->Event);
-
- $settings = $this->Config->getConfig(‘Event‘);
- if(!isset($settings[$event])){
- return $this->_throw_exception("Miss resoponse for Event {$event}", WXAPI_ERR_MISS_RESPONSE, ‘‘, __FILE__, __LINE__);
- }
-
- if(isset($settings[$event][‘MsgType‘])){
- $content = $settings[$event];
- }elseif(isset($receiveObj->postObj->EventKey)){
- $eventkey = (string) $receiveObj->postObj->EventKey;
- if(!isset($settings[$event][$eventkey])){
- return $this->_throw_exception("Miss response for Event {$event}, Key {$eventkey}", WXAPI_ERR_MISS_RESPONSE, ‘‘, __FILE__, __LINE__);
- }
-
- $content = $settings[$event][$eventkey];
- }
-
- return $content;
- }
-
-
- protected function _responseSubscribe($receiveObj){
- return $this->_responseEvent($receiveObj);
- }
-
-
- protected function _responseUnsubscribe($receiveObj){
- return $this->_responseEvent($receiveObj);
- }
-
-
- protected function _responseScan($receiveObj){
- return $this->_responseEvent($receiveObj);
- }
-
-
- protected function _responseLocation($receiveObj){
- if(!strcasecmp($receiveObj->postObj->MsgType, ‘event‘)){
- return $this->_responseEvent($receiveObj);
- }else if(!strcasecmp($receiveObj->postObj->MsgType, ‘location‘)){
-
-
- }
- }
-
-
- protected function _responseClick($receiveObj){
- return $this->_responseEvent($receiveObj);
- }
-
-
- protected function _responseView($receiveObj){
-
- }
-
-
- protected function _responseScanCodePush($receiveObj){
- return $this->_responseEvent($receiveObj);
- }
-
-
- protected function _responseScanCodeWaitMsg($receiveObj){
- return $this->_responseEvent($receiveObj);
- }
-
-
- protected function _responsePicSysPhoto($receiveObj){
- return $this->_responseEvent($receiveObj);
- }
-
-
- protected function _responsePicPhotoOrAlbum($receiveObj){
- return $this->_responseEvent($receiveObj);
- }
-
-
- protected function _responsePicWeixin($receiveObj){
- return $this->_responseEvent($receiveObj);
- }
-
-
- protected function _responseLocationSelect($receiveObj){
- return $this->_responseEvent($receiveObj);
- }
-
-
- protected function _responseMessage($receiveObj){
-
-
-
- if(($msg=$this->_responseArgot($receiveObj))!==false){
- return $msg;
- }
-
-
- elseif($this->Config->TransferCustomerService){
- return array(
- ‘MsgType‘ => ‘transfer_customer_service‘,
- );
- }
- }
-
-
- protected function _responseArgot($receiveObj){
- if(!isset($receiveObj->postObj->Content) || !($msg=$receiveObj->postObj->Content)){
- return false;
- }
-
- $msg = trim($msg);
-
- if(defined(‘WXAPI_ARGOT_WHO_AM_I‘) && WXAPI_ARGOT_WHO_AM_I && !strcasecmp(WXAPI_ARGOT_WHO_AM_I, $msg)){
- return "OH LORD,\nMY 4susername is " . $this->Config->AppName . ",\nAppId: " . $this->Config->AppId . ",\n Server: " . $_SERVER[‘HTTP_HOST‘] ." .";
- }
-
- elseif(defined(‘WXAPI_ARGOT_DESTORY_SESSION‘) && WXAPI_ARGOT_DESTORY_SESSION && !strcasecmp(WXAPI_ARGOT_DESTORY_SESSION, $msg)){
- $openid = $receiveObj->parse_openid();
- if($openid && class_exists(‘WeixinUserModel‘) && method_exists(‘WeixinUserModel‘, ‘destroy_session‘)){
- $oWeixinUserModel = new WeixinUserModel();
- $succ = $oWeixinUserModel->destroy_session($openid);
- return $succ?"Your session has been destoryed Successfully!":"Failed destroy your session!";
- }else{
- return false;
- }
- }
-
- else{
- return false;
- }
- }
-
-
- protected function _responseMassSendJobFinish($receiveObj){
-
-
- }
-
-
- protected function _responseTemplateSendJobFinish($receiveObj){
-
-
- }
-
-
- protected function _preprocessResponseMedia($Content){
- if(!is_array($Content) || empty($Content[‘MsgType‘])){
- return $Content;
- }
-
- $msgtype = strtolower($Content[‘MsgType‘]);
- switch ($msgtype){
- case ‘image‘:
- $mediaField = ‘MediaId‘;
- if(is_string($Content[‘Content‘]) && !$this->_isMediaId($Content[‘Content‘])){
- $mediaFile = $Content[‘Content‘];
- $oClient = WeixinApi::instance($this->Config->Client, $this->Config);
-
- $mediaId = $oClient->upload_media($mediaFile, ‘image‘);
-
- $Content[‘Content‘] = $mediaId;
- }else{
- $mediaId = $Content[‘Content‘];
- $mediaFile = ‘‘;
- }
- $this->_responseMedia[$mediaField] = array($mediaId, ‘image‘, $mediaFile);
-
- break;
- case ‘voice‘:
- $mediaField = ‘MediaId‘;
- if(is_string($Content[‘Content‘]) && !$this->_isMediaId($Content[‘Content‘])){
- $mediaFile = $Content[‘Content‘];
-
- $oClient = WeixinApi::instance($this->Config->Client, $this->Config);
- $mediaId = $oClient->upload_media($mediaFile, ‘voice‘);
-
- $Content[‘Content‘] = $mediaId;
- }else{
- $mediaId = $Content[‘Content‘];
- $mediaFile = ‘‘;
- }
- $this->_responseMedia[$mediaField] = array($mediaId, ‘voice‘, $mediaFile);
-
- break;
- case ‘video‘:
- $mediaField = ‘MediaId‘;
- if(!$this->_isMediaId($Content[‘Content‘][‘MediaId‘])){
- $mediaFile = $Content[‘Content‘][‘MediaId‘];
- $mediaField = ‘MediaId‘;
-
- $oClient = WeixinApi::instance($this->Config->Client, $this->Config);
- $mediaId = $oClient->upload_media($mediaFile, ‘video‘);
-
- $Content[‘Content‘][‘MediaId‘] = $mediaId;
- }else{
- $mediaId = $Content[‘Content‘][‘MediaId‘];
- $mediaFile = ‘‘;
- }
- $this->_responseMedia[$mediaField] = array($mediaId, ‘video‘, $mediaFile);
-
- $thumbMediaField = ‘ThumbMediaId‘;
- if(!$this->_isMediaId($Content[‘Content‘][‘ThumbMediaId‘])){
- $thumbMediaFile = $Content[‘Content‘][‘ThumbMediaId‘];
-
- $oClient = WeixinApi::instance($this->Config->Client, $this->Config);
- $thumbMediaId = $oClient->upload_media($thumbMediaFile, ‘thumb‘);
-
- $Content[‘Content‘][‘ThumbMediaId‘] = $thumbMediaId;
-
- }else{
- $thumbMediaId = $Content[‘Content‘][‘ThumbMediaId‘];
- $thumbMediaFile = ‘‘;
- }
- $this->_responseMedia[$thumbMediaField] = array($thumbMediaId, ‘thumb‘, $thumbMediaFile);
- break;
- case ‘music‘:
- $thumbMediaField = ‘ThumbMediaId‘;
- if(is_array($Content[‘Content‘])){
- if(!$this->_isMediaId($Content[‘Content‘][‘ThumbMediaId‘])){
- $thumbMediaFile = $Content[‘Content‘][‘ThumbMediaId‘];
-
- $oClient = WeixinApi::instance($this->Config->Client, $this->Config);
- $thumbMediaId = $oClient->upload_media($thumbMediaFile, ‘thumb‘);
-
- $Content[‘Content‘][‘ThumbMediaId‘] = $thumbMediaId;
- }else{
- $thumbMediaId = $Content[‘Content‘][‘ThumbMediaId‘];
- $thumbMediaFile = ‘‘;
- }
- }else{
- if(!$this->_isMediaId($Content[‘Content‘])){
- $thumbMediaFile = $Content[‘Content‘];
- $oClient = WeixinApi::instance($this->Config->Client, $this->Config);
- $thumbMediaId = $oClient->upload_media($thumbMediaFile, ‘thumb‘);
-
- $Content[‘Content‘] = $thumbMediaId;
- }else{
- $thumbMediaId = $Content[‘Content‘];
- $thumbMediaFile = ‘‘;
- }
- }
- $this->_responseMedia[$thumbMediaField] = array($thumbMediaId, ‘thumb‘, $thumbMediaFile);
-
- break;
- default:
-
- }
-
- return $Content;
- }
-
- public function createMessage($FromUserName, $ToUserName, $Content){
- return self::generateMessage($FromUserName, $ToUserName, $Content);
- }
-
-
- public static function generateMessage($FromUserName, $ToUserName, $Content){
- $aMsgTypes = array(
- ‘text‘, ‘image‘, ‘voice‘, ‘video‘, ‘music‘, ‘news‘, ‘transfer_customer_service‘
- );
-
- if(is_array($Content)){
- $type = $Content[‘MsgType‘];
- $data = $Content[‘Content‘];
- }else{
- $type = ‘text‘;
- $data = $Content;
- }
-
- if(!in_array($type, $aMsgTypes, false)){
- return WeixinApi::throw_exception("Unknown MsgType: $type", WXAPI_ERR_CONFIG, $Content, __FILE__, __LINE__);
- }
-
- if(!strcasecmp($type, ‘transfer_customer_service‘)){
- $method = ‘generateTransferCustomerServiceMessage‘;
- }else{
- $method = ‘generate‘ . ucfirst(strtolower($type)) . ‘Message‘;
- }
- return self::$method($FromUserName, $ToUserName, $data);
- }
-
- public function createTextMessage($FromUserName, $ToUserName, $content){
- return self::generateTextMessage($FromUserName, $ToUserName, $content);
- }
-
-
- public static function generateTextMessage($FromUserName, $ToUserName, $content)
- {
- $msgTpl = "<xml>
- <ToUserName><![CDATA[%s]]></ToUserName>
- <FromUserName><![CDATA[%s]]></FromUserName>
- <CreateTime>%s</CreateTime>
- <MsgType><![CDATA[text]]></MsgType>
- <Content><![CDATA[%s]]></Content>
- </xml>";
- return sprintf($msgTpl, $FromUserName, $ToUserName, time(), $content);
- }
-
- public function createImageMessage($FromUserName, $ToUserName, $mediaId){
- return self::generateImageMessage($FromUserName, $ToUserName, $mediaId);
- }
-
-
- public static function generateImageMessage($FromUserName, $ToUserName, $mediaId)
- {
- $msgTpl = "<xml>
- <ToUserName><![CDATA[%s]]></ToUserName>
- <FromUserName><![CDATA[%s]]></FromUserName>
- <CreateTime>%s</CreateTime>
- <MsgType><![CDATA[image]]></MsgType>
- <Image>
- %s
- </Image>
- </xml>";
- $mediaTpl = "<MediaId><![CDATA[%s]]></MediaId>";
- if(!is_array($mediaId)){
- $mediaIds = array($mediaId);
- }else{
- $mediaIds = $mediaId;
- }
-
- $media = ‘‘;
- foreach($mediaIds as $mediaId){
- $media .= sprintf($mediaTpl, $mediaId);
- }
-
- return sprintf($msgTpl, $FromUserName, $ToUserName, time(), $media);
- }
-
- public function createVoiceMessage($FromUserName, $ToUserName, $mediaId){
- return self::generateVoiceMessage($FromUserName, $ToUserName, $mediaId);
- }
-
-
- public static function generateVoiceMessage($FromUserName, $ToUserName, $mediaId)
- {
- $msgTpl = "<xml>
- <ToUserName><![CDATA[%s]]></ToUserName>
- <FromUserName><![CDATA[%s]]></FromUserName>
- <CreateTime>%s</CreateTime>
- <MsgType><![CDATA[voice]]></MsgType>
- <Voice>
- %s
- </Voice>
- </xml>";
- $mediaTpl = "<MediaId><![CDATA[%s]]></MediaId>";
- if(!is_array($mediaId)){
- $mediaIds = array($mediaId);
- }else{
- $mediaIds = $mediaId;
- }
-
- $media = ‘‘;
- foreach($mediaIds as $mediaId){
- $media .= sprintf($mediaTpl, $mediaId);
- }
-
- return sprintf($msgTpl, $FromUserName, $ToUserName, time(), $media);
- }
-
- public function createVideoMessage($FromUserName, $ToUserName, $mediaId, $thumbMediaId=NULL){
- return self::generateVideoMessage($FromUserName, $ToUserName, $mediaId, $thumbMediaId);
- }
-
-
- public static function generateVideoMessage($FromUserName, $ToUserName, $mediaId, $thumbMediaId=NULL)
- {
- $msgTpl = "<xml>
- <ToUserName><![CDATA[%s]]></ToUserName>
- <FromUserName><![CDATA[%s]]></FromUserName>
- <CreateTime>%s</CreateTime>
- <MsgType><![CDATA[video]]></MsgType>
- <Video>
- %s
- </Video>
- </xml>";
-
- if(is_array($mediaId)){
- $mediaData = array(
- ‘MediaId‘ => $mediaId[‘MediaId‘],
- ‘ThumbMediaId‘ => $mediaId[‘ThumbMediaId‘],
- );
- }else{
- $mediaData = array(
- ‘MediaId‘ => $mediaId,
- ‘ThumbMediaId‘ => $thumbMediaId,
- );
- }
-
- $media = "";
- foreach ($mediaData as $n=>$d){
- $n = ucfirst($n);
- $mediaTpl = "<{$n}><![CDATA[%s]]></{$n}>";
- $media .= sprintf($mediaTpl, $d);
- }
-
- return sprintf($msgTpl, $FromUserName, $ToUserName, time(), $media);
- }
-
- public function createMusicMessage($FromUserName, $ToUserName, $thumbMediaId, $title=NULL, $description=NULL, $musicUrl=NULL, $hqMusicUrl=NULL)
- {
- return self::generateMusicMessage($FromUserName, $ToUserName, $thumbMediaId, $title, $description, $musicUrl, $hqMusicUrl);
- }
-
-
- public static function generateMusicMessage($FromUserName, $ToUserName, $thumbMediaId, $title=NULL, $description=NULL, $musicUrl=NULL, $hqMusicUrl=NULL)
- {
- $msgTpl = "<xml>
- <ToUserName><![CDATA[%s]]></ToUserName>
- <FromUserName><![CDATA[%s]]></FromUserName>
- <CreateTime>%s</CreateTime>
- <MsgType><![CDATA[music]]></MsgType>
- <Music>%s
- </Music>
- </xml>";
-
- $media = "";
- if (is_array ( $thumbMediaId )) {
- $mediaData = array (
- "Title" => $thumbMediaId[‘Title‘],
- "Description" => $thumbMediaId[‘Description‘],
- "MusicUrl" => $thumbMediaId[‘MusicUrl‘],
- "HQMusicUrl" => $thumbMediaId[‘HQMusicUrl‘],
- "ThumbMediaId" => $thumbMediaId[‘ThumbMediaId‘],
- );
- } else {
- $mediaData = array (
- "Title" => $title,
- "Description" => $description,
- "MusicUrl" => $musicUrl,
- "HQMusicUrl" => $hqMusicUrl,
- "ThumbMediaId" => $thumbMediaId,
- );
- }
- foreach($mediaData as $n=>$d){
- if($d){
- $n = ucfirst($n);
- $mediaTpl = "<{$n}><![CDATA[%s]]></{$n}>";
- $media .= sprintf($mediaTpl, $d);
- }
- }
-
- return sprintf($msgTpl, $FromUserName, $ToUserName, time(), $media);
- }
-
- public function createNewsMessage($FromUserName, $ToUserName, $title, $description=NULL, $picUrl=NULL, $url=NULL)
- {
- return self::generateNewsMessage($FromUserName, $ToUserName, $title, $description, $picUrl, $url);
- }
-
-
- public static function generateNewsMessage($FromUserName, $ToUserName, $title, $description=NULL, $picUrl=NULL, $url=NULL)
- {
- $msgTpl = "<xml>
- <ToUserName><![CDATA[%s]]></ToUserName>
- <FromUserName><![CDATA[%s]]></FromUserName>
- <CreateTime>%s</CreateTime>
- <MsgType><![CDATA[news]]></MsgType>
- <ArticleCount>%s</ArticleCount>
- <Articles>
- %s
- </Articles>
- </xml>";
-
- $media = "";
- $items = array();
- if(is_array($title)){
- if(isset($title[‘Title‘]) || isset($title[‘Description‘]) || isset($title[‘PicUrl‘]) || isset($title[‘Url‘])){
- $items[] = $title;
- }else{
- $items = $title;
- }
- }else{
- $items[] = array(
- "Title" => $title,
- "Description" => $description,
- "PicUrl" => $picUrl,
- "Url" => $url,
- );
- }
-
- $count = count($items);
-
- if($count>10){
- return WeixinApi::throw_exception("Over Max 10 news messages", WXAPI_ERR_CONFIG, array(‘items‘=>$items), __FILE__, __LINE__);
- }
-
- $valid_item_tags = array(‘Title‘, ‘Description‘, ‘PicUrl‘, ‘Url‘);
- foreach($items as $item){
- $media .= "<item>";
- foreach ( $item as $n => $d ) {
- if ($d && in_array($n, $valid_item_tags, true)) {
- $n = ucfirst($n);
- $mediaTpl = "<{$n}><![CDATA[%s]]></{$n}>";
- $media .= sprintf ( $mediaTpl, $d );
- }
- }
- $media .= "</item>";
- }
-
- return sprintf($msgTpl, $FromUserName, $ToUserName, time(), $count, $media);
- }
-
-
- public static function generateTransferCustomerServiceMessage($FromUserName, $ToUserName, $TransInfo_KfAccount=NULL)
- {
- $msgTpl = "<xml>
- <ToUserName><![CDATA[%s]]></ToUserName>
- <FromUserName><![CDATA[%s]]></FromUserName>
- <CreateTime>%s</CreateTime>
- <MsgType><![CDATA[transfer_customer_service]]></MsgType>";
-
- if($TransInfo_KfAccount){
- $msg .= "<TransInfo>
- <KfAccount>%s</KfAccount>
- </TransInfo>";
- }
-
- $msgTpl .= "</xml>";
-
- return sprintf($msgTpl, $FromUserName, $ToUserName, time(), $TransInfo_KfAccount);
- }
-
- public function __get($c){
- if(substr($c, 0, 1)!=‘_‘){
- if(in_array($c, array(‘Http‘))){
- return parent::__get($c);
- }else{
- $n = ‘_‘ . $c;
- return $this->$n;
- }
- }
- }
-
-
- public static function genearteSignature($msg_encrypt, $nonce, $timestamp, $token){
- $tmpArr = array($token, $timestamp, $nonce, $msg_encrypt);
- sort($tmpArr, SORT_STRING);
- $tmpStr = implode( $tmpArr );
- return sha1( $tmpStr );
- }
-
-
- public static function generateEncryptMessage($encrypt_content, $nonce, $timestamp, $signature)
- {
- $msgTpl = "<xml>
- <Encrypt><![CDATA[%s]]></Encrypt>
- <MsgSignature><![CDATA[%s]]></MsgSignature>
- <TimeStamp>%s</TimeStamp>
- <Nonce><![CDATA[%s]]></Nonce>
- </xml>
- ";
- return sprintf($msgTpl, $encrypt_content, $signature, $timestamp, $nonce);
- }
-
-
- protected function _encrypt_response($msg, $encodingkey){
- $msg_encrypt = $this->_encryptMsg($msg, $encodingkey);
- $nonce = WeixinApi_Kit::gen_random_number(11);
- $timestamp = time();
- $signature = self::genearteSignature($msg_encrypt, $nonce, $timestamp, $this->Config->AppToken);
-
- return self::generateEncryptMessage($msg_encrypt, $nonce, $timestamp, $signature);
- }
-
-
- protected function _encryptMsg($msg, $encodingkey=NULL)
- {
- $AESKey = base64_decode(($encodingkey?$encodingkey:$this->Config->AppEncodingAESKey) . "=");
-
-
- $random = WeixinApi_Kit::gen_random_string ( 16 );
- $msg = $random . pack ( "N", strlen ( $msg ) ) . $msg . $this->Config->AppId;
-
- $size = mcrypt_get_block_size ( MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC );
- $module = mcrypt_module_open ( MCRYPT_RIJNDAEL_128, ‘‘, MCRYPT_MODE_CBC, ‘‘ );
- if(false===$module){
- return $this->_throw_exception(
- "Cann‘t open an encryption descriptor"
- , WXAPI_ERR_BAD_ENCRYPT
- , $msg
- , __FILE__, __LINE__
- );
- }
-
- $iv = substr ( $AESKey, 0, 16 );
-
-
- $msg = WeixinApi_Kit::pkcs7_encode ( $msg, 32 );
- $init = mcrypt_generic_init ( $module, $AESKey, $iv );
- if(false===$init){
- return $this->_throw_exception(
- "Cann‘t initialize buffers for encryption"
- , WXAPI_ERR_BAD_ENCRYPT
- , array(‘msg‘ => $msg, ‘mcrypt_generic_init return‘ => $init)
- , __FILE__, __LINE__
- );
- }elseif(-3==$init){
- return $this->_throw_exception(
- "the key length was incorrect"
- , WXAPI_ERR_BAD_ENCRYPT
- , array(‘msg‘ => $msg, ‘mcrypt_generic_init return‘ => $init)
- , __FILE__, __LINE__
- );
- }elseif(-4==$init){
- return $this->_throw_exception(
- "there was a memory allocation problem"
- , WXAPI_ERR_BAD_ENCRYPT
- , array(‘msg‘ => $msg, ‘mcrypt_generic_init return‘ => $init)
- , __FILE__, __LINE__
- );
- }elseif($init<0){
- return $this->_throw_exception(
- "an unknown error occurred when initialize buffers for encryption"
- , WXAPI_ERR_BAD_ENCRYPT
- , array(‘msg‘ => $msg, ‘mcrypt_generic_init return‘ => $init)
- , __FILE__, __LINE__
- );
- }
-
-
- $encrypted = mcrypt_generic ( $module, $msg );
- mcrypt_generic_deinit ( $module );
- mcrypt_module_close ( $module );
-
-
-
- return base64_encode ( $encrypted );
- }
- }
微信公众号接口类(PHP版本)
原文:https://www.cnblogs.com/piwefei/p/9172094.html