|
本系列教程以微信公众平台应用妈妈助手(账号mmhelper:发送食物名称或拼音首字母查询孕妇、坐月子、宝宝能不能吃哪些食物。发送自己的位置查询附近的母婴类商家,以及促销、团购等信息。发送与宝宝的合照测试与宝宝的脸部相似度)为例,讲解微信接口开发过程。欢迎大家关注该账号,二维码见底部图。
使用前提条件:拥有一个公网上的HTTP服务器主机空间,具有创建目录、上传文件等权限。推荐新浪的SAE。http://sae.sina.com.cn/
首先请注册微信公众平台的账号,注册地址:http://mp.weixin.qq.com/
本教程主要讲解接口的开发流程。
事例代码也跑不通。我研究了一番,终于搞定。方法如下:
一、写好接口程序
在你的服务器上上传好一个接口程序文件,如http://www.yourdomain.com/weixin.php 内容如下:
- <?php
- define("TOKEN", "weixin");//自己定义的token 就是个通信的私钥
- $wechatObj = new wechatCallbackapiTest();
- $wechatObj->valid();
- //$wechatObj->responseMsg();
- class wechatCallbackapiTest
- {
- public function valid()
- {
- $echoStr = $_GET["echostr"];
- if($this->checkSignature()){
- echo $echoStr;
- exit;
- }
- }
- public function responseMsg()
- {
- $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
- if (!empty($postStr)){
- $postObj = simplexml_load_string($postStr, 'SimpleXMLElement',
- LIBXML_NOCDATA);
- $fromUsername = $postObj->FromUserName;
- $toUsername = $postObj->ToUserName;
- $keyword = trim($postObj->Content);
- $time = time();
- $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(!empty( $keyword ))
- {
- $msgType = "text";
- $contentStr = '你好啊,屌丝';
- $resultStr = sprintf($textTpl, $fromUsername, $toUsername,
- $time, $msgType, $contentStr);
- echo $resultStr;
- }else{
- echo '咋不说哈呢';
- }
- }else {
- echo '咋不说哈呢';
- exit;
- }
- }
-
- private function checkSignature()
- {
- $signature = $_GET["signature"];
- $timestamp = $_GET["timestamp"];
- $nonce = $_GET["nonce"];
- $token =TOKEN;
- $tmpArr = array($token, $timestamp, $nonce);
- sort($tmpArr);
- $tmpStr = implode( $tmpArr );
- $tmpStr = sha1( $tmpStr );
-
- if( $tmpStr == $signature ){
- return true;
- }else{
- return false;
- }
- }
- }
- ?>
复制代码
二、配置微信公众平台回复接口
设置回复接口,填好URL和Token(url填上面的http://www.yourdomain.com/weixin.php,token必须跟上面程序里面定义的Token一致)
三、验证接口
用自己的个人微信关注下你的公众账号,给这个账号发一条消息过去,收到原样的消息返回,即验证成功了。
四、开始自定义回复
注释掉$wechatObj->valid(); 这行,同时去掉//$wechatObj->responseMsg();这行的注释。
你可以修改responseMsg函数里面的代码,根据用户的消息类型('text','image','location')和消息内容来回复用户不同的内容。
消息接口就可以使用了,发个消息试试看吧?
封装weixin.class.php
由于微信公众平台的通信使用的是特定格式的XML数据,每次接受和回复都要去做一大堆的数据处理。
我们就考虑在这个基础上做一次封装,weixin.class.php,代码如下:
- <?php
- class Weixin
- {
- public $token = '';//token
- public
- $debug = false;//是否debug的状态标示,方便我们在调试的时候记录一些中间数据
- public $setFlag =
- false;
- public $msgtype = 'text'; //('text','image','location')
-
- public $msg = array();
-
- public function
- __construct($token,$debug)
- {
- $this->token =
- $token;
- $this->debug = $debug;
- }<br>
- //获得用户发过来的消息(消息内容和消息类型 )
- public function getMsg()
- {
-
- $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
- if ($this->debug)
- {
- $this->write_log($postStr);
-
- }
- if (!empty($postStr)) {
- $this->msg =
- (array)simplexml_load_string($postStr, 'SimpleXMLElement',
- LIBXML_NOCDATA);
- $this->msgtype =
- strtolower($this->msg['MsgType']);
- }
- }<br>
- //回复文本消息
- public function makeText($text='')
- {
-
- $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);
- }<br>
- //根据数组参数回复图文消息
- 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;//微信公众平台图文回复的消息一次最多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)
- {
- if
- ($this->debug) {
-
- $this->write_log($data);
- }
- echo $data;
- }
-
- public function valid()
- {
- if ($this->checkSignature())
- {
- if( $_SERVER['REQUEST_METHOD']=='GET' )
-
- {
- echo $_GET['echostr'];
-
- exit;
- }
- }else{
-
- write_log('认证失败');
- 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;
- }
- }
- private function
- write_log($log){<br> //这里是你记录调试信息的地方 请自行完善
- 以便中间调试<br> }
- }
- ?>
复制代码
|
|