学卫网教程:是一个免费提供流行杀毒软件教程、在线学习分享的学习平台!

eaglephp运用微信api接口开发微信框架_php指南

微信(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
软件推荐:      

(网视站,专业提供浏览器下载)

相关软件

2345安全卫士最新官方版

2345安全卫士官方版 | 45.34MB

2345安全卫士官方版

2345安全卫士是集电脑体检、木马查杀、垃圾清理、修复系统漏洞、系统加速、软件管理等功能为一体的电脑安全管理的软件.提供全方位检测,用户可以通过检测结果快速了解自己的电脑并且对电脑进行优化..

360杀毒软件官方正式版

360杀毒软件官方正式版 | 35.10MB

360杀毒软件官方正式版

360杀毒具有查杀率高、资源占用少、升级迅速等优点。零广告、零打扰、零胁迫,一键扫描,快速、全面地诊断系统安全状况和健康程度,并进行精准修复,带来安全、专业、有效、新颖的查杀防护体验...

360安全卫士最新版下载

360安全卫士最新版下载 | 50.3MB

360安全卫士最新版下载

360安全卫士是一款由奇虎360公司推出的功能强、效果好、受用户欢迎的安全杀毒软件。360安全卫士拥有查杀木马、清理插件、修复漏洞、电脑体检、电脑救援、保护隐私,电脑专家,清理垃圾,清理痕迹...

QQ电脑管家官方正式版

QQ电脑管家官方正式版 | 24.2MB

QQ电脑管家官方正式版

腾讯电脑管家(Tencent PC Manager/原名QQ电脑管家)是腾讯公司推出的免费安全软件。拥有云查杀木马,系统加速,漏洞修复,实时防护,网速保护,电脑诊所,健康小助手...

金山毒霸2022最新版下载

金山毒霸2022下载 | 37MB

金山毒霸2018最新版下载

金山毒霸融合了启发式搜索、代码分析、虚拟机查毒等技术。经业界证明成熟可靠的反病毒技术,以及丰富的经验,使其在查杀病毒种类、查杀病毒速度、未知病毒防治等多方面达到世界先进水平...

猎豹清理大师官方版下载

猎豹清理大师下载 | 47.4MB

猎豹清理大师官方版下载

猎豹清理大师(原金山清理大师)是由金山网络开发的智能手机应用。它可以清理智能手机上的应用缓存、残余程序文件、历史痕迹以及应用程序安装包...