千家信息网

php中微信公众平台交互与接口的示例分析

发表于:2024-09-21 作者:千家信息网编辑
千家信息网最后更新 2024年09月21日,这篇文章主要介绍php中微信公众平台交互与接口的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!具体内容如下1、微信用户、微信服务器和后台服务器的交互例:微信用户向公众号
千家信息网最后更新 2024年09月21日php中微信公众平台交互与接口的示例分析

这篇文章主要介绍php中微信公众平台交互与接口的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

具体内容如下

1、微信用户、微信服务器和后台服务器的交互

例:微信用户向公众号发送一条文本消息,这条消息会首先传给微信服务器,微信服务器处理这条信息并将其以xml数据格式传递给后台服务器,后台服务器接受到数据后会对数据进行处理,再响应数据以xml数据格式传递给微信服务器,微信服务器再响应到用户微信界面。
微信用户与微信后台服务器之间的交互过程就是数据传递过程,只不过需要需要通过微信服务器这个中转站。

那么微信服务器这个中转站到底有什么用?
对xml数据进行加工包装后展现在手机屏幕上。我们接受的图文消息,如下:

单图文:

多图文

你会发现微信上几乎所有的图文都是这种格式,板式、大小都是一样,这就是经过微信服务器包装后的结果。

2、交互的数据类型

微信用户可以发送的数据类型
1、文本型(text)

%s";

2、语音(voice)

%s5836982871638042400//recognition表示语音识别的结果

3、图片( img)

%s5836982871638042400

每一条消息传给微信服务器后都会被标记一个MsgId,上传的图片、视频、语音等也会被标记一个mediaId。

4、视频(video)

%s5836982871638042400;//视频静止时显示那张图片地址

5、地理位置消息(location)

%s583698287163804240022.539968113.95498016

6、链接消息(link)

%s5836982871638042400<![CDATA[微信公众平台开发者的江湖]]>5839907284805129867

后台服务器响应的消息类型
1、文本型(text)
2、语音(voice)

%s5836982871638042400

3、图片( img)

%s5836982871638042400

4、视频(video)

%s5836982871638042400

5、音乐(music)

%s5836982871638042400<![CDATA[最炫民族风]]>

6、图文(news)

%s5836982871638042400%s<![CDATA[ 【深圳】实况 温度:6℃ 湿度:62﹪ 风速:东北风2级]]><![CDATA[ 【深圳】实况 温度:6℃ 湿度:62﹪ 风速:东北风2级]]>

上面代码在数据填写方面只做参照。以上代码在需要的时候调用即可,这里只是为大家展现以下数据格式。
CDATA是一个标记,被其标记的文本数据中不会被xml解析器进行解析。一个 CDATA 部件以"

ToUserName 接收方帐号
FromUserName 发送方帐号
CreateTime 发送事件
MsgType 数据类型
Content 文本内容
ArticleCount 图文数量
MsgId 数据id
MediaId 媒介id
Title 标题
Description 描述
MusicUrl 音乐连接地址
HQMusicUrl 高品质音乐连接地址

2、具体的交互步骤即代码

在上一章图2中,我们为测试号定义了url和token。url就是与微信服务器进行通信的后台服务器地址,而token一个相当于一个令牌。微信服务器与后台服务器进行通信时会出示该令牌,如果后台服务器发现微信服务器与自己携带的令牌相同才会进行通信,不相同则拒绝通信 。这个过程叫做token验证(这个令牌不是token的值)。
上面比较形象的说话,下面我通过代码来解释
例如:url为http://weixinceshi111111.applinzi.com/index2.php
token:weixin
index2.php代码

responseMsg();//响应数据}else{ $wechatObj->valid();//响应}class wechatCallbackapiTest{ public function valid() { $echoStr = $_GET["echostr"]; if($this->checkSignature()){ echo $echoStr; 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; } } public function responseMsg() { $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];//获取发送过来的数据。 if (!empty($postStr)){ $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', );//把xml字符串载入到一个SimpleXMLelement对象中。simplexml_load_string()是一种xml解析器。 $RX_TYPE = trim($postObj->MsgType);//trim去掉字符串两端kongge。 //用户发送的消息类型判断 switch ($RX_TYPE) { case "text":  $result = $this->receiveText($postObj);  break; case "image":  $result = $this->receiveImage($postObj);  break; case "voice":  $result = $this->receiveVoice($postObj);  break; case "video":  $result = $this->receiveVideo($postObj);  break; default:  $result = "unknow msg type: ".$RX_TYPE;  break; } echo $result; }else { echo ""; exit; } } private function receiveText($object) { $keyword = trim($object->Content); if($keyword == "文本"){ //回复文本消息 $content = "这是个文本消息"; $result = $this->transmitText($object, $content); } else if($keyword == "图文" || $keyword == "单图文"){ //回复单图文消息 $content = array(); $content[] = array("Title"=>"单图文标题",   "Description"=>"单图文内容",   "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg",   "Url" =>"http://m.cnblogs.com/?u=txw1958"); $result = $this->transmitNews($object, $content); } else if($keyword == "多图文"){ //回复多图文消息 $content = array(); $content[] = array("Title"=>"多图文1标题", "Description"=>"", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958"); $content[] = array("Title"=>"多图文2标题", "Description"=>"", "PicUrl"=>"http://d.hiphotos.bdimg.com/wisegame/pic/item/f3529822720e0cf3ac9f1ada0846f21fbe09aaa3.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958"); $content[] = array("Title"=>"多图文3标题", "Description"=>"", "PicUrl"=>"http://g.hiphotos.bdimg.com/wisegame/pic/item/18cb0a46f21fbe090d338acc6a600c338644adfd.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958"); $result = $this->transmitNews($object, $content); } else if($keyword == "音乐"){ //回复音乐消息 $content = array("Title"=>"最炫民族风",  "Description"=>"歌手:凤凰传奇",  "MusicUrl"=>"http://121.199.4.61/music/zxmzf.mp3", "HQMusicUrl"=>"http://121.199.4.61/music/zxmzf.mp3"); $result = $this->transmitMusic($object, $content); } return $result; } private function receiveImage($object) { //回复图片消息  $content = array("MediaId"=>$object->MediaId); $result = $this->transmitImage($object, $content);; return $result; } private function receiveVoice($object) { //回复语音消息  $content = array("MediaId"=>$object->MediaId); $result = $this->transmitVoice($object, $content);; return $result; } private function receiveVideo($object) { //回复视频消息  $content = array("MediaId"=>$object->MediaId, "ThumbMediaId"=>$object->ThumbMediaId, "Title"=>"", "Description"=>""); $result = $this->transmitVideo($object, $content);; return $result; }  /* * 回复文本消息,将要回复的xml消息进行包装。 */ private function transmitText($object, $content) { $textTpl = "%s"; $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content);//sprintf()这个函数的作用还是比较有意思的,可以搜索看看。 return $result; } /* * 回复图片消息 */ private function transmitImage($object, $imageArray) { $itemTpl = " "; $item_str = sprintf($itemTpl, $imageArray['MediaId']); $textTpl = "%s$item_str"; $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time()); return $result; } /* * 回复语音消息 */ private function transmitVoice($object, $voiceArray) { $itemTpl = " "; $item_str = sprintf($itemTpl, $voiceArray['MediaId']); $textTpl = "%s$item_str"; $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time()); return $result; } /* * 回复视频消息 */ private function transmitVideo($object, $videoArray) { $itemTpl = ""; $item_str = sprintf($itemTpl, $videoArray['MediaId'], $videoArray['ThumbMediaId'], $videoArray['Title'], $videoArray['Description']); $textTpl = "%s$item_str"; $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time()); return $result; } /* * 回复图文消息 */ private function transmitNews($object, $arr_item) { if(!is_array($arr_item)) return; $itemTpl = "  <![CDATA[%s]]>    "; $item_str = ""; foreach ($arr_item as $item) $item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']); $newsTpl = "%s%s$item_str"; $result = sprintf($newsTpl, $object->FromUserName, $object->ToUserName, time(), count($arr_item)); return $result; } /* * 回复音乐消息 */ private function transmitMusic($object, $musicArray) { $itemTpl = " <![CDATA[%s]]>   "; $item_str = sprintf($itemTpl, $musicArray['Title'], $musicArray['Description'], $musicArray['MusicUrl'], $musicArray['HQMusicUrl']); $textTpl = "%s$item_str"; $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time()); return $result; }}?>

3.接口

3.1 接口是什么

接口就相当于一个工具,具备特定的功能。比如你在建造房子的时候需要在墙上钻孔,你就会使用钻机工具来钻孔。从调来工具到钻孔完成,你要完成插电、校准、钻孔等一系列步骤,最终实现你的目标。钻机就是我们的接口,插电、校准、钻孔就是我们调用工具完成目的步骤。

微信的创建菜单接口举例。

调用接口的步骤:
1、获得微信菜单接口的连接地址,通过curl函数与这个接口建立对话。
2、把创建菜单数据发送给这个接口。
接口调用完成,这个接口会自动把这些数据进行处理并在微信公众好页面生成菜单。

以上是"php中微信公众平台交互与接口的示例分析"这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!

0