资源预览内容
第1页 / 共7页
第2页 / 共7页
第3页 / 共7页
第4页 / 共7页
第5页 / 共7页
第6页 / 共7页
第7页 / 共7页
亲,该文档总共7页全部预览完了,如果喜欢就下载吧!
资源描述
PHP对接微信公众平台消息接口开发流程教程这篇文章主要介绍了PHP对接微信公众平台消息接口开发流程,如何使用PHP版接口操作公众平台消息,需要的朋友可以参考下一、写好接口程序 在你的服务器上上传好一个接口程序文件,如http:/www.yourdomain.com/weixin.php 内容如下: 复制代码 代码如下:valid();/$wechatObj-responseMsg();class wechatCallbackapiTest public function valid() $echoStr = $_GETechostr; if($this-checkSignature() echo $echoStr; exit; public function responseMsg() $postStr = $GLOBALSHTTP_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 = %s 0 ; 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 = $_GETsignature; $timestamp = $_GETtimestamp; $nonce = $_GETnonce; $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)和消息内容来回复用户不同的内容。消息接口就可以使用了,发个消息试试看吧? 1.封装weixin.class.php 由于微信公众平台的通信使用的是特定格式的XML数据,每次接受和回复都要去做一大堆的数据处理。我们就考虑在这个基础上做一次封装,weixin.class.php,代码如下:复制代码 代码如下:token = $token; $this-debug = $debug; /获得用户发过来的消息(消息内容和消息类型 ) public function getMsg() $postStr = $GLOBALSHTTP_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-msgMsgType); /回复文本消息 public function makeText($text=www.hunanwang.net) $CreateTime = time(); $FuncFlag = $this-setFlag ? 1 : 0; $textTpl = msgFromUserName msgToUserName $CreateTime %s ; return sprintf($textTpl,$text,$FuncFlag); /根据数组参数回复图文消息 public function makeNews($newsData=array() $CreateTime = time(); $FuncFlag = $this-setFlag ? 1 : 0; $newTplHeader = msgFromUserName msgToUserName $CreateTime %s; $newTplItem = ; $newTplFoot = %s ; $Content =
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号