微信(WeChat)是腾讯公司于2011年1月21日推出的一个为智能终端提供即时通讯服务的免费应用程序,由张小龙所带领的腾讯广州研发中心产品团队打造 。微信支持跨通信运营商、跨操作系统平台通过网络快速发送免费(需消耗少量网络流量)语音短信、视频、图片和文字,同时,也可以使用通过共享流媒体内容的资料和基于位置的社交插件“摇一摇”、“漂流瓶”、“朋友圈”、”公众平台“、”语音记事本“等服务插件。
EaglePHP框架开发微信5.0的API接口,包含微信5.0 API基础接口、自定义菜单、高级接口,包括如下接收用户消息、向用户回复消息、会话界面自定义菜单、语音识别、客服接口等功能
适用平台:window/Linux
依赖项目:EaglePHP框架
包含微信5.0 API基础接口、自定义菜单、高级接口,具体如下:
1、接收用户消息。
2、向用户回复消息。
3、接受事件推送。
4、会话界面自定义菜单。
5、语音识别。
6、客服接口。
7、OAuth2.0网页授权。
8、生成带参数二维码。
9、获取用户地理位置。
10、获取用户基本信息。
11、获取关注者列表。
12、用户分组。
复制代码 代码如下:
<?php
/**
* 微信公众平台API
*/
class WeixinChat
{
private $token;
private $appid;
private $appsecret;
private $access_token;
// 接收的数据
private $_receive = array();
private $_reply = '';
// 接口错误码
private $errCode = '';
// 接口错误信息
private $errMsg = '';
// 微信oauth登陆获取code
const CONNECT_OAUTH_AUTHORIZE_URL = 'https://open.weixin.qq.com/connect/oauth2/authorize?';
// 微信oauth登陆通过code换取网页授权access_token
const SNS_OAUTH_ACCESS_TOKEN_URL = 'https://api.weixin.qq.com/sns/oauth2/access_token?';
// 微信oauth登陆刷新access_token(如果需要)
const SNS_OAUTH_REFRESH_TOKEN_URL = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?';
// 通过ticket换取二维码
const SHOW_QRCODE_URL = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?';
// 微信oauth登陆拉取用户信息(需scope为 snsapi_userinfo)
const SNS_USERINFO_URL = 'https://api.weixin.qq.com/sns/userinfo?';
// 请求api前缀
const API_URL_PREFIX = 'https://api.weixin.qq.com/cgi-bin';
// 自定义菜单创建
const MENU_CREATE_URL = '/menu/create?';
// 自定义菜单查询
const MENU_GET_URL = '/menu/get?';
// 自定义菜单删除
const MENU_DELETE_URL = '/menu/delete?';
// 获取 access_token
const AUTH_URL = '/token?grant_type=client_credential&';
// 获取用户基本信息
const USER_INFO_URL = '/user/info?';
// 获取关注者列表
const USER_GET_URL = '/user/get?';
// 查询分组
const GROUPS_GET_URL = '/groups/get?';
// 创建分组
const GROUPS_CREATE_URL = '/groups/create?';
// 修改分组名
const GROUPS_UPDATE_URL = '/groups/update?';
// 移动用户分组
const GROUPS_MEMBERS_UPDATE_URL = '/groups/members/update?';
// 发送客服消息
const MESSAGE_CUSTOM_SEND_URL = '/message/custom/send?';
// 创建二维码ticket
const QRCODE_CREATE_URL = '/qrcode/create?';
/**
* 初始化配置数据
* @param array $options
*/
public function __construct($options)
{
$this->token = isset($options['token']) ? $options['token'] : '';
$this->appid = isset($options['appid']) ? $options['appid'] : '';
$this->appsecret = isset($options['appsecret']) ? $options['appsecret'] : '';
}
/**
* 获取发来的消息
* 当普通微信用户向公众账号发消息时,微信服务器将POST消息的XML数据包到开发者填写的URL上。
*/
public function getRev()
{
$postStr = file_get_contents('php://input');
if($postStr)
{
$this->_receive = (array)simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
//Log::info(var_export($this->_receive, true));
}
return $this;
}
/**
* 获取微信服务器发来的消息
*/
public function getRevData()
{
return $this->_receive;
}
/**
* 获取接收者
*/
public function getRevTo()
{
return isset($this->_receive['ToUserName']) ? $this->_receive['ToUserName'] : false;
}
/**
* 获取消息发送者(一个OpenID)
*/
public function getRevFrom()
{
return isset($this->_receive['FromUserName']) ? $this->_receive['FromUserName'] : false;
}
/**
* 获取接收消息创建时间 (整型)
*/
public function getRevCTime()
{
return isset($this->_receive['CreateTime']) ? $this->_receive['CreateTime'] : false;
}
/**
* 获取接收消息类型(text、image、voice、video、location、link、event)
*/
public function getRevType()
{
return isset($this->_receive['MsgType']) ? $this->_receive['MsgType'] : false;
}
/**
* 获取接收消息编号
*/
public function getRevId()
{
return isset($this->_receive['MsgId']) ? $this->_receive['MsgId'] : false;
}
/**
* 获取接收消息文本
* 通过语音识别接口,用户发送的语音,将会同时给出语音识别出的文本内容。(需申请服务号的高级接口权限)
*/
public function getRevText()
{
if(isset($this->_receive['Content'])) return trim($this->_receive['Content']);
elseif(isset($this->_receive['Recognition'])) return trim($this->_receive['Recognition']);
else return false;
}
/**
* 获取接收图片消息
*/
public function getRevImage()
{
if(isset($this->_receive['PicUrl'])){
return array(
'picUrl' => $this->_receive['PicUrl'], //图片链接
'mediaId' => $this->_receive['MediaId'] //图片消息媒体id,可以调用多媒体文件下载接口拉取数据。
);
}
return false;
}
/**
* 获取接收语音消息
*/
public function getRevVoice()
{
if(isset($this->_receive['MediaId'])){
return array(
'mediaId' => $this->_receive['MediaId'], //语音消息媒体id,可以调用多媒体文件下载接口拉取数据。
'format' => $this->_receive['Format'] //语音格式,如amr,speex等
);
}
return false;
}
/**
* 获取接收视频消息
*/
public function getRevVideo()
{
if(isset($this->_receive['MediaId'])){
return array(
'mediaId' => $this->_receive['MediaId'], //视频消息媒体id,可以调用多媒体文件下载接口拉取数据。
'thumbMediaId' => $this->_receive['ThumbMediaId'] //视频消息缩略图的媒体id,可以调用多媒体文件下载接口拉取数据。
);
}
return false;
}
/**
* 获取用户地理位置
*/
public function getRevLocation()
{
if(isset($this->_receive['Location_X'])){
return array(
'locationX' => $this->_receive['Location_X'], //地理位置维度
'locationY' => $this->_receive['Location_Y'], //地理位置经度
'scale' => $this->_receive['Scale'], //地图缩放大小
'label' => $this->_receive['Label'] //地理位置信息
);
}
//开通了上报地理位置接口的公众号,用户在关注后进入公众号会话时,会弹框让用户确认是否允许公众号使用其地理位置。
//弹框只在关注后出现一次,用户以后可以在公众号详情页面进行操作。
elseif(isset($this->_receive['Latitude']))
{
return array(
'latitude' => $this->_receive['Latitude'], //地理位置纬度
'longitude' => $this->_receive['Longitude'], //地理位置经度
'precision' => $this->_receive['Precision'] // 地理位置精度
);
}
return false;
}
/**
* 获取接收链接消息
*/
public function getRevLink()
{
if(isset($this->_receive['Title'])){
return array(
'title' => $this->_receive['Title'], //消息标题
'description' => $this->_receive['Description'], //消息描述
'url' => $this->_receive['Url'] //消息链接
);
}
return false;
}
/**
* 获取接收事件类型
* 事件类型如:subscribe(订阅)、unsubscribe(取消订阅)、click
*/
public function getRevEvent()
{
if(isset($this->_receive['Event']))
{
return array(
'event' => strtolower($this->_receive['Event']),
'key'=> isset($this->_receive['EventKey']) ? $this->_receive['EventKey'] : ''
);
}
return false;
}
/**
* 设置回复文本消息
* @param string $content
* @param string $openid
*/
public function text($content='')
{
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$this->_reply = sprintf($textTpl,
$this->getRevFrom(),
$this->getRevTo(),
Date::getTimeStamp(),
'text',
$content
);
return $this;
}
/**
* 设置回复音乐信息
* @param string $title
* @param string $desc
* @param string $musicurl
* @param string $hgmusicurl
*/
public function music($title, $desc, $musicurl, $hgmusicurl='')
{
$textTpl = '<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Music>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<MusicUrl><![CDATA[%s]]></MusicUrl>
<HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
</Music>
</xml>';
//<ThumbMediaId><![CDATA[%s]]></ThumbMediaId>
$this->_reply = sprintf($textTpl,
$this->getRevFrom(),
$this->getRevTo(),
Date::getTimeStamp(),
'music',
$title,
$desc,
$musicurl,
$hgmusicurl
);
return $this;
}
/**
* 回复图文消息
* @param array
*/
public function news($data)
{
$count = count($data);
$subText = '';
if($count > 0)
{
foreach($data as $v)
{
$tmpText = '<item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>';
$subText .= sprintf(
$tmpText, $v['title'],
isset($v['description']) ? $v['description'] : '',
isset($v['picUrl']) ? $v['picUrl'] : '',
isset($v['url']) ? $v['url'] : ''
);
}
}
$textTpl = '<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime><![CDATA[%s]]></CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount><![CDATA[%d]]></ArticleCount>
<Articles>%s</Articles>
</xml>';
$this->_reply = sprintf(
$textTpl,
$this->getRevFrom(),
$this->getRevTo(),
Date::getTimeStamp(),
$count,
$subText
);
return $this;
}
/**
* 回复消息
* @param array $msg
* @param bool $return
*/
public function reply()
{
header('Content-Type:text/xml');
echo $this->_reply;
exit;
}
/**
* 自定义菜单创建
* @param array 菜单数据
*/
public function createMenu($data)
{
if(!$this->access_token && !$this->checkAuth()) return false;
$result = curlRequest(self::API_URL_PREFIX.self::MENU_CREATE_URL.'access_token='.$this->access_token, $this->jsonEncode($data), 'post');
if($result)
{
$jsonArr = json_decode($result, true);
if(!$jsonArr
软件推荐:
(网视站,专业提供浏览器下载)