Php api class
This example shares the PHP interface class for your reference. The specific content is as follows:
<? Php/*** wechat php test * // define your token // define the TOKEN key define ("TOKEN", "weixin "); // instantiate the object $ wechatObj = new wechatCallbackapiTest (); // comment out the valid method after the verification is successful // $ wechatObj-> valid (); // enable the automatic reply function $ wechatObj-> responseMsg (); // defines the class wechatCallbackapiTest {// implements the valid Verification Method: implements the public function valid () {// accept random string $ echoStr =$ _ GET ["echostr"]; // valid signature, option // perform user digital signature verification if ($ this-> checkSignature ()){ // If the response succeeds, the received random string echo $ echoStr is returned; // exit; }}// define the automatic reply function public function responseMsg () {// get post data, May be due to the different environments // accept xml data sent from the client $ postStr = $ GLOBALS ["HTTP_RAW_POST_DATA"]; // extract post data // judge whether the xml data is null if (! Empty ($ postStr) {/* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection, the best way is to check the validity of xml by yourself */libxml_disable_entity_loader (true ); // use simplexml for xml parsing $ postObj = simplexml_load_string ($ postStr, 'simplexmlelement', LIBXML_NOCDATA); // The received mobile terminal $ fromUsername = $ postObj-> FromUserName; // public platform $ toUsername = $ postObj-> ToUserName; // receives the keyword $ key sent by the user Word = trim ($ postObj-> Content); // 1. type of the received user message $ msgType = $ postObj-> MsgType; // timestamp $ time = time (); // text sending template $ textTpl = "<xml> <ToUserName> <! [CDATA [% s]> </ToUserName> <FromUserName> <! [CDATA [% s]> </FromUserName> <CreateTime> % s </CreateTime> <MsgType> <! [CDATA [% s]> </MsgType> <Content> <! [CDATA [% s]> </Content> <FuncFlag> 0 </FuncFlag> </xml> "; //////////////////////////////////////// //////////////////////////////////////// //// if the user sends a text file, run the following if ($ msgType = 'text') {if (! Empty ($ keyword) {/* this is an instance // if you send text information $ msgType = "text"; // reply content if ($ keyword = "Li Nan ") {$ contentStr = "Why do you call me";} else {$ contentStr = "Why do you call me";} // format the xml template, parameters correspond to the preceding template one by one. fromUsername is the opposite of the header Username. Only $ resultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgType, $ contentStr) with % s is written ); // return xml Information to the client echo $ resultStr; */if ($ keyword = "? "| $ Keyword = "? ") {$ MsgType =" text "; $ contentStr =" 1. special service number \ n2. communication service number "; $ resultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgType, $ contentStr); echo $ resultStr ;} elseif ($ keyword = 1) {$ msgType = "text"; $ contentStr = "1. alert: 110 \ n2. fire: 119 \ n3. emergency: 120 "; $ resultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgType, $ contentStr ); echo $ resultStr;} elseif ($ keyword = 2) {$ msgType = "text"; $ conte NtStr = "1. china Mobile: 10086 \ n2. China Unicom: 10010 "; $ resultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgType, $ contentStr ); echo $ resultStr ;}} else {echo "cannot speak ";}} //////////////////////////////////////// //////////////////////////////////////// ///// accept image information if ($ msgType = "image ") {// if you send a text message $ msgType = "text"; // The reply content $ contentStr = "you are sending an image file "; // format the string $ resultStr = sprintf ($ textTpl, $ fromUsern Ame, $ toUsername, $ time, $ msgType, $ contentStr); // return xml Information to the client echo $ resultStr ;} //////////////////////////////////////// //////////////////////////////////////// /// if ($ msgType = "voice ") {// if you send text information $ msgType = "text"; // The reply content $ contentStr = "you are sending a voice file "; // format the string $ resultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgType, $ contentStr ); // return the xml Information to the client echo $ resultStr ;}////////// //////////////////////////////////////// /// // If ($ msgType = "video ") {// if you send text information $ msgType = "text"; // The reply content $ contentStr = "you sent a video file "; // format the string $ resultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgType, $ contentStr ); // return xml Information to the client echo $ resultStr ;} //////////////////////////////////////// //////////////////////////////////////// /// if ($ msgType = "sh Ortvideo ") {// if you send text information $ msgType =" text "; // The reply content $ contentStr =" you are sending a small video file "; // format the string $ resultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgType, $ contentStr ); // return xml Information to the client echo $ resultStr ;} //////////////////////////////////////// //////////////////////////////////////// /// if ($ msgType = "location ") {// if you send text information $ msgType = "text"; // The reply content $ contentStr = "the location file you sent"; // format the string $ r EsultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgType, $ contentStr); // return xml Information to the client echo $ resultStr ;} //////////////////////////////////////// //////////////////////////////////////// /// if ($ msgType = "link ") {// if you send text information $ msgType = "text"; // The reply content $ contentStr = "the connection file you sent "; // format the string $ resultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgType, $ contentStr); // convert the xml Return information to the client echo $ resultStr ;} //////////////////////////////////////// //////////////////////////////////////// ///// * // determine whether the keyword sent by the user is null if (! Empty ($ keyword) {// if you send a text message $ msgType = "text"; // The reply content $ contentStr = "Hello, my name is hero "; // format the string $ resultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgType, $ contentStr ); // return xml Information to the client echo $ resultStr;} else {echo "Input something... ";}*/} else {echo" "; exit ;}} private function checkSignature () {// you must define TOKEN by yourself // determine whether TOKEN is defined, if not, an exception is thrown. if (! Defined ("TOKEN") {throw new Exception ('token is not defined! ') ;}$ Signature =$ _ GET ["signature"]; // accept the encrypted signature $ timestamp =$ _ GET ["timestamp"]; // accept the timestamp $ nonce = $ _ GET ["nonce"]; // accept the random number $ token = TOKEN; // assign the TOKEN constant to $ token // assemble relevant parameters into an array $ tmpArr = array ($ token, $ timestamp, $ nonce ); // use SORT_STRING rule // sort ($ tmpArr, SORT_STRING); // convert the sorted array to a string $ tmpStr = implode ($ tmpArr ); // use the hash algorithm to encrypt $ tmpStr = sha1 ($ tmpStr); // compare it with the encrypted signature if ($ tmpStr = $ signature) {// returns the same t Rue return true;} false return false for different else {//; }}}?>
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.