首页 > 微信应用  > 

php微信公众号开发(3)php实现简单微信文本通讯

php微信公众号开发(3)php实现简单微信文本通讯
微信开发前,需要设置token,这个是微信设置的,可以任意设置,用来实现微信通讯。这里有一个别人写的微信类,功能还比较不错。weixin.class.php代码如下

微信开发前,需要设置token,这个是微信设置的,可以任意设置,用来实现微信通讯。这里有一个别人写的微信类,功能还比较不错。weixin.class.php代码如下

<?phpclass Weixin{ public $token = &#39;&#39;;//token public $debug = false;//是否debug的状态标示,方便我们在调试的时候记录一些中间数据 public $setFlag = false; public $msgtype = &#39;text&#39;; //(&#39;text&#39;,&#39;image&#39;,&#39;location&#39;) public $msg = array(); public function __construct($token,$debug) { $this->token = $token; $this->debug = $debug; }//获得用户发过来的消息(消息内容和消息类型 ) public function getMsg() { $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; if (!empty($postStr)) { $this->msg = (array)simplexml_load_string($postStr, &#39;SimpleXMLElement&#39;, LIBXML_NOCDATA); $this->msgtype = strtolower($this->msg[&#39;MsgType&#39;]); } }//回复文本消息 public function makeText($text=&#39;&#39;) { $CreateTime = time(); $FuncFlag = $this->setFlag ? 1 : 0; $textTpl = "<xml> <ToUserName><![CDATA[{$this->msg[&#39;FromUserName&#39;]}]]></ToUserName> <FromUserName><![CDATA[{$this->msg[&#39;ToUserName&#39;]}]]></FromUserName> <CreateTime>{$CreateTime}</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[%s]]></Content> <FuncFlag>%s</FuncFlag> </xml>"; return sprintf($textTpl,$text,$FuncFlag); } //根据数组参数回复图文消息 public function makeNews($newsData=array()) { $CreateTime = time(); $FuncFlag = $this->setFlag ? 1 : 0; $newTplHeader = "<xml> <ToUserName><![CDATA[{$this->msg[&#39;FromUserName&#39;]}]]></ToUserName> <FromUserName><![CDATA[{$this->msg[&#39;ToUserName&#39;]}]]></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 = &#39;&#39;; $itemsCount = count($newsData[&#39;items&#39;]); $itemsCount = $itemsCount < 10 ? $itemsCount : 10;//微信公众平台图文回复的消息一次最多10条 if ($itemsCount) { foreach ($newsData[&#39;items&#39;] as $key => $item) { if ($key<=9) { $Content .= sprintf($newTplItem,$item[&#39;title&#39;],$item[&#39;description&#39;],$item[&#39;picurl&#39;],$item[&#39;url&#39;]); } } } $header = sprintf($newTplHeader,$newsData[&#39;content&#39;],$itemsCount); $footer = sprintf($newTplFoot,$FuncFlag); return $header . $Content . $footer; } public function reply($data) { echo $data; } public function valid() { if ($this->checkSignature()) { if( $_SERVER[&#39;REQUEST_METHOD&#39;]==&#39;GET&#39; ) { echo $_GET[&#39;echostr&#39;]; 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; } } }?>

php微信公众号开发(3)php实现简单微信文本通讯由讯客互联微信应用栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“php微信公众号开发(3)php实现简单微信文本通讯