本文分为三大部分为大家进行介绍,具体内容如下
1、微信用户、微信服务器和后台服务器的交互
例:微信用户向公众号发送一条文本消息,这条消息会首先传给微信服务器,微信服务器处理这条信息并将其以xml数据格式传递给后台服务器,后台服务器接受到数据后会对数据进行处理,再响应数据以xml数据格式传递给微信服务器,微信服务器再响应到用户微信界面。
微信用户与微信后台服务器之间的交互过程就是数据传递过程,只不过需要需要通过微信服务器这个中转站。
那么微信服务器这个中转站到底有什么用?
对xml数据进行加工包装后展现在手机屏幕上。我们接受的图文消息,如下:
单图文:
多图文
你会发现微信上几乎所有的图文都是这种格式,板式、大小都是一样,这就是经过微信服务器包装后的结果。
2、交互的数据类型
微信用户可以发送的数据类型
1、文本型(text)
<xml>
<tousername><![cdata[%s]]></tousername>
<fromusername><![cdata[%s]]></fromusername>
<createtime>%s</createtime>
<msgtype><![cdata[text]]></msgtype>
<content><![cdata[%s]]></content>
</xml>";
2、语音(voice)
<xml>
<tousername><![cdata[%s]]></tousername>
<fromusername><![cdata[%s]]></fromusername>
<createtime>%s</createtime>
<msgtype><![cdata[voice]]></msgtype>
<format><![cdata[amr]]></format>
<msgid>5836982871638042400</msgid>
<mediaid><![cdata[pgkso3lagbvtsfyo7fgu51kuya07d0c_nozz2fn1z6vythosf59ptfl0vaggxkvh]]></mediaid>
<recognition><![cdata[]]></recognition>//recognition表示语音识别的结果
</xml>
3、图片( img)
<xml>
<tousername><![cdata[%s]]></tousername>
<fromusername><![cdata[%s]]></fromusername>
<createtime>%s</createtime>
<msgtype><![cdata[image]]></msgtype>
<picurl><![cdata[http://mmbiz.qpic.cn/mmbiz/l4qjytoibummhn90t1mnaibyiar8ljyicf3mw7xx3blp1qzgub7ctz0d]]></picurl>
<msgid>5836982871638042400</msgid>
<mediaid><![cdata[pgkso3lagbvtsfyo7fgu51kuya07d0c_nozz2fn1z6vythosf59ptfl0vaggxkvh]]></mediaid>
</xml>
每一条消息传给微信服务器后都会被标记一个msgid,上传的图片、视频、语音等也会被标记一个mediaid。
4、视频(video)
<xml>
<tousername><![cdata[%s]]></tousername>
<fromusername><![cdata[%s]]></fromusername>
<createtime>%s</createtime>
<msgtype><![cdata[vedio]]></msgtype>
<msgid>5836982871638042400</msgid>
<mediaid><![cdata[pgkso3lagbvtsfyo7fgu51kuya07d0c_nozz2fn1z6vythosf59ptfl0vaggxkvh]]></mediaid>
<thumbmediaid><![cdata[mxuj5gcceesjwx2t9qsk62yzi
clcp_hnrdftqcojlpet2g9q3d22ukslybflz01j]]></thumbmediald>;//视频静止时显示那张图片地址
</xml>
5、地理位置消息(location)
<xml>
<tousername><![cdata[%s]]></tousername>
<fromusername><![cdata[%s]]></fromusername>
<createtime>%s</createtime>
<msgtype><![cdata[location]]></msgtype>
<msgid>5836982871638042400</msgid>
<location_x>22.539968</location_x>
<location_y>113.954980</location_y>
<scale>16</scale>
<label><![cdata[中国广东省深圳市南山区深南大道9001号
邮政编码: 518053]]></label>
</xml>
6、链接消息(link)
<xml>
<tousername><![cdata[%s]]></tousername>
<fromusername><![cdata[%s]]></fromusername>
<createtime>%s</createtime>
<msgtype><![cdata[link]]></msgtype>
<msgid>5836982871638042400</msgid>
<title><![cdata[微信公众平台开发者的江湖]]></title>
<description><![cdata[陈坤的微信公众号这段时间大火,大家...]]></description>
<url><![cdata[http://www.cnblogs.com/txw1958/]]></url>
<msgid>5839907284805129867</msgid>
</xml>
后台服务器响应的消息类型
1、文本型(text)
2、语音(voice)
<xml>
<tousername><![cdata[%s]]></tousername>
<fromusername><![cdata[%s]]></fromusername>
<createtime>%s</createtime>
<msgtype><![cdata[voice]]></msgtype>
<msgid>5836982871638042400</msgid>
<voice>
<mediaid><![cdata[pgkso3lagbvtsfyo7fgu51kuya07d0c_nozz2fn1z6vythosf59ptfl0vaggxkvh]]></mediaid>
</voice>
</xml>
3、图片( img)
<xml>
<tousername><![cdata[%s]]></tousername>
<fromusername><![cdata[%s]]></fromusername>
<createtime>%s</createtime>
<msgtype><![cdata[image]]></msgtype>
<msgid>5836982871638042400</msgid>
<image>
<mediaid><![cdata[pgkso3lagbvtsfyo7fgu51kuya07d0c_nozz2fn1z6vythosf59ptfl0vaggxkvh]]></mediaid>
</image>
</xml>
4、视频(video)
<xml>
<tousername><![cdata[%s]]></tousername>
<fromusername><![cdata[%s]]></fromusername>
<createtime>%s</createtime>
<msgtype><![cdata[vedio]]></msgtype>
<msgid>5836982871638042400</msgid>
<video>
<mediaid><![cdata[pgkso3lagbvtsfyo7fgu51kuya07d0c_nozz2fn1z6vythosf59ptfl0vaggxkvh]]></mediaid>
<thumbmediaid><![cdata[mxuj5gcceesjwx2t9qsk62yzi
clcp_hnrdftqcojlpet2g9q3d22ukslybflz01j]]></thumbmediald>;//视频静止时显示那张图片地址
</video>
</xml>
5、音乐(music)
<xml>
<tousername><![cdata[%s]]></tousername>
<fromusername><![cdata[%s]]></fromusername>
<createtime>%s</createtime>
<msgtype><![cdata[voice]]></msgtype>
<msgid>5836982871638042400</msgid>
<music>
<title><![cdata[最炫民族风]]></title>
<description><![cdata[凤凰传奇]]></description>
<musicurl><![cdata[http://zj189.cn/zj/download/music/zxmzf.mp3]]></musicurl>
<hqmusicurl><![cdata[http://zj189.cn/zj/dodownload/music/zxmzf.mp3]]></hqmusicurl>
</music>
</xml>
6、图文(news)
<xml>
<tousername><![cdata[%s]]></tousername>
<fromusername><![cdata[%s]]></fromusername>
<createtime>%s</createtime>
<msgtype><![cdata[news]]></msgtype>
<msgid>5836982871638042400</msgid>
<content><![cdata[]]></content>
<articlecount>%s</articlecount>
<articles>
<item>
<title><![cdata[ 【深圳】实况 温度:6℃ 湿度:62﹪ 风速:东北风2级]]></title>
<description><![cdata[]]></description>
<picurl><![cdata[http://www.doucube.com/weixin/weather/icon/banner.jpg]]></picurl>
<url><![cdata[]]></url>
</item>
<item>
<title><![cdata[ 【深圳】实况 温度:6℃ 湿度:62﹪ 风速:东北风2级]]></title>
<description><![cdata[]]></description>
<picurl><![cdata[http://www.doucube.com/weixin/weather/icon/banner.jpg]]></picurl>
<url><![cdata[]]></url>
</item>
</articles>
</xml>
上面代码在数据填写方面只做参照。以上代码在需要的时候调用即可,这里只是为大家展现以下数据格式。
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代码
<?php
//
// 响应用户消息
// 微信公众账号响应给用户的不同消息类型
//微信服务器要和后台服务器进行通信首先要进行token验证,微信会通过get方式发送signature(微信加密签名)、nonce(随机数)、timestamp(时间戳)、echostr(随机字符串)。后台服务器获取之后会将timestamp、nonce与自身定义的token按照一定的顺序拼接成字符串,通过shal加密后获得的结果与signature进行对比,如果相同则把echostr返回给微信服务器。 表示验证成功。
header("content-type:text;charset=utf8");
define("token", "weixin");
//token验证是通过get传输数据,微信用户发送的数据通过post方式发送。先进行get请求,再进行post请求。
$wechatobj = new wechatcallbackapitest();
//判断是get请求还是post请求。$_get['echostr']如果存在,表示是进行token验证的get请求。反之是传输数据的post请求。
if (!isset($_get['echostr'])) {
$wechatobj->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 = "<xml>
<tousername><![cdata[%s]]></tousername>
<fromusername><![cdata[%s]]></fromusername>
<createtime>%s</createtime>
<msgtype><![cdata[text]]></msgtype>
<content><![cdata[%s]]></content>
</xml>";
$result = sprintf($texttpl, $object->fromusername, $object->tousername, time(), $content);//sprintf()这个函数的作用还是比较有意思的,可以搜索看看。
return $result;
}
/*
* 回复图片消息
*/
private function transmitimage($object, $imagearray)
{
$itemtpl = "<image>
<mediaid><![cdata[%s]]></mediaid>
</image>";
$item_str = sprintf($itemtpl, $imagearray['mediaid']);
$texttpl = "<xml>
<tousername><![cdata[%s]]></tousername>
<fromusername><![cdata[%s]]></fromusername>
<createtime>%s</createtime>
<msgtype><![cdata[image]]></msgtype>
$item_str
</xml>";
$result = sprintf($texttpl, $object->fromusername, $object->tousername, time());
return $result;
}
/*
* 回复语音消息
*/
private function transmitvoice($object, $voicearray)
{
$itemtpl = "<voice>
<mediaid><![cdata[%s]]></mediaid>
</voice>";
$item_str = sprintf($itemtpl, $voicearray['mediaid']);
$texttpl = "<xml>
<tousername><![cdata[%s]]></tousername>
<fromusername><![cdata[%s]]></fromusername>
<createtime>%s</createtime>
<msgtype><![cdata[voice]]></msgtype>
$item_str
</xml>";
$result = sprintf($texttpl, $object->fromusername, $object->tousername, time());
return $result;
}
/*
* 回复视频消息
*/
private function transmitvideo($object, $videoarray)
{
$itemtpl = "<video>
<mediaid><![cdata[%s]]></mediaid>
<thumbmediaid><![cdata[%s]]></thumbmediaid>
<title><![cdata[%s]]></title>
<description><![cdata[%s]]></description>
</video>";
$item_str = sprintf($itemtpl, $videoarray['mediaid'], $videoarray['thumbmediaid'], $videoarray['title'], $videoarray['description']);
$texttpl = "<xml>
<tousername><![cdata[%s]]></tousername>
<fromusername><![cdata[%s]]></fromusername>
<createtime>%s</createtime>
<msgtype><![cdata[video]]></msgtype>
$item_str
</xml>";
$result = sprintf($texttpl, $object->fromusername, $object->tousername, time());
return $result;
}
/*
* 回复图文消息
*/
private function transmitnews($object, $arr_item)
{
if(!is_array($arr_item))
return;
$itemtpl = " <item>
<title><![cdata[%s]]></title>
<description><![cdata[%s]]></description>
<picurl><![cdata[%s]]></picurl>
<url><![cdata[%s]]></url>
</item>
";
$item_str = "";
foreach ($arr_item as $item)
$item_str .= sprintf($itemtpl, $item['title'], $item['description'], $item['picurl'], $item['url']);
$newstpl = "<xml>
<tousername><![cdata[%s]]></tousername>
<fromusername><![cdata[%s]]></fromusername>
<createtime>%s</createtime>
<msgtype><![cdata[news]]></msgtype>
<content><![cdata[]]></content>
<articlecount>%s</articlecount>
<articles>
$item_str</articles>
</xml>";
$result = sprintf($newstpl, $object->fromusername, $object->tousername, time(), count($arr_item));
return $result;
}
/*
* 回复音乐消息
*/
private function transmitmusic($object, $musicarray)
{
$itemtpl = "<music>
<title><![cdata[%s]]></title>
<description><![cdata[%s]]></description>
<musicurl><![cdata[%s]]></musicurl>
<hqmusicurl><![cdata[%s]]></hqmusicurl>
</music>";
$item_str = sprintf($itemtpl, $musicarray['title'], $musicarray['description'], $musicarray['musicurl'], $musicarray['hqmusicurl']);
$texttpl = "<xml>
<tousername><![cdata[%s]]></tousername>
<fromusername><![cdata[%s]]></fromusername>
<createtime>%s</createtime>
<msgtype><![cdata[music]]></msgtype>
$item_str
</xml>";
$result = sprintf($texttpl, $object->fromusername, $object->tousername, time());
return $result;
}
}
?>
3.接口
3.1 接口是什么
接口就相当于一个工具,具备特定的功能。比如你在建造房子的时候需要在墙上钻孔,你就会使用钻机工具来钻孔。从调来工具到钻孔完成,你要完成插电、校准、钻孔等一系列步骤,最终实现你的目标。钻机就是我们的接口,插电、校准、钻孔就是我们调用工具完成目的步骤。
微信的创建菜单接口举例。
调用接口的步骤:
1、获得微信菜单接口的连接地址,通过curl函数与这个接口建立对话。
2、把创建菜单数据发送给这个接口。
接口调用完成,这个接口会自动把这些数据进行处理并在微信公众好页面生成菜单。
微信接口的调用方式请看下一章:微信公众平台开发(三):微信高级接口的调用。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持硕编程。
【说明】:
本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!