Development Mode (php)
To learn the Development Mode and PHP, and record it here for later reading.
ResponseMsg ();} else {$ wechatObj-> valid ();} class wechatCallbackapiTest {public function valid () {$ echoStr = $ _ GET ["echostr"]; // valid signature, option if ($ this-> checkSignature () {echo $ echoStr; exit ;}} public function responseMsg () {// get post data, may be due to the different environments $ postStr = $ GLOBALS ["HTTP_RAW_POST_DATA"]; // extract post data 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 ); $ postObj = simplexml_load_string ($ postStr, 'simplexmlelement', LIBXML_NOCDATA); $ RX_TYPE = trim ($ postObj-> MsgType); switch ($ RX_TYPE) {case "text ": $ resultStr = $ this-> paietext ($ postObj); brea K; case "event": $ resultStr = $ this-> receiveEvent ($ postObj); break; default: $ resultStr = ""; break;} echo $ resultStr ;} else {echo ""; exit ;}} private function effecetext ($ object) {$ getkeyword = $ object-> Content; switch ($ getkeyword) {case 'product ': $ funcFlag = 0; $ contentStr = "product link: http://www.xxx.com/products/"; $ resultStr = $ this-> transmitText ($ object, $ contentStr, $ funcFlag); break; Case 'News': $ funcFlag = 0; $ contentStr = "News link: http://www.xxx.com/news/"; $ resultStr = $ this-> transmitText ($ object, $ contentStr, $ funcFlag ); break; case 'scheme': $ funcFlag = 0; $ contentStr = "Scheme link: http://www.xxx.com/articles/"; $ resultStr = $ this-> transmitText ($ object, $ contentStr, $ funcFlag); break; case 'ours: $ funcFlag = 0; $ contentStr = "link: http://www.xxx.com/culture? Type = detail & id = 1 "; $ resultStr = $ this-> transmitText ($ object, $ contentStr, $ funcFlag); break; default: break;} return $ resultStr ;} private function receiveEvent ($ object) {$ contentStr = ""; switch ($ object-> Event) {case "subscribe": $ contentStr = "Welcome to xxx "; case "unsubscribe": break; case "CLICK": switch ($ object-> EventKey) {case "V1001_GETCODE": $ contentStr = "xxx"; break; case "V1002_HISTORY ": $ contentStr [] = array ("Title" => "last record", "Description" => "xxx", "PicUrl" => "xxx ", "Url" => "xxx"); break; case "V2001_INTERDUCE": $ contentStr [] = array ("Title" => "xxx ", "Description" => "xxx", "Url" => "http://mp.weixin.qq.com/xxx"); break; case "V3001_JOIN": $ getuid = $ this-> getUid ($ object ); if ($ getuid = 1) {$ contentStr = 'could not connect :'. mysql_error ();} else {$ contentStr = "unique code :". $ getuid;} break; default: $ contentStr [] = array ("Title" => "default menu reply", "Description" => "xxx ", "PicUrl" => "xxx", "Url" => "xxx"); break;} break; default: break;} if (is_array ($ contentStr )) {$ resultStr = $ this-> transmitNews ($ object, $ contentStr);} else {$ resultStr = $ this-> transmitText ($ object, $ contentStr );} return $ resultStr;} private function transmitText ($ object, $ content, $ funcFlag = 0) {$ textTpl ="
%s
%s
% S
text
%s
% D
"; $ ResultStr = sprintf ($ textTpl, $ object-> FromUserName, $ object-> ToUserName, time (), $ content, $ funcFlag); return $ resultStr ;} private function transmitNews ($ object, $ arr_item, $ funcFlag = 0) {if (! Is_array ($ arr_item) return; $ itemTpl ="
<! [CDATA [% s]>
%s
%s
%s
"; $ Item_str =" "; foreach ($ arr_item as $ item) $ item_str. = sprintf ($ itemTpl, $ item ['title'], $ item ['description'], $ item ['picurl'], $ item ['url']); $ newsTpl ="
%s
%s
% S
news
% S$ Item_str
% S
"; $ ResultStr = sprintf ($ newsTpl, $ object-> FromUserName, $ object-> ToUserName, time (), count ($ arr_item), $ funcFlag ); return $ resultStr;}/* generate unique code */public function getUid ($ object) {$ getOpenid = $ object-> FromUserName; $ con = mysql_connect ("localhost ", "root", "xxx"); mysql_select_db ("xxx", $ con); if (! $ Con) {return 1 ;}$ flag = true; $ getUserresult = mysql_query ("SELECT * FROM loginuser WHERE openid = '". $ getOpenid. "'"); $ getuserrow = mysql_num_rows ($ getUserresult); if ($ getuserrow> 0) {$ userrow = mysql_fetch_array ($ getUserresult ); if ($ userrow ['state'] = 0) {return $ userrow ['userpassword']. "[unfinished exam]"; $ flag = false;} else {return $ userrow ['userpassword']. "[completed exams]"; $ flag = false ;} }/* Cyclically determine uniqueness */while ($ flag) {$ uid = uniqid (true); $ uid = substr ($ uid,-8 ); $ result = mysql_query ("SELECT * FROM loginuser where userPassword = '". $ uid. "'"); $ row = mysql_num_rows ($ result); if ($ row = 0) {mysql_query ("insert into loginuser (userPassword, state, openid) VALUES ('". $ uid. "', '0 ','". $ getOpenid. "')"); mysql_close ($ con); return $ uid; $ flag = false ;}} private function GetUidtest ($ object) {return $ object-> FromUserName;} private function checkSignature () {// you must define TOKEN by yourself if (! Defined ("TOKEN") {throw new Exception ('token is not defined! ');} $ Signature = $ _ GET ["signature"]; $ timestamp = $ _ GET ["timestamp"]; $ nonce = $ _ GET ["nonce"]; $ token = TOKEN; $ tmpArr = array ($ token, $ timestamp, $ nonce); // use SORT_STRING rule sort ($ tmpArr, SORT_STRING ); $ tmpStr = implode ($ tmpArr); $ tmpStr = sha1 ($ tmpStr); if ($ tmpStr ==$ signature) {return true ;} else {return false ;}}}?>