Home>Article>WeChat Applet> php WeChat public account development (3) php implements simple WeChat text communication

php WeChat public account development (3) php implements simple WeChat text communication

黄舟
黄舟 Original
2018-05-15 16:28:56 1966browse

Before WeChat development, you need to set the token. This is set by WeChat and can be set arbitrarily to implement WeChat communication. Here is a WeChat class written by someone else, which has pretty good functions. The weixin.class.php code is as follows

token = $token; $this->debug = $debug; } //获得用户发过来的消息(消息内容和消息类型 ) 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']); } } //回复文本消息 public function makeText($text='') { $CreateTime = time(); $FuncFlag = $this->setFlag ? 1 : 0; $textTpl = " msg['FromUserName']}]]> msg['ToUserName']}]]> {$CreateTime}   %s "; return sprintf($textTpl,$text,$FuncFlag); } //根据数组参数回复图文消息 public function makeNews($newsData=array()) { $CreateTime = time(); $FuncFlag = $this->setFlag ? 1 : 0; $newTplHeader = " msg['FromUserName']}]]> msg['ToUserName']}]]> {$CreateTime}   %s"; $newTplItem = " <![CDATA[%s]]>    "; $newTplFoot = " %s "; $Content = ''; $itemsCount = count($newsData['items']); $itemsCount = $itemsCount < 10 ? $itemsCount : 10;//微信公众平台图文回复的消息一次最多10条 if ($itemsCount) { foreach ($newsData['items'] as $key => $item) { 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 = sha1( $tmpStr ); if( $tmpStr == $signature ){ return true; }else{ return false; } } } ?>

Then proceed to formal development. Use the Baidu SVN address to create the weixinapi.php file. This is named according to your background settings.

getMsg(); $type = $weixin->msgtype;//消息类型 $keyword = $weixin->msg['Content'];//获取的文本 if ($type==='text') { $reply = $weixin->makeText($key); }elseif($type==='event'){//第一次关注推送事件 $reply = $weixin->makeText("欢迎关注"); }else{//其他类型 $reply = $weixin->makeText("暂时没有图片,声音,地理位置等功能,后续开发会增加,感谢你关注"); } $weixin->reply($reply);

In this way, an example is implemented. For the first time, we focus on event replies, non-text replies, and text replies. The text replies here return whatever you input.
The specific implementation function is written in the text reply.
Other functions will not be implemented for the time being, and the specific development will be discussed in the next section.

The above is the content of PHP WeChat public account development (3) PHP to implement simple WeChat text communication. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn