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'], //地