Undertake the above WeChatCallBack
Fields that are common to various messages are defined in the member variables of the WeChatCallBack class, and these fields are assigned values in the init function. At the same time, the parsed XML object is also used as the member variable $_postObject of this class and assigned in init. The purpose is to obtain the special fields of specific types of messages when implementing the business logic of specific public accounts.
The process function is a function that needs to be overloaded when implementing the business logic of a specific public account. The default implementation is to return an "not implemented" error message.
The focus of this article is the access script. The access script is interface.php in the project root directory. The code list is as follows:
require_once dirname(__FILE__) . '/common/GlobalFunctions.php';
Function checkSingnature()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = WEIXIN_TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr);
$tmpStr = implode( $tmpArr );
$tmpStr = shal( $tmpStr );
if( $tmpStr == $signature ) {
return true;
} else {
return false;
}
}
if(checkSignature()) {
if($_GET["echostr"]) {
echo $_GET["echostr"];
exit(0);
}
} else {
// Malicious request: Get the source IP and write it to the log
$ip = getIp();
interface_log(ERROR, EC_OTHER, 'malicious: ' . $ip);
exit(0);
}
Function getWeChatObj($toUserName) {
if($toUserName == USERNAME_FINDFACE) {
require_once dirname(__FILE__) . '/class/
WeChatCallBackMeiri10futu.php' ;
return new WeChatCallBackMeir10futu();
}
if($toUserName == USERNAME_MR) {
require_once dirname(__FILE__) . '/class/
WeChatCallBackMeir10futu.php' ;
return new WeChatCallBackMeir10futu();
}
if($toUserName == USERNAME_ES) {
require_once dirname(__FILE__) . '/class/
WeChatCallBackEchoServer.php' ;
return new WeChatCallBackEchoServer();
}
require_once dirname(__FILE__) . '/class/WeChatCallBcak.php' ;
Return new WeChatCallBack();
}
function exitErrorInput() {
echo 'error input!' ;
Interface_log(INFO, EC_OK, "***** interface request end ******") ;
Interface_log(INFO, EC_OK, "********************** ******") ;
Interface_log(INFO, EC_OK, "") ;
exit( 0 );
}
$postStr = file_get_contents ( "php://input" );
interface_log(INFO, EC_OK, "");
interface_log(INFO, EC_OK, "******************************");
interface_log(INFO, EC_OK, "***** interface request start *****");
interface_log(INFO, EC_OK, 'request: ' . var_export($_GET, ture)) ;
if (empty ( $postStr )) {
Interface_log (ERROR, EC_OK, "error input!" );
exitErrorInput();
}
// Get parameters
$postObj = simplexml_load_string ( $postStr, 'SimpleXMLElement',
LIBXML_NOCDATA ) ;
$toUserName = ( string ) trim ( $postObj->ToUserName ) ;
if (! $toUserName) {
Interface_log ( ERROR, EC_OK, "error input!" ) ;
exitErrorInput() ;
} else {
$wechatObj = getWeChatObj ($ toUserName);
}
retStr = $ wechatObj->process ();
interface_log(INFO, EC_OK, "***** interface request end *****") ;
interface_log(INFO, EC_OK, "******************************") ;
interface_log(INFO, EC_OK, "");
?>
Interface.php logical description:
1. Use the checkSignature function to verify whether the request is legal. If it is illegal, record the malicious source IP.
2. Obtain the POST data, parse the XML data, determine which public account the message is sent to based on ToUserName, then load the corresponding file and obtain the corresponding object (getWeChatObj function).
3. Call the init function of the object to initialize the object.
4. Call the process function of the object to process the public account logic and get the return message string.
5. Return the message.
Get source IP function:
Function getIp ()
{
if (isset($_SERVER)) {
if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
$realip = $_SERVER["HTTP_X_FORWARDED_FOR"] ;
} else if (isser($_SERVER["HTTP_CLIENT_IP"])) {
$realip = $_SERVER["HTTP_CLIENT_IP"] ;
} else {
$realip = $SERVER["REMOTE_ADDR"];
}
} else {
if (getenv(""HTTP_X_FORWARDED_FOR")) {
$realip = getenv("HTTP_X_FORWARDED_FOR") ;
} else if (getenv("HTTP_CLIENT_IP")) {
$realip = getenv("HTTP_CLIENT_IP") ;
} else {
$realip = getenv("REMOVE_ADDR") ;
}
}
return $realip;
}
___________over____________
is the short message access code for the mobile network information service business in the second type of value-added telecommunications business, which is divided into two categories: full network and ground network. The full network sp access code is the full network operational sp starting with 1066. Access codes, and non-commercial sp access codes starting with 1069 for the entire network; sp access codes for the ground network are divided into commercial sp access codes starting with 1062 and non-commercial sp access codes starting with 1063. The application agency for the SP access code for the whole network is the Ministry of Industry and Information Technology, and the application agency for the ground network access code is the local Communications Administration Bureau. So what materials need to be submitted to apply for the SP access code? sp access code application materials 1. Short message service access code application report (number length, scope of application, contact number); 2. Short message service access code (provincial use) application form; 3. Commitment letter for the use of short message service access codes; 4. Original and copy of value-added telecommunications business license (business category: short message service); 5. Original and copy of business license copy; 6. Technical plan, business plan, etc. .
The access code is different in each region
You can only dial 10086 to contact the manual service
Or you can tell me the location of the phone number so I can check it for you!