Php public account development (3) php implements simple text communication,
Before development, you need to set token. This is set and can be set as needed for communication. There is a class written by someone else, and the function is quite good. The weixin. class. php code is as follows:
<? Phpclass Weixin {public $ token = ''; // token public $ debug = false; // indicates whether the debug status is correct, it is convenient for us to record some intermediate data public $ setFlag = false; public $ msgtype = 'text'; // ('text', 'image', 'location') during debugging ') public $ msg = array (); public function _ construct ($ token, $ debug) {$ this-> token = $ token; $ this-> debug = $ debug ;} // obtain the user-sent message (message content and message type) public function getMsg () {$ postStr = $ GLOBALS ["HTTP_RAW_POST_DATA"]; if (! Empty ($ postStr) {$ this-> msg = (array) simplexml_load_string ($ postStr, 'simplexmlelement', LIBXML_NOCDATA ); $ this-> msgtype = strtolower ($ this-> msg ['msgtype']) ;}// reply to the public function makeText ($ text = '') of the text message '') {$ CreateTime = time (); $ FuncFlag = $ this-> setFlag? 1: 0; $ textTpl = "<xml> <ToUserName> <! [CDATA [{$ this-> msg ['fromusername']}]> </ToUserName> <FromUserName> <! [CDATA [{$ this-> msg ['tousername']}]> </FromUserName> <CreateTime >{$ CreateTime} </CreateTime> <MsgType> <! [CDATA [text]> </MsgType> <Content> <! [CDATA [% s]> </Content> <FuncFlag> % s </FuncFlag> </xml> "; return sprintf ($ textTpl, $ text, $ FuncFlag);} // reply to the public function makeNews ($ newsData = array () {$ CreateTime = time (); $ FuncFlag = $ this-> setFlag? 1: 0; $ newTplHeader = "<xml> <ToUserName> <! [CDATA [{$ this-> msg ['fromusername']}]> </ToUserName> <FromUserName> <! [CDATA [{$ this-> msg ['tousername']}]> </FromUserName> <CreateTime >{$ CreateTime} </CreateTime> <MsgType> <! [CDATA [news]> </MsgType> <Content> <! [CDATA [% s]> </Content> <ArticleCount> % s </ArticleCount> <Articles> "; $ newTplItem =" <item> <Title> <! [CDATA [% s]> </Title> <Description> <! [CDATA [% s]> </Description> <PicUrl> <! [CDATA [% s]> </PicUrl> <Url> <! [CDATA [% s]> </Url> </item> "; $ newTplFoot = "</Articles> <FuncFlag> % s </FuncFlag> </xml>"; $ Content = ''; $ itemsCount = count ($ newsData ['items ']); $ itemsCount = $ itemsCount <10? $ ItemsCount: 10; // a maximum of 10 if ($ itemsCount) {foreach ($ newsData ['items '] as $ key => $ item) messages can be replied to by text on the public platform at a time) {if ($ key <= 9) {$ Content. = sprintf ($ newTplItem, $ item ['title'], $ item ['description'], $ item ['picurl'], $ item ['url']) ;}}$ header = sprintf ($ newTplHeader, $ newsData ['content'], $ itemsCount); $ footer = sprintf ($ newTplFoot, $ FuncFlag); return $ header. $ Content. $ footer;} public function reply ($ data) {Echo $ data;} public function valid () {if ($ this-> checkSignature ()) {if ($ _ SERVER ['request _ method'] = 'get') {echo $ _ GET ['echostr']; exit ;}} else {exit ;}} private function checkSignature () {$ signature =$ _ GET ["signature"]; $ timestamp =$ _ GET ["timestamp"]; $ nonce = $ _ GET ["nonce"]; $ tmpArr = array ($ this-> token, $ timestamp, $ nonce); sort ($ tmpArr ); $ tmpStr = implode ($ tmpArr); $ tmpStr = sh A1 ($ tmpStr); if ($ tmpStr ==$ signature) {return true;} else {return false ;}}?>
Go on to the Official Development page and use the Baidu SVN address to create the weixinapi. php file. Set the name based on your background.
<? Phpdefine ("TOKEN", ""); define ('debug', false); include_once ('weixin. class. php '); require_once ("db. php "); $ weixin = new Weixin (TOKEN, DEBUG); // instantiate $ weixin-> getMsg (); $ type = $ weixin-> msgtype; // Message type $ keyword = $ weixin-> msg ['content']; // obtain the text if ($ type = 'text ') {$ reply = $ weixin-> makeText ($ key);} elseif ($ type = 'event ') {// follow the push event for the first time $ reply = $ weixin-> makeText ("Welcome ");} else {// other types $ reply = $ weixin-> makeText ");} $ weixin-> reply ($ reply );
In this way, an example is implemented. The first time you pay attention to event reply, non-text reply, and text reply, the text reply here is what you input and then returns.
The specific implementation function is written in the text reply.
Other functions are not available for the time being.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.