有许多同学都在群里问如何使用php编写微信公众平台处理接口教程。以下,微市场就为大家整理了处理消息的教程。希望能对大家有帮助! 一.创建接口文件 微信开发者模式首先需要我们有自己的服务器,很多同学没有自己的服务器,那么可以去淘宝买虚拟空间,几块
有许多同学都在群里问如何使用php编写微信公众平台处理接口教程。以下,微市场就为大家整理了处理消息的教程。希望能对大家有帮助!
一. 创建接口文件
微信开发者模式首先需要我们有自己的服务器,很多同学没有自己的服务器,那么可以去淘宝买虚拟空间,几块钱就有了,也可以用新浪sae。具体怎么弄可以去百度一下,这里我们就不介绍了。
二. 写接口文件内部方法
然后,我们使用编辑器 notepad++ 打开 index.php 这个文件,注意要用utf-8模式 ,写一个 处理微信公众平台请求的入口函数。
当用户发送消息给公众帐号时,公众平台会将消息以 xml 格式 post到接口url去处理。那么接口的入口函数我们就应该先获取post过来的xml内容。
可以使用以下两种方式。
$poststr = file_get_contents(php://input);
接收完以后就存储给叫 $poststr 的变量了,下面 就要将xml中的对象值取出来了。
$postobj = simplexml_load_string($poststr, 'simplexmlelement', libxml_nocdata);
$fu = (string)$postobj->fromusername;
$tu = (string)$postobj->tousername;
$msgtype = (string)$postobj->msgtype;
变量 $fu 就是用户的账号 $tu 就是公众账号 $msgtype 就是消息类型 目前支持的有 图片 位置 文字 和 事件 。
取出这几个主要的值以后 我们就可以根据需要来写处理逻辑了。
要实现回复消息的话 我们写一个 消息模版的类。这里我们新建一个 php 文件 命名为 tpl.class.php
打开 编辑此文件,把用到的消息模版写进入 方便调用。目前有 文字类型消息 图文类型消息 音乐类型消息 实现自动回复,就是再收到微信公众平台post 的请求时 echo 一个xml格式的消息模版即可 实现回复。我们这样写
1){
$add = $this->news_add($data);
$tpl =
.$_server['request_time'].
.$num.
.$add.
.$flag.
;
echo $tpl;
}else{
$tpl =
.$_server['request_time'].
1
.$flag.
;
echo $tpl;
}
}
function news_add($data){
$add = ;
foreach ($data as $k){
$add .=
;
}
return $add;
}
function audio($fu,$tu,$data){
$tpl =
.$_server['request_time'].
0
;
echo $tpl;
}
}
返回编辑主文件 index.php
send(); //请求该接口文件时 先执行 send 函数
class weixin extends tpl{
//此函数表示所在类被实例化的同时 将会执行它内部的方法。
function __construct(){
if(!$this->checksignature()) exit; //检查来路 可提高安全性
}
public function send(){
//获取输入流并取出主要对象的值
$poststr = file_get_contents(php://input);
$postobj = simplexml_load_string($poststr, 'simplexmlelement', libxml_nocdata);
$fu = (string)$postobj->fromusername; //取出用户的账号
$tu = (string)$postobj->tousername; //公众账号
$msgtype = (string)$postobj->msgtype; //取出消息类型
//根据消息类型进入相应操作
switch($msgtype){
case 'text': //文字消息类型
$content = trim($postobj->content); // 取出消息内容
//根据发送的内容回复相应的内容
switch($content){
case '0': //如果发送的是数字0则回复以下内容
$this->txt($fu,$tu,'你发送了数字 0 ,所以你就看到了这么多 000000000000。');
break;
case '1': //如果发送的是数字1则回复以下内容
$this->txt($fu,$tu,'你发送了数字 1 ,所以你就看到了这么多 111111111111。');
break;
case '2': //如果发送的是数字2 则回复以下内容
$msg['title'] = '音频文件标题';
$msg['intro'] = '音频文件简介';
$msg['url'] = 'http://facebowl.in/everythingisbetter.mp3'; //音频文件的绝对完整 url
$msg['hqurl'] = 'http://facebowl.in/everythingisbetter.mp3'; //音频文件的高清绝对完整 url wifi下优先播放此url.
$this->audio($fu,$tu,$msg); //回复一个可播放的音频消息
break;
default: //如果是其他内容则回复
$this->txt($fu,$tu,'你发送的内容是。'.$content);
break;
}
break;
case 'image': //图像消息类型
$pic = (string)$postobj->picurl; //取出图片url
//回复一个图文 开始定义数组
$msg[0]['title'] = '这里是标题';
$msg[0]['intro'] = '简介';
$msg[0]['pic'] = $pic; //图片url 这里是返回发送的原图 url 要使用绝对完整地址
$msg[0]['url'] = 'http://bbs.binguo.me'; //这里是超链接
$this->news($fu,$tu,$msg);
//如要回复多条图文,可对该二维数组赋值多个元素 如 $msg[0] $msg[1] $msg[2] 。
break;
case 'location': //地理位置消息类型
$l_x = $postobj->location_x; //取出 x 坐标
$l_y = $postobj->location_y; //取出 y 坐标
$scale = $postobj->scale; //取出 缩放等级
$lable = $postobj->label; //取出 位置信息
//回复文字消息
$this->txt($fu,$tu,'你所处位置是:'.$lable.'坐标为 x:'.$l_x.'y:'.$l_y);
break;
case 'event': //事件消息类型
$event = $postobj->event; //取出事件内容
$eventkey = $postobj->eventkey; //取出事件标识
switch($event){
case 'subscribe': //如果为 订阅 事件
$this->txt($fu,$tu,'欢迎关注我们,发送数字 0 试试。');
break;
}
break;
default:
//默认执行接口验证方法
$this->valid();
break;
}
}
//验证接口的方法 也可直接 echo $_get[echostr]; token 任意设置。
public function valid(){
$echostr = $_get[echostr];
//valid signature , option
if($this->checksignature()){
echo $echostr;
exit;
}
}
//检查token是否一致 可用来检测请求来路是否为微信
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填写的是我们上传到服务器的地址 如http://你的域名/index.php
token 填写的是你在index.php中定义的token 如mytoken