PHP微信公众号开发之自动回复
先把源码类发出来
- <?php
- /**
- 自己封装 微信 开发api
- */
- header('Content-type: text/html; charset=utf-8');#设置头信息
- class zhphpWeixinApi{
- //定义属性
- private $userPostData; #微信反馈给平台的数据集
- private $fromUserName; #发微信用户姓名
- private $toUserName; #接受微信用户姓名
- private $keyword; #接受用户发的微信信息
- private $createTime; #创建时间
- private $requestId;#获取接收消息编号
- private $msgType; #用户发的微信的类型
- public $token; #api token
- private $appid;#开发者 id
- private $appSecret;# 开发者的应用密钥
- private $access_token;#微信平台返回的access_token
- private $expires_in=0;#权限的期限
- public $weixinConfig=array();#微信全局配置
- public $debug=false;
- private $saveFilePath; //缓存文件保存路径
- public $oauthAccessToken; ##第三方网页授权accecctoken
- public $oauthOpenId;##授权后的用户id
- /**
- $wx_msgType为数组,可以依据账号的权限补充
- */
- private $wx_msgType=array(
- 'text',#文本消息内容类型
- 'image',#图片消息内容类型
- 'voice',#语音消息内容类型
- 'video',#视频消息内容类型
- 'link',#链接消息内容类型
- 'location',#本地地理位置消息内容类型
- 'event',#事件消息内容类型
- 'subscribe',#是否为普通关注事件
- 'unsubscribe',#是否为取消关注事件
- 'music',#音乐消息内容类型
- 'news',#新闻消息内容
- );
- /**
- 配置文件
- $config=array(
- 'token'=>'',
- 'appid'=>'开发者 id ',
- 'appSecret'=>'应用密钥'
- )
- */
- public function setConfig($config){
- if( ! empty( $config ) ){
- $this->weixinConfig=$config;
- }elseif( empty($config) && ! empty($this->weixinConfig) ){
- $config=$this->weixinConfig;
- }
- #配置参数属性,这里使用 isset进行了判断,目的是为后续程序判断提供数据
- $this->token=isset($config['token'])?$config['token']:null;
- $this->appid=isset($config['appid'])?$config['appid']:null;
- $this->appSecret=isset($config['appSecret'])?$config['appSecret']:null;
- }
- /**
- 获取config
- */
- public function getConfig(){
- return $this->weixinConfig;
- }
- /**
- 检验 token
- */
- public function validToken(){
- if(empty($this->token)){ //如果 不存在 token 就抛出异常
- return false;
- }else{
- if($this->checkSignature()){//检查签名,签名通过之后,就需要处理用户请求的数据
- return true;
- }else{
- return false;
- }
- }
- }
- /**
- 检查签名
- */
- private function checkSignature(){
- try{ # try{.....}catch{.....} 捕捉语句异常
- $signature = isset($_GET["signature"])?$_GET["signature"]:null;//判断腾讯微信返回的参数 是否存在
- $timestamp = isset($_GET["timestamp"])?$_GET["timestamp"]:null;//如果存在 就返回 否则 就 返回 null
- $nonce = isset($_GET["nonce"])?$_GET["nonce"]:null;
- ######下面的代码是--微信官方提供代码
- $tmpArr = array($this->token, $timestamp, $nonce);
- sort($tmpArr, SORT_STRING);
- $tmpStr = implode( $tmpArr );
- $tmpStr = sha1( $tmpStr );
- if( $tmpStr == $signature ){
- return true;
- }else{
- return false;
- }
- ######上面的代码是--微信官方提供代码
- }catch(Exception $e){
- echo $e->getMessage();
- exit();
- }
- }
- /**
- 处理用户的请求
- */
- private function handleUserRequest(){
- if(isset($_GET['echostr'])){ //腾讯微信官方返回的字符串 如果是存在 echostr 变量 就表明 是微信的返回 我们直接输出就可以了
- $echoStr = $_GET["echostr"];
- echo $echoStr;
- exit;
- }else{//否则 就是用户自己回复 的
- $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];//用户所有的回复,腾讯微信都是放在这个变量的
- if (!empty($postStr)){
- libxml_disable_entity_loader(true); //由于微信返回的数据 都是以xml 的格式,所以需要将xml 格式数据转换成 对象操作
- $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
- $this->fromUserName=$postObj->FromUserName; //得到发送者 姓名 一般为微信人的账号
- $this->toUserName=$postObj->ToUserName;//得到 接受者的 姓名 获取请求中的收信人OpenId,一般为公众账号自身
- $this->msgType=trim($postObj->MsgType); //得到 用户发的数据的类型
- $this->keyword=addslashes(trim($postObj->Content));//得到 发送者 发送的内容
- $this->createTime=date('Y-m-d H:i:s',$_SERVER['REQUEST_TIME']);//当前的时间,我们这里是服务器的时间
- $this->requestId=$postObj->MsgId;//MsgId 获取接收消息编号
- $this->userPostData=$postObj;
- //$this->responseMessage('text','返回:'.$this->msgType);
- }
- }
- }
- /**
- 获取用户的数据对象集
- */
- public function getUserPostData(){
- return $this->userPostData;
- }
- /**
- 检查类型 方法
- 依据不同的数据类型调用不同的模板
- 判断一下 微信反馈回来的数据类型 是否存在于 wx_msgType 数组中
- */
- private function isWeixinMsgType(){
- if(in_array($this->msgType,$this->wx_msgType)){
- return true;
- }else{
- return false;
- }
- }
- /**
- 文本会话
- */
- private function textMessage($callData){
- if(is_null($callData)){
- return 'null';
- }
- $textTpl = "<xml>
- <ToUserName><![CDATA[%s]]></ToUserName>
- <FromUserName><![CDATA[%s]]></FromUserName>
- <CreateTime>%s</CreateTime>
- <MsgType><![CDATA[%s]]></MsgType>
- <Content><![CDATA[%s]]></Content>
- <FuncFlag>5</FuncFlag>
- </xml>";
- if(is_string($callData)){
- $resultStr = sprintf($textTpl,$this->fromUserName,$this->toUserName,$this->createTime,'text',$callData);
- }else if(is_array($callData)){
- $content='';
- foreach($callData as $key => $value){
- $content.=$value;
- }
- $resultStr= sprintf($textTpl,$this->fromUserName,$this->toUserName,$this->createTime,'text',$content);
- }
- return $resultStr;
- }
- /**
- 图片会话
- */
- private function imageMessage($callData){
- if(is_null($callData)){
- return 'null';
- }
- $textTpl = "<xml>
- <ToUserName><![CDATA[%s]]></ToUserName>
- <FromUserName><![CDATA[%s]]></FromUserName>
- <CreateTime>%s</CreateTime>
- <MsgType><![CDATA[%s]]></MsgType>
- <Image>
- <MediaId><![CDATA[%s]]></MediaId>
- </Image>
- </xml>";
- $resultStr = sprintf($textTpl,$this->fromUserName,$this->toUserName,$this->createTime,'image', $callData);
- return $resultStr;
- }
- /**
- 语音会话
- */
- private function voiceMessage($callData){
- if(is_null($callData)){
- return 'null';
- }
- $textTpl = "<xml>
- <ToUserName><![CDATA[%s]]></ToUserName>
- <FromUserName><![CDATA[%s]]></FromUserName>
- <CreateTime>%s</CreateTime>
- <MsgType><![CDATA[%s]]></MsgType>
- <Voice>
- <MediaId><![CDATA[%s]]></MediaId>
- </Voice>
- </xml>";
- $resultStr = sprintf($textTpl,$this->fromUserName,$this->toUserName,$this->createTime,'voice',$callData['MediaId']);
- return $resultStr;
- }
- /**
- 视频会话
- */
- private function videoMessage($callData){
- if(is_null($callData)){
- return 'null';
- }
- $textTpl = "<xml>
- <ToUserName><![CDATA[%s]]></ToUserName>
- <FromUserName><![CDATA[%s]]></FromUserName>
- <CreateTime>%s</CreateTime>
- <MsgType><![CDATA[%s]]></MsgType>
- <Video>
- <MediaId><![CDATA[%s]]></MediaId>
- <ThumbMediaId><![CDATA[%s]]></ThumbMediaId>
- <Title><![CDATA[%s]]></Title>
- <Description><![CDATA[%s]]></Description>
- </Video>
- </xml>";
- $resultStr = sprintf($textTpl,$this->fromUserName,$this->toUserName,$this->createTime,'video',$callData['MediaId'],$callData['ThumbMediaId'],$callData['Title'],$callData['Description']);
- return $resultStr;
- }
- /**
- 音乐会话
- */
- private function musicMessage($callData){ //依据文本 直接调用
- if(is_null($callData)){
- return 'null';
- }
- $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>';
- $resultStr = sprintf($textTpl,$this->fromUserName,$this->toUserName,$this->createTime,'music',$callData['Title'],$callData['Description'],$callData['MusicUrl'],$callData['HQMusicUrl']);
- return $resultStr;
- }
- /**
- 回复图文消息
- $items 必须是数组 必须是二维数组
- $items=array(
- array('Title'=>'','Description'=>'','PicUrl'=>'','Url'=>'')
- )
- */
- private function newsMessage($items){
- if(is_null($items)){
- return 'null';
- }
- //##公共部分 图文公共部分
- $textTpl = '<xml>
- <ToUserName><![CDATA[%s]]></ToUserName>
- <FromUserName><![CDATA[%s]]></FromUserName>
- <CreateTime>%s</CreateTime>
- <MsgType><![CDATA[%s]]></MsgType>
- <ArticleCount>%d</ArticleCount>
- <Articles>%s</Articles>
- </xml>';
- //##新闻列表部分模板
- $itemTpl = '<item>
- <Title><![CDATA[%s]]></Title>
- <Description><![CDATA[%s]]></Description>
- <PicUrl><![CDATA[%s]]></PicUrl>
- <Url><![CDATA[%s]]></Url>
- </item>';
- $articles = '';
- $count=0;
- if(is_array($items)){
- $level=$this->arrayLevel($items);//判断数组的维度
- if($level == 1){ //是一维数组的情况下
- $articles= sprintf($itemTpl, $items['Title'], $items['Description'], $items['PicUrl'], $items['Url']);
- $count=1;
- }else{
- foreach($items as $key=>$item){
- if(is_array($item)){
- $articles.= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']);
- }
- }
- }
- $count=count($items);
- }
- $resultStr = sprintf($textTpl,$this->fromUserName,$this->toUserName,$this->createTime,'news',$count, $articles);
- return $resultStr;
- }
- /**
- debug调试
- */
- public function debug($data){
- echo '<pre>';
- print_r($data);
- echo '</pre>';
- }
- /**
- 得到数组的维度
- */
- private function arrayLevel($vDim){
- if(!is_array($vDim)){
- return 0;
- }else{
- $max1 = 0;
- foreach($vDim as $item1){
- $t1 = $this->arrayLevel($item1);
- if( $t1 > $max1) {
- $max1 = $t1;
- }
- }
- return $max1 + 1;
- }
- }
- /**
- 订阅号需要初始化
- */
- public function weixinBaseApiMessage($args=array()){
- $this->setConfig($args);
- //检查配置文件
- if(empty($this->weixinConfig)){
- return false;
- }
- $this->handleUserRequest(); //处理用户 请求
- return true;
- }
- public function weixinHighApiMessage($args=array()){
- $this->setConfig($args);
- //检查配置文件
- if(empty($this->weixinConfig)){
- return false;
- }
- return true;
- }
- /**
- 通过同的类型调用不同的微信模板
- 回复微信内容信息
- $wxmsgType 参数是 数据类型 微信规定的类型
- $callData 参数是 数据库查询出来的数据或者指定数据
- 小机器人 被动回复
- */
- public function responseMessage($wxmsgType,$callData=''){
- // if($this->isWeixinMsgType()){
- $method=$wxmsgType.'Message';//类型方法组装
- $CallResultData=$this->$method($callData);//把程序的数据传递给模板,并返回数据格式
- if (!headers_sent()){//判断是否有发送过头信息,如果没有就发送,并输出内容
- header('Content-Type: application/xml; charset=utf-8');
- echo $CallResultData;
- exit;
- }
- //}
- }
- /**
- 事件消息内容类型
- */
- public function responseEventMessage($message=''){
- $content = "";
- $event=$this->userPostData->Event;
- if($event == 'subscribe'){
- return $content = $message;
- }elseif($event == 'unsubscribe'){
- return $content = "取消关注";
- }elseif($event == 'scan' || $event=='SCAN'){
- return $this->getUserEventScanRequest();
- }elseif($event == 'click' || $event == 'CLICK'){
- switch ($this->userPostData->EventKey)
- {
- case "company":
- $content =$message.'为你提供服务!';
- break;
- default:
- $content =$this->getUsertEventClickRequest();//返回点击的字符串
- break;
- }
- return $content;
- }elseif($event == 'location' || $event=='LOCATION'){
- return $this->getUserLocationRequest();//本地地理位置分享后 返回x 、y坐标,并返回经度和维度
- }elseif($event == 'view' || $event == 'VIEW'){
- return $this->userPostData->EventKey; //返回跳转的链接
- }elseif($event == 'masssendjobfinish' || $event == 'MASSSENDJOBFINISH'){
- return $this->getUserMessageInfo();//返回会话的所有信息
- }else{
- return "receive a new event: ".$$this->userPostData->Event;
- }
- return false;
- }
- /**
- 获取微信端 返回的数据类型
- */
- public function getUserMsgType(){
- return strval($this->msgType);
- }
- /**
- 获取用户发送信息的时间
- */
- public function getUserSendTime(){
- return $this->createTime;
- }
- /**
- 获取用户的微信id
- */
- public function getUserWxId(){
- return strval($this->fromUserName);
- }
- /**
- 获取到平台的微信id
- */
- public function getPlatformId(){
- return strval($this->toUserName);
- }
- /**
- 获取用户在客户端返回的数据,文本数据
- */
- public function getUserTextRequest(){
- return empty($this->keyword)?null:strval($this->keyword);
- }
- /**
- 获取接收消息编号,微信平台接收的第几条信息
- */
- public function getUserRequestId(){
- return strval($this->requestId);
- }
- /**
- 获取图片信息的内容
- */
- public function getUserImageRequest(){
- $image = array();
- $image['PicUrl'] = strval($this->userPostData->PicUrl);//图片url地址
- $image['MediaId'] = strval($this->userPostData->MediaId);//图片在微信公众平台下的id号
- return $image;
- }
- /**
- 获取语音信息的内容
- */
- public function getUserVoiceRequest(){
- $voice = array();
- $voice['MediaId'] = $this->userPostData->MediaId;//语音ID
- $voice['Format'] = $this->userPostData->Format;//语音格式
- $voice['MsgId']=$this->userPostData->MsgId;//id
- if (isset($this->userPostData->Recognition) && !empty($this->userPostData->Recognition)){
- $voice['Recognition'] = $this->userPostData->Recognition;//语音的内容;;你刚才说的是: xxxxxxxx
- }
- return $voice;
- }
- /**
- 获取视频信息的内容
- */
- public function getUserVideoRequest(){
- $video = array();
- $video['MediaId'] =$this->userPostData->MediaId;
- $video['ThumbMediaId'] = $this->userPostData->ThumbMediaId;
- return $video;
- }
- /**
- 获取音乐消息内容
- */
- public function getUserMusicRequest(){
- $music=array();
- $music['Title'] =$this->userPostData->Title;//标题
- $music['Description']=$this->userPostData->Description;//简介
- $music['MusicUrl']=$this->userPostData->MusicUrl;//音乐地址
- $music['HQMusicUrl']=$this->userPostData->HQMusicUrl;//高品质音乐地址
- return $music;
- }
- /**
- 获取本地地理位置信息内容
- */
- public function getUserLocationRequest(){
- $location = array();
- $location['Location_X'] = strval($this->userPostData->Location_X);//本地地理位置 x坐标
- $location['Location_Y'] = strval($this->userPostData->Location_Y);//本地地理位置 Y 坐标
- $location['Scale'] = strval($this->userPostData->Scale);//缩放级别为
- $location['Label'] = strval($this->userPostData->Label);//位置为
- $location['Latitude']=$this->userPostData->Latitude;//维度
- $location['Longitude']=$this->userPostData->Longitude;//经度
- return $location;
- }
- /**
- 获取链接信息的内容
- */
- public function getUserLinkRequest(){ //数据以文本方式返回 在程序调用端 调用 text格式输出
- $link = array();
- $link['Title'] = strval($this->userPostData->Title);//标题
- $link['Description'] = strval($this->userPostData->Description);//简介
- $link['Url'] = strval($this->userPostData->Url);//链接地址
- return $link;
- }
- /**
- 二维码扫描事件内容
- */
- public function getUserEventScanRequest(){
- $info = array();
- $info['EventKey'] = $this->userPostData->EventKey;
- $info['Ticket'] = $this->userPostData->Ticket;
- $info['Scene_Id'] = str_replace('qrscene_', '', $this->userPostData->EventKey);
- return $info;
- }
- /**
- 上报地理位置事件内容
- */
- public function getUserEventLocationRequest(){
- $location = array();
- $location['Latitude'] = $this->userPostData->Latitude;
- $location['Longitude'] =$this->userPostData->Longitude;
- return $location;
- }
- /**
- 获取菜单点击事件内容
- */
- public function getUsertEventClickRequest(){
- return strval($this->userPostData->EventKey);
- }
- /**
- 获取微信会话状态info
- */
- public function getUserMessageInfo(){
- $info=array();
- $info['MsgID']=$this->userPostData->MsgID;//消息id
- $info['Status']=$this->userPostData->Status;//消息结果状态
- $info['TotalCount']=$this->userPostData->TotalCount;//平台的粉丝数量
- $info['FilterCount']=$this->userPostData->FilterCount;//过滤
- $info['SentCount']=$this->userPostData->SentCount;//发送成功信息
- $info['ErrorCount']=$this->userPostData->ErrorCount;//发送错误信息
- return $info;
- }
- /**
- 向第三方请求数据,并返回结果
- */
- public function relayPart3($url, $rawData){
- $headers = array("Content-Type: text/xml; charset=utf-8");
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $rawData);
- $output = curl_exec($ch);
- curl_close($ch);
- return $output;
- }
- /**
- 字节转Emoji表情
- "中国:".$this->bytes_to_emoji(0x1F1E8).$this->bytes_to_emoji(0x1F1F3)."\n仙人掌:".$this->bytes_to_emoji(0x1F335);
- */
- public function bytes_to_emoji($cp){
- if ($cp > 0x10000){ # 4 bytes
- return chr(0xF0 | (($cp & 0x1C0000) >> 18)).chr(0x80 | (($cp & 0x3F000) >> 12)).chr(0x80 | (($cp & 0xFC0) >> 6)).chr(0x80 | ($cp & 0x3F));
- }else if ($cp > 0x800){ # 3 bytes
- return chr(0xE0 | (($cp & 0xF000) >> 12)).chr(0x80 | (($cp & 0xFC0) >> 6)).chr(0x80 | ($cp & 0x3F));
- }else if ($cp > 0x80){ # 2 bytes
- return chr(0xC0 | (($cp & 0x7C0) >> 6)).chr(0x80 | ($cp & 0x3F));
- }else{ # 1 byte
- return chr($cp);
- }
- }
- #############################################################高级接口################################
- /**
- 微信api 接口地址
- */
- private $weixinApiLinks = array(
- 'message' => "https://api.weixin.qq.com/cgi-bin/message/custom/send?",##发送客服消息
- 'group_create' => "https://api.weixin.qq.com/cgi-bin/groups/create?",##创建分组
- 'group_get' => "https://api.weixin.qq.com/cgi-bin/groups/get?",##查询分组
- 'group_getid' => "https://api.weixin.qq.com/cgi-bin/groups/getid?",##查询某个用户在某个分组里面
- 'group_rename' => "https://api.weixin.qq.com/cgi-bin/groups/update?",##修改分组名
- 'group_move' => "https://api.weixin.qq.com/cgi-bin/groups/members/update?",## 移动用户分组
- 'user_info' => "https://api.weixin.qq.com/cgi-bin/user/info?",###获取用户基本信息
- 'user_get' => 'https://api.weixin.qq.com/cgi-bin/user/get?',##获取关注者列表
- 'menu_create' => 'https://api.weixin.qq.com/cgi-bin/menu/create?',##自定义菜单创建
- 'menu_get' => 'https://api.weixin.qq.com/cgi-bin/menu/get?',##自定义菜单查询
- 'menu_delete' => 'https://api.weixin.qq.com/cgi-bin/menu/delete?',##自定义菜单删除
- 'qrcode' => 'https://api.weixin.qq.com/cgi-bin/qrcode/create?',##创建二维码ticket
- 'showqrcode' => 'https://mp.weixin.qq.com/cgi-bin/showqrcode?',##通过ticket换取二维码
- 'media_download' => 'http://file.api.weixin.qq.com/cgi-bin/media/get?',
- 'media_upload' => 'http://file.api.weixin.qq.com/cgi-bin/media/upload?',##上传媒体接口
- 'oauth_code' => 'https://open.weixin.qq.com/connect/oauth2/authorize?',##微信oauth登陆获取code
- 'oauth_access_token' => 'https://api.weixin.qq.com/sns/oauth2/access_token?',##微信oauth登陆通过code换取网页授权access_token
- 'oauth_refresh' => 'https://api.weixin.qq.com/sns/oauth2/refresh_token?',##微信oauth登陆刷新access_token(如果需要)
- 'oauth_userinfo' => 'https://api.weixin.qq.com/sns/userinfo?',##微信oauth登陆拉取用户信息(需scope为 snsapi_userinfo)
- 'api_prefix'=>'https://api.weixin.qq.com/cgi-bin?',##请求api前缀
- 'message_template'=>'https://api.weixin.qq.com/cgi-bin/message/template/send?',##模板发送消息接口
- 'message_mass'=>'https://api.weixin.qq.com/cgi-bin/message/mass/send?',##群发消息
- 'upload_news'=>'https://api.weixin.qq.com/cgi-bin/media/uploadnews?',##上传图片素材
- );
- /**
- curl 数据提交
- */
- public function curl_post_https($url='', $postdata='',$options=FALSE){
- $curl = curl_init();// 启动一个CURL会话
- curl_setopt($curl, CURLOPT_URL, $url);//要访问的地址
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);//对认证证书来源的检查
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);//从证书中检查SSL加密算法是否存在
- curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);//模拟用户使用的浏览器
- curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);//使用自动跳转
- curl_setopt($curl, CURLOPT_AUTOREFERER, 1);//自动设置Referer
- if(!empty($postdata)){
- curl_setopt($curl, CURLOPT_POST, 1);//发送一个常规的Post请求
- if(is_array($postdata)){
- curl_setopt($curl, CURLOPT_POSTFIELDS,json_encode($postdata,JSON_UNESCAPED_UNICODE));//Post提交的数据包
- }else{
- curl_setopt($curl, CURLOPT_POSTFIELDS,$postdata);//Post提交的数据包
- }
- }
- //curl_setopt($curl, CURLOPT_COOKIEFILE, './cookie.txt'); //读取上面所储存的Cookie信息
- // curl_setopt($curl, CURLOPT_TIMEOUT, 30);//设置超时限制防止死循环
- curl_setopt($curl, CURLOPT_HEADER, $options);//显示返回的Header区域内容 可以是这样的字符串 "Content-Type: text/xml; charset=utf-8"
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//获取的信息以文件流的形式返回
- $output = curl_exec($curl);//执行操作
- if(curl_errno($curl)){
- if($this->debug == true){
- $errorInfo='Errno'.curl_error($curl);
- $this->responseMessage('text',$errorInfo);//将错误返回给微信端
- }
- }
- curl_close($curl);//关键CURL会话
- return $output;//返回数据
- }
- /**
- 本地缓存token
- */
- private function setFileCacheToken($cacheId,$data,$savePath='/'){
- $cache=array();
- $cache[$cacheId]=$data;
- $this->saveFilePath=$_SERVER['DOCUMENT_ROOT'].$savePath.'token.cache';
- file_exists($this->saveFilePath)?chmod($this->saveFilePath,0775):chmod($this->saveFilePath,0775);//给文件覆权限
- file_put_contents($this->saveFilePath,serialize($cache));
- }
- /**
- 本地读取缓存
- */
- private function getFileCacheToken($cacheId){
- $fileDataInfo=file_get_contents($_SERVER['DOCUMENT_ROOT'].'/token.cache');
- $token=unserialize($fileDataInfo);
- if(isset($token[$cacheId])){
- return $token[$cacheId];
- }
- }
- /**
- 检查高级接口权限 tokenc
- */
- public function checkAccessToken(){
- if($this->appid && $this->appSecret){
- $access=$this->getFileCacheToken('access');
- if(isset($access['expires_in'])){
- $this->expires_in= $access['expires_in'];
- }
- if( ( $this->expires_in - time() ) < 0 ){//表明已经过期
- $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->appid}&secret={$this->appSecret}";
- $access = json_decode($this->curl_post_https($url));
- if(isset($access->access_token) && isset($access->expires_in)){
- $this->access_token = $access->access_token;##得到微信平台返回得到token
- $this->expires_in=time()+$access->expires_in;##得到微信平台返回的过期时间
- $this->setFileCacheToken('access',array('token'=>$this->access_token,'expires_in'=>$this->expires_in));##加入缓存access_token
- return true;
- }
- }else{
- $access=$this->getFileCacheToken('access');
- $this->access_token=$access['token'];
- return true;
- }
- }
- return false;
- }
- /**
- 获取access_token
- */
- public function getAccessToken(){
- return strval($this->access_token);
- }
- /**
- 得到时间
- */
- public function getExpiresTime(){
- return $this->expires_in;
- }
- /**
- 获取用户列表
- $next_openid 表示从第几个开始,如果为空 默认从第一个用户开始拉取
- */
- public function getUserList($next_openid=null){
- $url=$this->weixinApiLinks['user_get']."access_token={$this->access_token}&next_openid={$next_openid}";
- $resultData=$this->curl_post_https($url);
- return json_decode($resultData,true);
- }
- /**
- 获取用户的详细信息
- */
- public function getUserInfo($openid){
- $url=$this->weixinApiLinks['user_info']."access_token=".$this->access_token."&openid=".$openid."&lang=zh_CN";
- $resultData=$this->curl_post_https($url);
- return json_decode($resultData,true);
- }
- /**
- 创建用户分组
- */
- public function createUsersGroup($groupName){
- $data = '{"group": {"name": "'.$groupName.'"}}';
- $url=$this->weixinApiLinks['group_create']."access_token=".$this->access_token;
- $resultData=$this->curl_post_https($url,$data);
- return json_decode($resultData,true);
- }
- /**
- 移动用户分组
- */
- public function moveUserGroup($toGroupid,$openid){
- $data = '{"openid":"'.$openid.'","to_groupid":'.$toGroupid.'}';
- $url=$this->weixinApiLinks['group_move']."access_token=".$this->access_token;
- $resultData=$this->curl_post_https($url,$data);
- return json_decode($resultData,true);
- }
- /**
- 查询所有分组
- */
- public function getUsersGroups(){
- $url=$this->weixinApiLinks['group_get']."access_token=".$this->access_token;
- $resultData=$this->curl_post_https($url);
- return json_decode($resultData,true);
- }
- /**
- 查询用户所在分组
- */
- public function getUserGroup($openid){
- $data='{"openid":"'.$openid.'"}';
- $url=$this->weixinApiLinks['group_getid']."access_token=".$this->access_token;
- $resultData=$this->curl_post_https($url,$data);
- return json_decode($resultData,true);
- }
- /**
- 修改分组名
- */
- public function updateUserGroup($groupId,$groupName){
- $data='{"group":{"id":'.$groupId.',"name":"'.$groupName.'"}}';
- $url=$this->weixinApiLinks['group_rename']."access_token=".$this->access_token;
- $resultData=$this->curl_post_https($url,$data);
- return json_decode($resultData,true);
- }
- /**
- 生成二维码
- */
- public function createQrcode($scene_id=0,$qrcodeType=1,$expire_seconds=1800){
- $scene_id=($scene_id == 0)?rand(1,9999):$scene_id;
- if($qrcodeType == 1){
- $action_name='QR_SCENE';##表示临时二维码
- $data='{"expire_seconds":'.$expire_seconds.',"action_name": "QR_SCENE","action_info":{"scene":{"scene_id": '.$scene_id.'}}}';
- }else{
- $action_name='QR_LIMIT_SCENE';
- $data='{"action_name": "QR_LIMIT_SCENE", "action_info":{"scene":{"scene_id": '.$scene_id.'}}}';
- }
- $url=$this->weixinApiLinks['qrcode']."access_token=".$this->access_token;
- $resultData=$this->curl_post_https($url,$data);
- $result=json_decode($resultData,true);
- return $this->weixinApiLinks['showqrcode']."ticket=".urlencode($result["ticket"]);
- }
- /**
- 上传多媒体文件
- type 分别有图片(image)、语音(voice)、视频(video)和缩略图(thumb)
- */
- public function uploadMedia($type, $file){
- $data=array("media" => "@".dirname(__FILE__).'\\'.$file);
- $url=$this->weixinApiLinks['media_upload']."access_token=".$this->access_token."&type=".$type;
- $resultData=$this->curl_post_https($url, $data);
- return json_decode($resultData, true);
- }
- /**
- 创建菜单
- */
- public function createMenu($menuListdata){
- $url =$this->weixinApiLinks['menu_create']."access_token=".$this->access_token;
- $resultData = $this->curl_post_https($url, $menuListdata);
- $callData=json_decode($resultData, true);
- if($callData['errcode'] > 0){#表示菜单创建失败
- return $callData;
- }
- return true;
- }
- /**
- 查询菜单
- */
- public function queryMenu(){
- $url = $this->weixinApiLinks['menu_get']."access_token=".$this->access_token;
- $resultData = $this->curl_post_https($url);
- return json_decode($resultData, true);
- }
- /**
- 删除菜单
- */
- public function deleteMenu(){
- $url = $this->weixinApiLinks['menu_delete']."access_token=".$this->access_token;
- $resultData = $this->curl_post_https($url);
- return json_decode($resultData, true);
- }
- /**
- 给某个人发送文本内容
- */
- public function sendMessage($touser, $data, $msgType = 'text'){
- $message = array();
- $message['touser'] = $touser;
- $message['msgtype'] = $msgType;
- switch ($msgType){
- case 'text': $message['text']['content']=$data; break;
- case 'image': $message['image']['media_id']=$data; break;
- case 'voice': $message['voice']['media_id']=$data; break;
- case 'video':
- $message['video']['media_id']=$data['media_id'];
- $message['video']['thumb_media_id']=$data['thumb_media_id'];
- break;
- case 'music':
- $message['music']['title'] = $data['title'];// 音乐标题
- $message['music']['description'] = $data['description'];// 音乐描述
- $message['music']['musicurl'] = $data['musicurl'];// 音乐链接
- $message['music']['hqmusicurl'] = $data['hqmusicurl'];// 高品质音乐链接,wifi环境优先使用该链接播放音乐
- $message['music']['thumb_media_id'] = $data['title'];// 缩略图的媒体ID
- break;
- case 'news':
- $message['news']['articles'] = $data; // title、description、url、picurl
- break;
- }
- $url=$this->weixinApiLinks['message']."access_token={$this->access_token}";
- $calldata=json_decode($this->curl_post_https($url,$message),true);
- if(!$calldata || $calldata['errcode'] > 0){
- return false;
- }
- return true;
- }
- /**
- * 群发
- * */
- public function sendMassMessage($sendType,$touser=array(),$data){
- $massArrayData=array();
- switch($sendType){
- case 'text':##文本
- $massArrayData=array(
- "touser"=>$touser,
- "msgtype"=>'text',
- "text"=>array('content'=>$data),
- );
- break;
- case 'news':##图文
- $massArrayData=array(
- "touser"=>$touser,
- "msgtype"=>'mpnews',
- "mpnews"=>array('media_id'=>$data),
- );
- break;
- case 'voice':##语音
- $massArrayData=array(
- "touser"=>$touser,
- "msgtype"=>'voice',
- "voice"=>array('media_id'=>$data),
- );
- break;
- case 'image':##图片
- $massArrayData=array(
- "touser"=>$touser,
- "msgtype"=>'image',
- "media_id"=>array('media_id'=>$data),
- );
- break;
- case 'wxcard': ##卡卷
- $massArrayData=array(
- "touser"=>$touser,
- "msgtype"=>'wxcard',
- "wxcard"=>array('card_id'=>$data),
- );
- break;
- }
- $url=$this->weixinApiLinks['message_mass']."access_token={$this->access_token}";
- $calldata=json_decode($this->curl_post_https($url,$massArrayData),true);
- return $calldata;
- }
- /**
- 发送模板消息
- */
- public function sendTemplateMessage($touser,$template_id,$url,$topColor,$data){
- $templateData=array(
- 'touser'=>$touser,
- 'template_id'=>$template_id,
- 'url'=>$url,
- 'topcolor'=>$topColor,
- 'data'=>$data,
- );
- $url=$this->weixinApiLinks['message_template']."access_token={$this->access_token}";
- $calldata=json_decode($this->curl_post_https($url,urldecode(json_encode($templateData))),true);
- return $calldata;
- }
- /**
- * @param $type
- * @param $filePath 文件根路径
- */
- public function mediaUpload($type,$filePath){
- $url=$this->weixinApiLinks['media_upload']."access_token={$this->access_token}&type=".$type;
- $postData=array('media'=>'@'.$filePath);
- $calldata=json_decode($this->https_request($url,$postData),true);
- return $calldata;
- }
- /**
- * @param $data
- * @return mixed
- * 上传图片资源
- */
- public function newsUpload($data){
- $articles=array( 'articles'=>$data );
- $url=$this->weixinApiLinks['upload_news']."access_token={$this->access_token}";
- $calldata=json_decode($this->curl_post_https($url,$articles),true);
- return $calldata;
- }
- /**
- * 获取微信授权链接
- *
- * @param string $redirect_uri 跳转地址
- * @param mixed $state 参数
- */
- public function getOauthorizeUrl($redirect_uri = '', $state = '',$scope='snsapi_userinfo'){
- $redirect_uri = urlencode($redirect_uri);
- $state=empty($state)?'1':$state;
- $url=$this->weixinApiLinks['oauth_code']."appid={$this->appid}&redirect_uri={$redirect_uri}&response_type=code&scope={$scope}&state={$state}#wechat_redirect";
- return $url;
- }
- /**
- * 获取授权token
- *
- * @param string $code 通过get_authorize_url获取到的code
- */
- public function getOauthAccessToken(){
- $code = isset($_GET['code'])?$_GET['code']:'';
- if (!$code) return false;
- $url=$this->weixinApiLinks['oauth_access_token']."appid={$this->appid}&secret={$this->appSecret}&code={$code}&grant_type=authorization_code";
- $token_data=json_decode($this->curl_post_https($url),true);
- $this->oauthAccessToken=$token_data['access_token'];
- return $token_data;
- }
- /**
- * 刷新access token并续期
- */
- public function getOauthRefreshToken($refresh_token){
- $url=$this->weixinApiLinks['oauth_refresh']."appid={$this->appid}&grant_type=refresh_token&refresh_token={$refresh_token}";
- $token_data=json_decode($this->curl_post_https($url),true);
- $this->oauthAccessToken=$token_data['access_token'];
- return $token_data;
- }
- /**
- * 获取授权后的微信用户信息
- *
- * @param string $access_token
- * @param string $open_id 用户id
- */
- public function getOauthUserInfo($access_token='', $open_id = ''){
- $url=$this->weixinApiLinks['oauth_userinfo']."access_token={$access_token}&openid={$open_id}&lang=zh_CN";
- $info_data=json_decode($this->curl_post_https($url),true);
- return $info_data;
- }
- /**
- * 登出当前登陆用户
- */
- public function logout($openid='',$uid=''){
- $url = 'https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxlogout?redirect=1&type=1';
- $data=array('uin'=>$uid,'sid'=>$openid);
- $this->curl_post_https($url,$data);
- return true;
- }
- public function https_request($url, $data = null){
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
- if (!empty($data)){
- curl_setopt($curl, CURLOPT_POST, 1);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
- }
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
- $output = curl_exec($curl);
- curl_close($curl);
- return $output;
- }
- }
- 告知义务:
- 1. api提供订阅号基本服务以及公众号常用服务;
- 2. api如不能满足您的需求,请阅读微信公众平台说明文档
- 使用说明:
- header('Content-type: text/html; charset=utf-8');#设置头信息
- require_once('zhphpWeixinApi.class.php');#加载微信接口类文件
- $zhwx=new zhphpWeixinApi();//实例化
- 配置:
- 第一种方式:
- $zhwx->weixinConfig=array(
- 'token'=>'weixintest',
- 'appid'=>'wx7b4b6ad5c7bfaae1',
- 'appSecret'=>'faaa6a1e840fa40527934a293fabfbd1',
- 'myweixinId'=>'gh_746ed5c6a58b'
- );
- 第二种方式:
- $configArr=array(
- 'token'=>'weixintest',
- 'appid'=>'wx7b4b6ad5c7bfaae1',
- 'appSecret'=>'faaa6a1e840fa40527934a293fabfbd1',
- 'myweixinId'=>'gh_746ed5c6a58b'
- );
- $zhwx->setConfig($configArr);
- 第三种方式:
- $zhwx->weixinBaseApiMessage($configArr); ##订阅号使用
- $zhwx->weixinHighApiMessage($configArr);## 服务号使用
- 官方建议使用 第二种方式
- 订阅号基本功能调用
- $zhwx->weixinBaseApiMessage(); #订阅号接口入口 返回 true false
- 下面是调用案例
- if($zhwx->weixinBaseApiMessage()){ //基本微信接口会话 必须
- $keyword=$zhwx->getUserTextRequest();//得到用户发的微信内容, 这里可以写你的业务逻辑
- $msgtype=$zhwx->getUserMsgType();//得到用户发的微信数据类型, 这里可以写你的业务逻辑
- $SendTime=$zhwx->getUserSendTime();//得到用户发送的时间
- $fromUserName=$zhwx->getUserWxId();//用户微信id
- $pingtaiId=$zhwx->getPlatformId();//平台微信id
- $requestId=$zhwx->getUserRequestId(); //获取微信公众平台消息ID
- $userPostData=$zhwx->getUserPostData();//获取用户提交的post 数据对象集
- if( $msgtype == 'text' ){ //如果是文本类型
- if($keyword == 'news'){ //新闻类别请求
- $callData=array(
- array('Title'=>'第一条新闻的标题','Description'=>'第一条新闻的简介','PicUrl'=>'http://t12.baidu.com/it/u=1040955509,77044968&fm=76','Url'=>'http://wxphptest1.applinzi.com/'),
- array('Title'=>'第二条新闻的标题','Description'=>'第一条新闻的简介','PicUrl'=>'https://images0.cnblogs.com/blog2015/340216/201505/051316468603876.png','Url'=>'http://wxphptest1.applinzi.com/'),
- array('Title'=>'第三条新闻的标题','Description'=>'第一条新闻的简介','PicUrl'=>'http://dimgcn2.s-msn.com/imageadapter/w60h60q85/stimgcn3.s-msn.com/msnportal/hp/2016/01/21/3e3ef7f07361b5211e2b155bb66fa6a9.jpg','Url'=>'http://wxphptest1.applinzi.com/'),
- );
- $zhwx->responseMessage('news',$callData);
- }elseif($keyword == 'music'){ //音乐请求
- $music=array(
- "Title"=>"最炫民族风",
- "Description"=>"歌手:凤凰传奇",
- "MusicUrl"=>"http://121.199.4.61/music/zxmzf.mp3",
- "HQMusicUrl"=>"http://121.199.4.61/music/zxmzf.mp3"
- );
- $zhwx->responseMessage('music',$music);
- }else{
- $callData='这是zhphp 官方对你的回复:
- 你输入的内容是'.$keyword.'
- 你发数据类型是:'.$msgtype.'
- 你发的数据时间是:'.$SendTime.'
- 你的微信ID是:'.$fromUserName.'
- 平台的微信Id是:'.$pingtaiId.'
- zhphp官方接收的ID是:'.$requestId;
- $zhwx->responseMessage('text',$callData);
- }
- }else if($msgtype == 'image'){ //如果是图片类型
- $image=$zhwx->getUserImageRequest();//如果用户发的是图片信息,就去得到用户的图形 信息
- $callData=$image['MediaId'];//mediaID 是微信公众平台返回的图片的id,微信公众平台是依据该ID来调用用户所发的图片
- $zhwx->responseMessage('image',$callData);
- }else if($msgtype == 'voice'){ //如果是语音消息,用于语音交流
- $callData=$zhwx->getUserVoiceRequest(); //返回数组
- $zhwx->responseMessage('voice',$callData);
- }elseif($msgtype == 'event'){//事件,由系统自动检验类型
- $content='感谢你关注zhphp官方平台';//给数据为数据库查询出来的
- $callData=$zhwx->responseEventMessage($content);//把数据放进去,由系统自动检验类型,并返回字符串或者数组的结果
- $zhwx->responseMessage('text',$callData); //使用数据
- }else{
- $callData='这是zhphp 官方对你的回复:
- 你发数据类型是:'.$msgtype.'
- 你发的数据时间是:'.$SendTime.'
- zhphp官方接收的ID是:'.$requestId;
- $zhwx->responseMessage('text',$callData);
- }
- }
- ##########################################
- 订阅号提供基本接口
- validToken() 平台与微信公众号握手
- getAccessToken() 获取accessToken
- getExpiresTime() 获取accessToken 过期时间
- CheckAccessToken() 检查高级接口权限
- bytes_to_emoji($cp) 字节转表情
- getUserMessageInfo() 获取用户的所有会话状态信息
- getUsertEventClickRequest() 获取用户点击菜单信息
- getUserEventLocationRequest() 上报地理位置事件
- getUserEventScanRequest() 二维码扫描类容
- getUserLinkRequest() 获取用户链接信息
- getUserLocationRequest() 获取用户本地位置信息
- getUserMusicRequest() 获取音乐内容
- getUserVideoRequest() 获取视频内容
- getUserVoiceRequest() 获取语音消息
- getUserImageRequest() 获取图片消息
- getUserRequestId() 返回微信平台的当条微信id
- getUserTextRequest() 获取用户输入的信息
- getPlatformId() 获取当前平台的id
- getUserWxId() 获取当前用户的id
- getUserSendTime() 获取用户发送微信的时间
- getUserMsgType() 获取当前的会话类型
- isWeixinMsgType() 检查用户发的微信类型
- getUserPostData() 获取用户发送信息的数据对象集
- getConfig() 获取配置信息
- responseMessage($wxmsgType,$callData='') 微信平台回复用户信息统一入口
- #####################################################
- 服务号基本功能调用
- $zhwx->weixinHighApiMessage(); #服务号接口入口 返回 true false
- $zhwx->CheckAccessToken(); #检查access权限
- 下面是调用案例
- if($zhwx->weixinHighApiMessage()){// 检查配置统一入口
- if($zhwx->CheckAccessToken()){ //检查accessToken 为必须
- $arrayData=$zhwx->getUserList(); ##测试用户列表
- echo '<pre>';
- rint_r($arrayData);
- echo '</pre>';*/
- }
- }
- ######################################################
- 服务号提供的接口:
- getUserList($next_openid=null) 获取用户列表
- getUserInfo($openid) 获取用户详细信息
- createUsersGroup($groupName) 创建分组
- moveUserGroup($toGroupid,$openid) 移动用户到组
- getUsersGroups() 获取所有分组
- getUserGroup($openid) 获取用户所在的组
- updateUserGroup($groupId,$groupName) 修改组名
- createQrcode($scene_id,$qrcodeType=1,$expire_seconds=1800) 生成二维码
- uploadMedia($type, $file) 上传文件
- createMenu($menuListdata) 创建菜单
- queryMenu() 查询菜单
- deleteMenu() 删除菜单
- sendMessage($touser, $data, $msgType = 'text') 主动某个用户发送数据
- newsUpload($data) 上传图文素材 按接口规定 $data 必须是二维数组
- mediaUpload($type,$filePath) 上传媒体文件 filePath 必须是文件绝对路径
- sendTemplateMessage($touser,$template_id,$url,$topColor,$data) 发送模板消息 通常为通知接口
- sendMassMessage($sendType,$touser=array(),$data) 群发消息
- header('Content-type: text/html; charset=utf-8');#设置头信息
- // 微信类 调用
- require_once('zhphpWeixinApi.class.php');
- echo 'helloword';
- exit;
- $zhwx=new zhphpWeixinApi();//实例化
- $zhwx->weixinConfig=array('token'=>'weixintest','appid'=>'wx7b4b6ad5c7bfaae1','appSecret'=>'faaa6a1e840fa40527934a293fabfbd1','myweixinId'=>'gh_746ed5c6a58b');//参数配置
- /*$configArr=array(
- 'token'=>'weixintest',
- 'appid'=>'wx7b4b6ad5c7bfaae1',
- 'appSecret'=>'faaa6a1e840fa40527934a293fabfbd1',
- 'myweixinId'=>'gh_746ed5c6a58b'
- );
- $zhwx->setConfig($configArr);
- */
- #################################应用程序逻辑# 订阅号测试 start #####################################
- if($zhwx->weixinBaseApiMessage()){ //基本微信接口会话 必须
- $keyword=$zhwx->getUserTextRequest();//得到用户发的微信内容, 这里可以写你的业务逻辑
- $msgtype=$zhwx->getUserMsgType();//得到用户发的微信数据类型, 这里可以写你的业务逻辑
- $SendTime=$zhwx->getUserSendTime();//得到用户发送的时间
- $fromUserName=$zhwx->getUserWxId();//用户微信id
- $pingtaiId=$zhwx->getPlatformId();//平台微信id
- $requestId=$zhwx->getUserRequestId(); //获取微信公众平台消息ID
- $userPostData=$zhwx->getUserPostData();//获取用户提交的post 数据对象集
- if( $msgtype == 'text' ){ //如果是文本类型
- if($keyword == 'news'){ //新闻类别请求
- $callData=array(
- array('Title'=>'第一条新闻的标题','Description'=>'第一条新闻的简介','PicUrl'=>'http://t12.baidu.com/it/u=1040955509,77044968&fm=76','Url'=>'http://wxphptest1.applinzi.com/'),
- array('Title'=>'第二条新闻的标题','Description'=>'第一条新闻的简介','PicUrl'=>'https://images0.cnblogs.com/blog2015/340216/201505/051316468603876.png','Url'=>'http://wxphptest1.applinzi.com/'),
- array('Title'=>'第三条新闻的标题','Description'=>'第一条新闻的简介','PicUrl'=>'http://dimgcn2.s-msn.com/imageadapter/w60h60q85/stimgcn3.s-msn.com/msnportal/hp/2016/01/21/3e3ef7f07361b5211e2b155bb66fa6a9.jpg','Url'=>'http://wxphptest1.applinzi.com/'),
- );
- $zhwx->responseMessage('news',$callData);
- }elseif($keyword == 'music'){ //音乐请求
- $music=array(
- "Title"=>"最炫民族风",
- "Description"=>"歌手:凤凰传奇",
- "MusicUrl"=>"http://121.199.4.61/music/zxmzf.mp3",
- "HQMusicUrl"=>"http://121.199.4.61/music/zxmzf.mp3"
- );
- $zhwx->responseMessage('music',$music);
- }else{
- $callData='这是zhphp 官方对你的回复:
- 你输入的内容是'.$keyword.'
- 你发数据类型是:'.$msgtype.'
- 你发的数据时间是:'.$SendTime.'
- 你的微信ID是:'.$fromUserName.'
- 平台的微信Id是:'.$pingtaiId.'
- zhphp官方接收的ID是:'.$requestId;
- $zhwx->responseMessage('text',$callData);
- }
- }else if($msgtype == 'image'){ //如果是图片类型
- $image=$zhwx->getUserImageRequest();//如果用户发的是图片信息,就去得到用户的图形 信息
- $callData=$image['MediaId'];//mediaID 是微信公众平台返回的图片的id,微信公众平台是依据该ID来调用用户所发的图片
- $zhwx->responseMessage('image',$callData);
- }else if($msgtype == 'voice'){ //如果是语音消息,用于语音交流
- $callData=$zhwx->getUserVoiceRequest(); //返回数组
- $zhwx->responseMessage('voice',$callData);
- }elseif($msgtype == 'video'){//视频请求未测试成功
- $video=$zhwx->getUserVideoRequest();//得到用户视频内容信息
- $callData['Title']='这是是视频的标题';//参数为必须
- $callData['Description']='这是视频的简介';//参数为必须
- $callData['MediaId']=$video['MediaId'];//得到数组中的其中的一个id
- $callData['ThumbMediaId']=$video['ThumbMediaId'];//视频的缩略图id
- $zhwx->responseMessage('video',$callData); //把数据传递到平台接口 api 类 操作并返回数据给微信平台
- }elseif($msgtype == 'link'){
- $link=$zhwx->getUserLinkRequest();
- $callData='这是zhphp 官方对你的回复:
- 你输入的内容是'.$keyword.'
- 你发数据类型是:'.$msgtype.'
- 你发的数据时间是:'.$SendTime.'
- zhphp官方接收的ID是:'.$requestId;
- $zhwx->responseMessage('text',$callData);
- }elseif($msgtype == 'event'){//事件,由系统自动检验类型
- $content='感谢你关注zhphp官方平台';//给数据为数据库查询出来的
- $callData=$zhwx->responseEventMessage($content);//把数据放进去,由系统自动检验类型,并返回字符串或者数组的结果
- $zhwx->responseMessage('text',$callData); //使用数据
- }else{
- $callData='这是zhphp 官方对你的回复:
- 你发数据类型是:'.$msgtype.'
- 你发的数据时间是:'.$SendTime.'
- zhphp官方接收的ID是:'.$requestId;
- $zhwx->responseMessage('text',$callData);
- }
- }
- ###################################服务号测试 start ############
- /*if($zhwx->weixinHighApiMessage()){// 检查配置统一入口
- $zhwx->checkAccessToken();
- //if($zhwx->checkAccessToken()){ //检查accessToken 为必须
- //$arrayData=$zhwx->getUserList(); ##测试用户列表
- /*$openid='oQucos17KaXpwPfwp39FwzfzrfNA'; ###测试获取个人信息
- $arrayData=$zhwx->getUserInfo($openid);
- echo '<pre>';
- print_r($arrayData);
- echo '</pre>';*/
- ###测试二维码
- //$qrcodeImgsrc=$zhwx->createQrcode(100);
- //echo '<img src="'.$qrcodeImgsrc.'">';
- ###创建一个一级菜单 type 点击类型 name 菜单名称 key 菜单说明
- /*$menuListData=array(
- 'button'=>array(
- array('type'=>'click','name'=>'投票活动','key'=>'tphd',),
- array('type'=>'view','name'=>'查看结果','key'=>'ckjg','url'=>''),
- )
- );
- $list=$zhwx->createMenu($menuListData);
- var_dump($list);
- //}
- }*/
- ###############################模板消息################
- if($zhwx->weixinBaseApiMessage()){
- if($msgtype == 'text'){
- $location=urlencode($keyword);
- $url='http://api.map.baidu.com/telematics/v3/weather?location='.$location.'&output=json&ak=C845576f91f52f4e62bf8d9153644cb8';
- $jsonData=$zhwx->curl_post_https($url);
- $data=json_decode($jsonData,true);
- $weth=$data['results'][0];
- /*$content='你查询的是:'.$weth['currentCity'].'的天气情况';
- $content.='pm2.5='.$weth['pm25'];
- $content.='今天的天气是:'.$weth['weather_data'][0]['date'];
- $zhwx->responseMessage('text',$content);*/
- // $userlist=$zhwx->getUserList();
- // $zhwx->responseMessage('text',json_encode($userlist));
- $touser= $fromUserName;
- $tpl_id='eSYzsYE3rCupK8_boCnfMcayi0cUXwPB6W7Lrz4TrdA';
- $url='http://www.baidu.com/';
- $topcolor="#52654D";
- $wdata=array(
- 'first'=>array('value'=>urlencode('欢迎使用天气查询系统'),'color'=>'#12581F'),
- 'city'=>array('value'=>urlencode($weth['currentCity']),'color'=>'#56751F'),
- 'pm'=>array('value'=>urlencode($weth['pm25']),'color'=>'#15751F'),
- );
- $zhwx->checkAccessToken();
- $cc=$zhwx->sendTemplateMessage($touser,$tpl_id,$url,$topcolor,$wdata);
- }
- }
- ###########################################新增群发消息
- <?php
- header('Content-type: text/html; charset=utf-8');#设置头信息
- require_once('zhphpWeixinApi.class.php');
- $zhwx=new zhphpWeixinApi();//实例化
- $configArr=array(
- 'token'=>'weixintest',
- 'appid'=>'wx7b4b6ad5c7bfaae1',
- 'appSecret'=>'faaa6a1e840fa40527934a293fabfbd1',
- 'myweixinId'=>'gh_746ed5c6a58b'
- );
- $zhwx->setConfig($configArr);
- $zhwx->checkAccessToken();
- $userlist=$zhwx->getUserList();
- if(isset($_POST['sub'])){
- $touser=$_POST['userOpenId'];
- //开始群发
- $content='这是群发的文本';
- $data= $zhwx->sendMassMessage('text',$touser,$content);
- echo '<pre>';
- print_r($data);
- echo '</pre>';
- exit;
- }
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <title>微信群发信息</title>
- <style type="text/css">
- .userlistBox{
- width: 1000px;
- height: 300px;
- border: 1px solid red;
- }
- td{ text-align: center; border: 1px solid black;}
- </style>
- </head>
- <body>
- <?php
- $userinfos=array();
- foreach($userlist['data'] as $key=>$user){
- foreach($user as $kk=>$list){
- $userinfos[]=$zhwx->getUserInfo($list);
- }
- }
- ?>
- <form action="wx.php?act=send" method="post">
- <div class="userlistBox">
- <h2>请选择用户</h2>
- <table >
- <tr>
- <th style="width: 10%;">编号</th>
- <th style="width: 15%;">关注事件类型</th>
- <th style="width: 15%;">用户姓名</th>
- <th style="width: 10%;">性别</th>
- <th style="width: 15%;">头像</th>
- <th style="width: 15%;">所在地</th>
- <th style="width: 15%;">所在组</th>
- <th style="width: 5%;">操作</th>
- </tr>
- <?php
- foreach($userinfos as $key=>$userinfo){
- ?>
- <tr>
- <td><?php echo $key+1;?></td>
- <td><?php
- if($userinfo['subscribe'] == 1){
- echo '普通关注事件';
- }
- ?></td>
- <td> <?php echo $userinfo['nickname']; ?></td>
- <td> <?php echo $userinfo['sex']==1?'男':'女'; ?></td>
- <td> <?php
- if(empty($userinfo['headimgurl'])){
- echo '没有头像';
- }else{
- echo '<img width="50px" height="50px" src="'.$userinfo['headimgurl'].'">';
- }
- ?></td>
- <td><?php echo $userinfo['country'].'-'.$userinfo['province'].'-'.$userinfo['city']; ?></td>
- <td><?php
- if($userinfo['groupid'] == 0){
- echo '没有分组';
- }else{
- echo '还有完善功能';
- }
- ?>
- </td>
- <td><input type="checkbox" value="<?php echo $userinfo['openid'];?>" name="userOpenId[]" id="m_<?php echo $key+1; ?>" /></td>
- </tr>
- <?php
- }
- ?>
- </table>
- <input type="submit" name="sub" value="发送" />
- </div>
- </form>
- </body>
- </html>
- #######################################图文发送案例调用
- <?php
- header('Content-type: text/html; charset=utf-8');#设置头信息
- require_once('dodiWeixinApi.class.php');
- $zhwx=new dodiWeixinApi();//实例化
- $configArr=array(
- 'token'=>'weixintest',
- 'appid'=>'wx7b4b6ad5c7bfaae1',
- 'appSecret'=>'faaa6a1e840fa40527934a293fabfbd1',
- 'myweixinId'=>'gh_746ed5c6a58b'
- );
- $zhwx->setConfig($configArr);##配置参数
- $zhwx->checkAccessToken();##检查token
- $userlist=$zhwx->getUserList();##获取用户列表
- //var_dump($userlist);
- //$callData=$zhwx->mediaUpload('image','a.jpg');## 上传图片到微信 得到 media id
- //CQKrXGNiJWiKutD17CWhoqlqW_80IByyFpLWa-dnPzhl7jPpQFiBbXhsAQVwId-q
- $data=array(
- array(
- "thumb_media_id"=>"CQKrXGNiJWiKutD17CWhoqlqW_80IByyFpLWa-dnPzhl7jPpQFiBbXhsAQVwId-q",
- "author"=>"xxx",
- "title"=>"Happy Day",
- "content_source_url"=>"www.qq.com",
- "content"=>"<div style='width: 100%; height: 50px; background-color: red;'>文章内容</div>",
- "diges"=>"digest"
- ),
- );
- //$aa=$zhwx->newsUpload($data);//上传图文素材[ media_id] => 2n32OjOIdP7jewG0d55bKvuJYcH6XpWTeg8ViHCfzHGgKBZxmE72s3T2LY-3Q8LO
- if(isset($_POST['sub'])){
- $touser=$_POST['userOpenId'];
- //开始群发
- $content='
- <div><img src="http://d005151912.0502.dodi.cn/a.jpg" style="width:100%; height:50px;"></div>
- <div> 这是商品说明</div>
- ';
- $content.='查看详情: <a href="http://msn.huanqiu.com/china/article/2016-01/8473360.html">国家统计局局长王保安涉嫌严重违纪被免职</a>';
- // $data= $zhwx->sendMassMessage('text',$touser,$content);
- $data=$zhwx->sendMassMessage('news',$touser,'2n32OjOIdP7jewG0d55bKvuJYcH6XpWTeg8ViHCfzHGgKBZxmE72s3T2LY-3Q8LO'); ##主动发送图文消息
- echo '<pre>';
- print_r($data);
- echo '</pre>';
- exit;
- }
- ?>
下面是我的写法
1.首先是我的文件结构
2.文件偷偷打开来给你看看
- <?php
- header('Content-type: text/html; charset=utf-8');#设置头信息
- require_once('weixinapi.php');#加载微信接口类文件
- $zhwx=new zhphpWeixinApi();//实例化
- $configArr=array(//自己修改
- 'token'=>'----------------',
- 'appid'=>'----------------------',
- 'appSecret'=>'--------------------',
- 'myweixinId'=>'--------------------'
- );
- $zhwx->setConfig($configArr);
- if($zhwx->weixinBaseApiMessage()){ //基本微信接口会话 必须
- $keyword=$zhwx->getUserTextRequest();//得到用户发的微信内容, 这里可以写你的业务逻辑
- $msgtype=$zhwx->getUserMsgType();//得到用户发的微信数据类型, 这里可以写你的业务逻辑
- $SendTime=$zhwx->getUserSendTime();//得到用户发送的时间
- $fromUserName=$zhwx->getUserWxId();//用户微信id
- $pingtaiId=$zhwx->getPlatformId();//平台微信id
- $requestId=$zhwx->getUserRequestId(); //获取微信公众平台消息ID
- $userPostData=$zhwx->getUserPostData();//获取用户提交的post 数据对象集
- if( $msgtype == 'text' ){ //如果是文本类型
- if($keyword == 'news'){ //新闻类别请求
- $callData=array(
- array('Title'=>'第一条新闻','Description'=>'第一条新闻的简介','PicUrl'=>'https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3246575267,854993829&fm=27&gp=0.jpg','Url'=>'https://mp.weixin.qq.com/s/ay8RRkElw2xUlffeDnhQLw'),
- array('Title'=>'第二条新闻的标题','Description'=>'第一条新闻的简介','PicUrl'=>'https://images0.cnblogs.com/blog2015/340216/201505/051316468603876.png','Url'=>'http://wxphptest1.applinzi.com/'),
- array('Title'=>'第三条新闻的标题','Description'=>'第一条新闻的简介','PicUrl'=>'http://dimgcn2.s-msn.com/imageadapter/w60h60q85/stimgcn3.s-msn.com/msnportal/hp/2016/01/21/3e3ef7f07361b5211e2b155bb66fa6a9.jpg','Url'=>'http://wxphptest1.applinzi.com/'),
- );
- $zhwx->responseMessage('news',$callData);
- }elseif($keyword == 'music'){ //音乐请求
- $music=array(
- "Title"=>"安眠曲",
- "Description"=>"谭嗣同",
- "MusicUrl"=>"https://www.xiami.com/song/xN1kyHa331d?spm=a1z1s.3521865.23309997.51.kf2b5r"
- // ,"HQMusicUrl"=>"http://www.kugou.com/song/#hash=3339B5F7024EC2ABB8954C0CC4983548&album_id=548709"
- );
- $zhwx->responseMessage('music',$music);
- }else{
- $callData='我轻轻的告诉你~
- 你输入的内容是:'.$keyword.'
- 你发数据类型是:'.$msgtype.'
- 你发的数据时间是:'.$SendTime.'
- ------------------------------------------'.'
- 输入 "news" "music"试试吧!';
- $zhwx->responseMessage('text',$callData);
- }
- }else if($msgtype == 'image'){ //如果是图片类型
- $image=$zhwx->getUserImageRequest();//如果用户发的是图片信息,就去得到用户的图形 信息
- $callData=$image['MediaId'];//mediaID 是微信公众平台返回的图片的id,微信公众平台是依据该ID来调用用户所发的图片
- $zhwx->responseMessage('image',$callData);
- }else if($msgtype == 'voice'){ //如果是语音消息,用于语音交流
- $callData=$zhwx->getUserVoiceRequest(); //返回数组
- $zhwx->responseMessage('voice',$callData);
- }elseif($msgtype == 'event'){//事件,由系统自动检验类型
- $content='欢迎光临━(*`∀´*)ノ亻'.'
- 随意输入试试吧!';//给数据为数据库查询出来的
- $callData=$zhwx->responseEventMessage($content);//把数据放进去,由系统自动检验类型,并返回字符串或者数组的结果
- $zhwx->responseMessage('text',$callData); //使用数据
- }else{
- $callData='我听不清你在说什么:
- 你发数据类型是:'.$msgtype.'
- 你发的数据时间是:'.$SendTime.'
- 我接收的ID是:'.$requestId;
- $zhwx->responseMessage('text',$callData);
- }
- }
wei.php
源码和weixinapi.php是不一样的,后者删减了部分,自己去对照。。
基本就这样了,可以去公众号上测试
PHP微信公众号开发之自动回复的更多相关文章
- Java微信公众号开发----关键字自动回复消息
在配置好开发者配置后,本人第一个想要实现的是自动回复消息的功能,说明以下几点: 1. url 仍然不变,还是开发配置里的url 2. 微信采用 xml 格式传输数据 3.微信服务器传给我们的参数主要有 ...
- [.NET] 使用 Senparc.Weixin 接入微信公众号开发:简单实现自动回复
使用 Senparc.Weixin 接入微信公众号开发:简单实现自动回复 目录 一.前提 二.基本配置信息简析 三.配置服务器地址(URL) 四.请求处理 一.前提 先申请微信公众号的授权,找到或配置 ...
- 线程安全使用(四) [.NET] 简单接入微信公众号开发:实现自动回复 [C#]C#中字符串的操作 自行实现比dotcore/dotnet更方便更高性能的对象二进制序列化 自已动手做高性能消息队列 自行实现高性能MVC WebAPI 面试题随笔 字符串反转
线程安全使用(四) 这是时隔多年第四篇,主要是因为身在东软受内网限制,好多文章就只好发到东软内部网站,懒的发到外面,现在一点点把在东软写的文章给转移出来. 这里主要讲解下CancellationT ...
- 快递Api接口 & 微信公众号开发流程
之前的文章,已经分析过快递Api接口可能被使用的需求及场景:今天呢,简单给大家介绍一下微信公众号中怎么来使用快递Api接口,来完成我们的需求和业务场景. 开发语言:Nodejs,其中用到了Neo4j图 ...
- .net微信公众号开发——消息与事件
作者:王先荣 本文介绍如何处理微信公众号开发中的消息与事件,包括:(1)消息(事件)概况:(2)验证消息的真实性:(3)解析消息:(4)被动回复消息:(5)发送其他消息. 开源项目地址:h ...
- .net微信公众号开发——快速入门
作者:王先荣 最近在学习微信公众号开发,将学习的成果做成了一个类库,方便重复使用. 现在微信公众号多如牛毛,开发微信的高手可以直接无视这个系列的文章了. 使用该类库的流程及寥寥数行代码得到的结果如下. ...
- C#微信公众号开发 -- (六)自定义菜单事件之CLICK
微信公众号中当用户手动点击了按钮,微信公众号会被动的向用户发送文字消息或者图文消息. 通过C#微信公众号开发 -- (五)自定义菜单创建 我们知道了如何将CLICK类型的按钮添加到自己的微信公众平台上 ...
- 微信公众号开发被动回复用户消息,回复内容Content使用了"\n"换行符还是没有换行
使用语言和框架:本人后端开发使用的Python的DRF(Django REST framework)框架 需求:在微信公众号开发时,需要实现自动回复,即被关注回复.收到消息回复.关键词回复 发现问题: ...
- python之微信公众号开发(基本配置和校验)
前言 最近有微信公众号开发的业务,以前没有用python做过微信公众号开发,记录一下自己的学习和开发历程,共勉! 公众号类型 订阅号 普通订阅号 认证订阅号 服务号 普通服务号 认证服务号 服务方式 ...
随机推荐
- 让使用SQLite的.NET应用自适应32位/64位系统
如果一个.NET应用要自适应32位/64位系统,只需要在项目的“目标平台”设置为“Any CPU”.但是如果应用中使用了SQLite,情况就不同了. SQLite的.NET开发包来自是System.D ...
- VS中实时获取SVN的版本号并写入到AssemblyInfo.cs中
在开发项目时,需要知道当前发布的到底是哪个版本,比较好的方式就是获取SVN的版本来作为项目的版本.项目版本一般由主版本.次版本.内部版本.修改版本四个部分组成,我们获取的SVN版本就作为修改版本即可. ...
- BeanShell用法(摘抄至网络)
说明:本文部分资料摘抄至 来源: http://www.cnblogs.com/puresoul/p/4915350.html 来源: http://www.cnblogs.com/puresoul/ ...
- 在free bsd上跑JMeter 的 plugin "PerfMon Server Agent"
在free bsd上跑JMeter 的 plugin "PerfMon Server Agent" 目的: 在free bsd上跑JMeter 的 plugin "Per ...
- Redis set数据结构
set里的数据不能重复 1. 增加set1,值为 a b c d 1 2 3 2. 返回集合元素的数量 3. 重命名set1为set100 4. 查看集合中的成员 5.sdiff set100 set ...
- Visual Studio: 一键卸载所有组件工具,彻底卸载干净。
第一步.手动卸载VS主体 第二步.下载工具并解压 网盘下载地址:https://pan.baidu.com/s/1eSHRYxW 也可以在Github上下载最新版本:https://github.co ...
- 调试PHP错误
error_reporting(E_ALL & ~E_NOTICE); ini_set('display_errors', "On");
- win2008 server 多IP配置
本人服务器环境 win8 + phpstudy 一个服务器多个IP 以前都是用linux,买了几套源码结果都是win8server 服务器+phpstudy. 渐渐也就随大流了.懒的去琢磨 一 ...
- 关键两招就解决Wampserver 打开localhost显示IIS7图片问题
我们在安装集成环境Wampserver之后,有时会遇到一个问题, 打开localhost显示一张IIS7图片,这个问题该如何解决呢,我在网上找了一些说的都很乱,我在这里简单整理了一下解决方法 1 ...
- Ubuntu 14.10 下Eclipse操作HBase
环境介绍 64位Ubuntu14.10,Hadoop 2.5.0 ,HBase 0.99.0 准备环境 1 安装Hadoop 2.5.0,可参考http://www.cnblogs.com/liuch ...