Home  >  Article  >  php教程  >  php编写微信公众平台消息处理接口教程

php编写微信公众平台消息处理接口教程

WBOY
WBOYOriginal
2016-06-06 19:45:46930browse

有许多同学都在群里问如何使用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格式的消息模版即可 实现回复。我们这样写

 

/* 
     QQ群 : 171055458

*/
class Tpl{
function txt($fu,$tu,$content,$flag = 0){
  $tpl = " 
     
     
    ".$_SERVER['REQUEST_TIME']." 
     
     
    ".$flag."
    
";
  echo $tpl;
}

function news($fu,$tu,$data,$flg = 0){
  $num = count($data);
  if($num > 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   

/* 
一般性微信接口开发,均可采用此处理流程来写

   QQ群 : 171055458*/
require('Tpl.class.php'); //载入 Tpl.class.php 文件
define('TOKEN','mytoken'); //定义TOKEN 常量 MP 上验证的时候就填写这个值 mytoken
$WeiXin = new WeiXin; //实例化 WeiXin 类 
$WeiXin->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;
}
}

}

将上面的代码上传到服务器上之后就可以到微信公众平台验证了。

php编写微信公众平台消息处理接口教程

URL填写的是我们上传到服务器的地址 如http://你的域名/index.php

token 填写的是你在index.php中定义的token  如mytoken

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn