目录

0x01 订阅号 和 服务号之间的区别

0x02 申请条件

0x03 开发前的准备

0x04微信公众号后台的简单使用

0x05 接入到微信服务器

0x06 微信发送接受消息实现的原理

#消息传输的形式

#用户发送文本消息并回复

#用户发送图片消息回复

#语音消息的回复:

#多图文消息回复

#用户关注后回复


微信公众号:mp.weixin.qq.com

0x01 订阅号 和 服务号之间的区别

服务号:1个月只能推送4条消息,侧重于为企业服务,有微信支付功能

订阅号:每天可以推送1条,一般自媒体申请,没有微信支付接口

实际运营的话,一个公司可以同时申请服务号和订阅号

0x02 申请条件

一个身份证号只能申请5个公众号,

微信支付需要对公账号

认证的过程大概需要2周左右,并且需要交300元的费用,认证后开放的微信接口更多

0x03 开发前的准备

1.外网服务器+备案的域名

2.已经认证好的服务号或者订阅号(也可以是测试号)

0x04微信公众号后台的简单使用

0x05 接入到微信服务器

<?php
$data = $_GET;
$token='你设置的token';
$signature=$_GET['signature'];
$timestamp=$_GET['timestamp'];
$nonce = $_GET['nonce'];
$echostr = $_GET['echostr'];
//$token $timestamp $nonce 拼接成数组然后排序 然后在转化为字符串,再sha1加密,获取签名
$arr = [$token,$timestamp,$nonce];
sort($arr);
// 将数组转化为字符串
$sign = sha1(implode($arr));
//如果我们加密出来的签名 和 微信发过来的签名一样 就把$echostr返回回去
if($sign==$signature){echo $echostr;
}
?>

0x06 微信发送接受消息实现的原理

首先用户发送消息给微信服务器,微信服务器,把消息转给开发者服务器的入口url,开发者根据用户发的消息返回回复内容给微信服务器,微信服务器转给用户。

#消息传输的形式

在后台配置有三种模式:

明文模式:微信服务器 和我们服务器之间的消息传输是以明文传输的,方便调试

兼容模式:既可以是明文也可以是加密

安全模式:微信服务器和我们服务器之间的消息传输都是加密的,上线后改为安全模式。

微信服务器发给我们的服务器的消息有两种:一部分是get,另一部分是post

以get发送的数据,可以直接用$_GET接受

以post发送的数据,需要用$post_data = file_put_contents('php://input')来接收

<?php
$data = $_GET;
$post_data = file_get_contents('php://input');$token='weixin';
$signature=$_GET['signature'];
$timestamp=$_GET['timestamp'];
$nonce = $_GET['nonce'];
$echostr = $_GET['echostr'];
//$token $timestamp $nonce 拼接成数组然后排序 然后在转化为字符串,再sha1加密,获取签名
$arr = [$token,$timestamp,$nonce];
sort($arr);
// 将数组转化为字符串
$sign = sha1(implode($arr));
file_put_contents('info.txt',json_encode($data).'\n',FILE_APPEND);
file_put_contents('info.txt',json_encode($post_data).'\n',FILE_APPEND);
//如果我们加密出来的签名 和 微信发过来的签名一样 就把$echostr返回回去
if($sign==$signature){echo $echostr;
}
?>

open_id 是公众号的唯一标识

明文get发送的数据 signature timestamp  nonce openid4个参数{"signature":"957ea3513433448757cb22c4a04ea9ec983e2c2f","timestamp":"1513683478","nonce":"2103214831","openid":"oovLMv78NK5zhdr6AZ-NHBPFo"
}post发送的数据<xml><ToUserName><![CDATA[gh_8c6e72a02897]]><\/ToUserName><FromUserName><![CDATA[oovLMv78NK5zhdr6AZ-WWWNHBPFo]]><\/FromUserName><CreateTime>1513683478<\/CreateTime><MsgType><![CDATA[text]]><\/MsgType><Content><![CDATA[\u54c8\u54c8]]><\/Content><MsgId>6501221034921908180<\/MsgId>
<\/xml>安全模式 get发送的数据    signature    timestamp    openid  encrypt_type  msg_signature{"signature":"c8ce5973608bd9a59a267fafc4b4be4fd3a81e3e","timestamp":"1513684346","nonce":"2089005646","openid":"oovLMv78NK5zhdr6AZ-WWWNHBPFo","encrypt_type":"aes","msg_signature":"aff0498ab42c973a058f0d75562745405abb5caa"
}
post发送的数据<xml>   <ToUserName><![CDATA[gh_8c6e72a02897]]><\/ToUserName>\n    <Encrypt><![CDATA[ALbwJ1Tr0vO\/iwGhHUHRtb0RxcJto2E0Gipbo7htYWwnO7bUCXOYEkk3ht5qdPIsJb1NDLHUPWyE3jd2Hqc\/tLu\/3kwGM14dlg8lxDdF2SeMXxMZz8KxP72mL4u6dHSesQWPWJpwj8og9gcC23owjbIxlwMPwqf6t3UfELFZI\/p3Nd7tDvr5BZoj8fHPo8nQPYPNemEbYKuN\/Yw6+jVKQSqEkMHUvg8YFW7AC0PLzfCJVogGjehcL1bSPDIEgWT84o+qMJOSm9b+3ahNHhuOht1tsnuJ2860ysnuTLVRz9JEIVy24PRZr\/LaIXp5ox8CJn4zdqspGUZTWuLokC3EmS7EMck6iWA7etB\/h8bQbuaGtWV+Q6ZO92QtOeMTFgLsf7v004wdsO3bgjTqA4CfEzETN8aOiG3zvZuxoaLqvhU=]]><\/Encrypt>
<\/xml>

安全模式其实就是将明文模式下的这一部分:

加密为了:

解释:

ToUserName 接受者

FromUserName发送者

MsgType 消息类型 可以是 text img voice

Content 消息的内容

#用户发送文本消息并回复

用户发送消息给公众号,公众号将消息转发给我们服务器,服务器上脚本对消息进行处理,然后将回复消息返回给公众号,公众号回复用户信息

因为我们选择的是安全模式,所以需要先在官方文档中下载消息加解密插件:

<?php
$data = $_GET;
$token='你的token';
$encodingAesKey='你的encodingAesKey';
$appId = 'wx3e149f4a28aab453';
$signature=$_GET['signature'];
$timestamp=$_GET['timestamp'];
$nonce = $_GET['nonce'];
$echostr = $_GET['echostr'];//如果有echostr 就接入服务器
if($echostr){//$token $timestamp $nonce 拼接成数组然后排序 然后在转化为字符串,再sha1加密,获取签名$arr = [$token,$timestamp,$nonce];sort($arr);// 将数组转化为字符串$sign = sha1(implode($arr));//如果我们加密出来的签名 和 微信发过来的签名一样 就把$echostr返回回去if($sign==$signature){echo $echostr;}
}else{
//如果没有echostr,就接受用户发过来的数据require_once "./encrypt/wxBizMsgCrypt.php";$msg_sign = $_GET['msg_signature'];$post_data = file_get_contents('php://input');$pc = new WXBizMsgCrypt($token,$encodingAesKey,$appId);//post_data是被加密的数据,解密后的数据会被赋值给msg$errCode = $pc->decryptMsg($msg_sign,$timestamp,$nonce,$post_data,$msg);// file_put_contents('info.txt',json_encode($data).'\n',FILE_APPEND);file_put_contents('encode.txt',json_encode($post_data).'\n',FILE_APPEND);file_put_contents('decode.txt',$msg.'\n',FILE_APPEND);file_put_contents('error.txt',$errCode,FILE_APPEND);//把xml数据转化为对象$obj = simplexml_load_string($msg);$msg_type = $obj->MsgType;$content = $obj->Content;file_put_contents('data.txt',$msg_type.'==='.$content,FILE_APPEND);if($msg_type='text'){if($content=='你好'){$text = '同好同好!';$res = transText($obj,$text);}//对返回的信息$res进行加密生成密文$encryptMsg$errorCode = $pc->encryptMsg($res,$timestamp,$nonce,$encryptMsg);//将密文返回给微信服务器,微信服务器返回给用户echo $encryptMsg;}
}
function transText($obj,$content){$xml = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[%s]]></Content></xml>";$result = sprintf($xml,$obj->FromUserName,$obj->ToUserName,time(),$content);return $result;
}
?>

#用户发送图片消息回复

Demo:实现用户发送什么图片就回复什么图片的功能

<?php
$data = $_GET;
$token='你的token';
$encodingAesKey='你的aeskey';
$appId = 'wx3e149f4a28aab453';
$signature=$_GET['signature'];
$timestamp=$_GET['timestamp'];
$nonce = $_GET['nonce'];
$echostr = $_GET['echostr'];//如果有echostr 就接入服务器
if($echostr){//$token $timestamp $nonce 拼接成数组然后排序 然后在转化为字符串,再sha1加密,获取签名$arr = [$token,$timestamp,$nonce];sort($arr);// 将数组转化为字符串$sign = sha1(implode($arr));//如果我们加密出来的签名 和 微信发过来的签名一样 就把$echostr返回回去if($sign==$signature){echo $echostr;}
}else{
//如果没有echostr,就接受用户发过来的数据require_once "./encrypt/wxBizMsgCrypt.php";$msg_sign = $_GET['msg_signature'];$post_data = file_get_contents('php://input');$pc = new WXBizMsgCrypt($token,$encodingAesKey,$appId);//post_data是被加密的数据,解密后的数据会被赋值给msg$errCode = $pc->decryptMsg($msg_sign,$timestamp,$nonce,$post_data,$msg);// file_put_contents('info.txt',json_encode($data).'\n',FILE_APPEND);file_put_contents('encode.txt',json_encode($post_data).'\n',FILE_APPEND);file_put_contents('decode.txt',$msg.'\n',FILE_APPEND);file_put_contents('error.txt',$errCode,FILE_APPEND);//把xml数据转化为对象$obj = simplexml_load_string($msg);$msg_type = $obj->MsgType;$content = $obj->Content;file_put_contents('data.txt',$msg_type.'==='.$content,FILE_APPEND);if($msg_type=='text'){if($content=='你好'){$text = '同好同好!';$res = transText($obj,$text);}}elseif($msg_type=='image'){$res = transImage($obj);}//对返回的信息$res进行加密生成密文$encryptMsg$errorCode = $pc->encryptMsg($res,$timestamp,$nonce,$encryptMsg);//将密文返回给微信服务器,微信服务器返回给用户echo $encryptMsg;
}
//文本消息格式
function transText($obj,$content){$xml = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[%s]]></Content></xml>";$result = sprintf($xml,$obj->FromUserName,$obj->ToUserName,time(),$content);return $result;
}
//图片消息格式
function transImage($obj){$xml="<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[image]]></MsgType><Image><MediaId><![CDATA[%s]]></MediaId></Image></xml>";//实现功能:用户发什么图片,我们就回复什么图片$result =sprintf($xml,$obj->FromUserName,$obj->ToUserName,time(),$obj->MediaId);return $result;}
?>

#语音消息的回复:

Demo:用户发送什么语音,我们就回复什么语音

<?php
$data = $_GET;
$token='你的token';
$encodingAesKey='你的aeskey';
$appId = 'wx3e149f4a28aab453';
$signature=$_GET['signature'];
$timestamp=$_GET['timestamp'];
$nonce = $_GET['nonce'];
$echostr = $_GET['echostr'];//如果有echostr 就接入服务器
if($echostr){//$token $timestamp $nonce 拼接成数组然后排序 然后在转化为字符串,再sha1加密,获取签名$arr = [$token,$timestamp,$nonce];sort($arr);// 将数组转化为字符串$sign = sha1(implode($arr));//如果我们加密出来的签名 和 微信发过来的签名一样 就把$echostr返回回去if($sign==$signature){echo $echostr;}
}else{
//如果没有echostr,就接受用户发过来的数据require_once "./encrypt/wxBizMsgCrypt.php";$msg_sign = $_GET['msg_signature'];$post_data = file_get_contents('php://input');$pc = new WXBizMsgCrypt($token,$encodingAesKey,$appId);//post_data是被加密的数据,解密后的数据会被赋值给msg$errCode = $pc->decryptMsg($msg_sign,$timestamp,$nonce,$post_data,$msg);// file_put_contents('info.txt',json_encode($data).'\n',FILE_APPEND);file_put_contents('encode.txt',json_encode($post_data).'\n',FILE_APPEND);file_put_contents('decode.txt',$msg.'\n',FILE_APPEND);file_put_contents('error.txt',$errCode,FILE_APPEND);//把xml数据转化为对象$obj = simplexml_load_string($msg);$msg_type = $obj->MsgType;$content = $obj->Content;file_put_contents('data.txt',$msg_type.'==='.$content,FILE_APPEND);if($msg_type=='text'){if($content=='你好'){$text = '同好同好!';$res = transText($obj,$text);}}elseif($msg_type=='image'){$res = transImage($obj);}elseif($msg_type=='voice'){$res = transVoice($obj);}//对返回的信息$res进行加密生成密文$encryptMsg$errorCode = $pc->encryptMsg($res,$timestamp,$nonce,$encryptMsg);//将密文返回给微信服务器,微信服务器返回给用户echo $encryptMsg;
}
//文本消息格式
function transText($obj,$content){$xml = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[%s]]></Content></xml>";$result = sprintf($xml,$obj->FromUserName,$obj->ToUserName,time(),$content);return $result;
}
//图片消息格式
function transImage($obj){$xml="<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[image]]></MsgType><Image><MediaId><![CDATA[%s]]></MediaId></Image></xml>";//实现功能:用户发什么图片,我们就回复什么图片$result =sprintf($xml,$obj->FromUserName,$obj->ToUserName,time(),$obj->MediaId);return $result;}
//语音消息格式
function transVoice($obj){$xml="<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[voice]]></MsgType><Voice><MediaId><![CDATA[%s]]></MediaId></Voice></xml>";$result =sprintf($xml,$obj->FromUserName,$obj->ToUserName,time(),$obj->MediaId);return $result;}
?>

#多图文消息回复

#用户关注后回复

<?php
require_once './model/passage.php';
$passage = new passage();
$data = $_GET;
$token='你的token';
$encodingAesKey='你的aeskey';
$appId = '你的appid';
$signature=$_GET['signature'];
$timestamp=$_GET['timestamp'];
$nonce = $_GET['nonce'];
$echostr = $_GET['echostr'];//如果有echostr 就接入服务器
if($echostr){//$token $timestamp $nonce 拼接成数组然后排序 然后在转化为字符串,再sha1加密,获取签名$arr = [$token,$timestamp,$nonce];sort($arr);// 将数组转化为字符串$sign = sha1(implode($arr));//如果我们加密出来的签名 和 微信发过来的签名一样 就把$echostr返回回去if($sign==$signature){echo $echostr;}
}else{
//如果没有echostr,就接受用户发过来的数据require_once "./encrypt/wxBizMsgCrypt.php";$msg_sign = $_GET['msg_signature'];$post_data = file_get_contents('php://input');$pc = new WXBizMsgCrypt($token,$encodingAesKey,$appId);//post_data是被加密的数据,解密后的数据会被赋值给msg$errCode = $pc->decryptMsg($msg_sign,$timestamp,$nonce,$post_data,$msg);// file_put_contents('info.txt',json_encode($data).'\n',FILE_APPEND);file_put_contents('encode.txt',json_encode($post_data).'\n',FILE_APPEND);file_put_contents('decode.txt',$msg.'\n',FILE_APPEND);file_put_contents('error.txt',$errCode,FILE_APPEND);//把xml数据转化为对象$obj = simplexml_load_string($msg);$msg_type = $obj->MsgType;$content = $obj->Content;file_put_contents('data.txt',$msg_type.'==='.$content,FILE_APPEND);if($msg_type=='text'){if($content=='你好'){$text = '同好同好!';$res = transText($obj,$text);}elseif($content=='图文'){$res = transTexts($obj);}else{$content = addslashes($content);$where = "title like '%$content%' or content like '%$content%'";$ret  = $passage->selectPassage($where);file_put_contents('data.txt',json_encode($ret),FILE_APPEND);$res = transTextsEx($obj,$ret);}}elseif($msg_type=='image'){$res = transImage($obj);}elseif($msg_type=='voice'){$res = transVoice($obj);}elseif($msg_type=='event'){$event = $obj->Event;//关注事件if($event == 'subscribe'){$text = '欢迎关注DataBank西电站';$res = transText($obj,$text); //取消关注事件}elseif($event=='unsubscribe'){//取消关注用户已经看不见了}}//对返回的信息$res进行加密生成密文$encryptMsg$errorCode = $pc->encryptMsg($res,$timestamp,$nonce,$encryptMsg);file_put_contents('errorCode.txt',$errorCode);//将密文返回给微信服务器,微信服务器返回给用户echo $encryptMsg;
}
//文本消息格式
function transText($obj,$content){$xml = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[%s]]></Content></xml>";$result = sprintf($xml,$obj->FromUserName,$obj->ToUserName,time(),$content);return $result;
}
//图片消息格式
function transImage($obj){$xml="<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[image]]></MsgType><Image><MediaId><![CDATA[%s]]></MediaId></Image></xml>";//实现功能:用户发什么图片,我们就回复什么图片$result =sprintf($xml,$obj->FromUserName,$obj->ToUserName,time(),$obj->MediaId);return $result;}
//语音消息格式
function transVoice($obj){$xml="<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[voice]]></MsgType><Voice><MediaId><![CDATA[%s]]></MediaId></Voice></xml>";$result =sprintf($xml,$obj->FromUserName,$obj->ToUserName,time(),$obj->MediaId);return $result;
}
//图文消息
function transTextsEx($obj,$data){$pic_url = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1590926116552&di=07551eb958090165e0ac75f03dc8fb26&imgtype=0&src=http%3A%2F%2Fa3.att.hudong.com%2F14%2F75%2F01300000164186121366756803686.jpg";$url="http://www.baidu.com";$count = count($data);$toUserName =$obj->FromUserName;$fromUserName = $obj->ToUserName;$time = time();$xml ="<xml><ToUserName><![CDATA[$toUserName]]></ToUserName><FromUserName><![CDATA[$fromUserName]]></FromUserName><CreateTime>$time</CreateTime><MsgType><![CDATA[news]]></MsgType><ArticleCount>$count</ArticleCount><Articles>";foreach($data as $v){$title = trim(strip_tags($v['title']));$content = trim(strip_tags($v['content']));$xml.="<item><Title><![CDATA[$title]]></Title><Description><![CDATA[$content]]></Description><PicUrl><![CDATA[$pic_url]]></PicUrl><Url><![CDATA[$url]]></Url></item>";}$xml.='</Articles></xml>';file_put_contents('xml.txt',$xml);return $xml;
} //图文消息
function transTexts($obj){$pic_url = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1590926116552&di=07551eb958090165e0ac75f03dc8fb26&imgtype=0&src=http%3A%2F%2Fa3.att.hudong.com%2F14%2F75%2F01300000164186121366756803686.jpg";$url="http://www.baidu.com";$xml="<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[news]]></MsgType><ArticleCount>2</ArticleCount><Articles><item><Title><![CDATA[标题1]]></Title><Description><![CDATA[内容描述]]></Description><PicUrl><![CDATA[%s]]></PicUrl><Url><![CDATA[%s]]></Url></item><item><Title><![CDATA[标题2]]></Title><Description><![CDATA[内容描述2]]></Description><PicUrl><![CDATA[%s]]></PicUrl><Url><![CDATA[%s]]></Url></item></Articles></xml>";$result =sprintf($xml,$obj->FromUserName,$obj->ToUserName,time(),$pic_url,$url,$pic_url,$url);file_put_contents('transTexts.txt',$result);return $result;
}
?>

#实战:回复英文励志语录

我调用的api平台:https://www.showapi.com/ 万维易源

坑:文本消息要想在公众号那边实现换行,\n 必须用" " 引住才会被解析为换行符,用单引号则会被解析为字符串

<?php
require_once './model/passage.php';
include_once 'showapi_sdk_php/Util/Autoloader.php';//包含sdk中的自动导入工具类
$passage = new passage();
$data = $_GET;
$token='weixin';
$encodingAesKey='你的encodingAesKey';
$appId = '你的appid';
$signature=$_GET['signature'];
$timestamp=$_GET['timestamp'];
$nonce = $_GET['nonce'];
$echostr = $_GET['echostr'];//如果有echostr 就接入服务器
if($echostr){//$token $timestamp $nonce 拼接成数组然后排序 然后在转化为字符串,再sha1加密,获取签名$arr = [$token,$timestamp,$nonce];sort($arr);// 将数组转化为字符串$sign = sha1(implode($arr));//如果我们加密出来的签名 和 微信发过来的签名一样 就把$echostr返回回去if($sign==$signature){echo $echostr;}
}else{
//如果没有echostr,就接受用户发过来的数据require_once "./encrypt/wxBizMsgCrypt.php";$msg_sign = $_GET['msg_signature'];$post_data = file_get_contents('php://input');$pc = new WXBizMsgCrypt($token,$encodingAesKey,$appId);//post_data是被加密的数据,解密后的数据会被赋值给msg$errCode = $pc->decryptMsg($msg_sign,$timestamp,$nonce,$post_data,$msg);// file_put_contents('info.txt',json_encode($data).'\n',FILE_APPEND);// file_put_contents('encode.txt',json_encode($post_data).'\n',FILE_APPEND);// file_put_contents('decode.txt',$msg.'\n',FILE_APPEND);// file_put_contents('error.txt',$errCode,FILE_APPEND);//把xml数据转化为对象$obj = simplexml_load_string($msg);$msg_type = $obj->MsgType;$content = $obj->Content;file_put_contents('data.txt',$msg_type.'==='.$content,FILE_APPEND);if($msg_type=='text'){if($content=='你好'){$text = '同好同好!';$res = transText($obj,$text);}elseif($content=='图文'){$res = transTexts($obj);}elseif(strstr($content,'语录') or strstr($content,'英文') or strstr($content,'励志')){$text = getEnglish(); $res = transText($obj,$text);}else{$content = addslashes($content);$where = "title like '%$content%' or content like '%$content%'";$ret  = $passage->selectPassage($where);file_put_contents('data.txt',json_encode($ret),FILE_APPEND);$res = transTextsEx($obj,$ret);}}elseif($msg_type=='image'){$res = transImage($obj);}elseif($msg_type=='voice'){$res = transVoice($obj);}elseif($msg_type=='event'){$event = $obj->Event;//关注事件if($event == 'subscribe'){$text = '欢迎关注DataBank西电站';$res = transText($obj,$text); //取消关注事件}elseif($event=='unsubscribe'){//取消关注用户已经看不见了// 我们可以进行一些数据库的操作}}//对返回的信息$res进行加密生成密文$encryptMsg$errorCode = $pc->encryptMsg($res,$timestamp,$nonce,$encryptMsg);file_put_contents('errorCode.txt',$errorCode);//将密文返回给微信服务器,微信服务器返回给用户echo $encryptMsg;
}
//文本消息格式
function transText($obj,$content){$xml = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[%s]]></Content></xml>";$result = sprintf($xml,$obj->FromUserName,$obj->ToUserName,time(),$content);return $result;
}
//图片消息格式
function transImage($obj){$xml="<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[image]]></MsgType><Image><MediaId><![CDATA[%s]]></MediaId></Image></xml>";//实现功能:用户发什么图片,我们就回复什么图片$result =sprintf($xml,$obj->FromUserName,$obj->ToUserName,time(),$obj->MediaId);return $result;}
//语音消息格式
function transVoice($obj){$xml="<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[voice]]></MsgType><Voice><MediaId><![CDATA[%s]]></MediaId></Voice></xml>";$result =sprintf($xml,$obj->FromUserName,$obj->ToUserName,time(),$obj->MediaId);return $result;
}
//图文消息
function transTextsEx($obj,$data){$pic_url = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1590926116552&di=07551eb958090165e0ac75f03dc8fb26&imgtype=0&src=http%3A%2F%2Fa3.att.hudong.com%2F14%2F75%2F01300000164186121366756803686.jpg";$url="http://www.baidu.com";$count = count($data);$toUserName =$obj->FromUserName;$fromUserName = $obj->ToUserName;$time = time();$xml ="<xml><ToUserName><![CDATA[$toUserName]]></ToUserName><FromUserName><![CDATA[$fromUserName]]></FromUserName><CreateTime>$time</CreateTime><MsgType><![CDATA[news]]></MsgType><ArticleCount>$count</ArticleCount><Articles>";foreach($data as $v){$title = trim(strip_tags($v['title']));$content = trim(strip_tags($v['content']));$xml.="<item><Title><![CDATA[$title]]></Title><Description><![CDATA[$content]]></Description><PicUrl><![CDATA[$pic_url]]></PicUrl><Url><![CDATA[$url]]></Url></item>";}$xml.='</Articles></xml>';// file_put_contents('xml.txt',$xml);return $xml;
} //图文消息
function transTexts($obj){$pic_url = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1590926116552&di=07551eb958090165e0ac75f03dc8fb26&imgtype=0&src=http%3A%2F%2Fa3.att.hudong.com%2F14%2F75%2F01300000164186121366756803686.jpg";$url="http://www.baidu.com";$xml="<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[news]]></MsgType><ArticleCount>2</ArticleCount><Articles><item><Title><![CDATA[标题1]]></Title><Description><![CDATA[内容描述]]></Description><PicUrl><![CDATA[%s]]></PicUrl><Url><![CDATA[%s]]></Url></item><item><Title><![CDATA[标题2]]></Title><Description><![CDATA[内容描述2]]></Description><PicUrl><![CDATA[%s]]></PicUrl><Url><![CDATA[%s]]></Url></item></Articles></xml>";$result =sprintf($xml,$obj->FromUserName,$obj->ToUserName,time(),$pic_url,$url,$pic_url,$url);// file_put_contents('transTexts.txt',$result);return $result;
} //获取英文励志语录
function getEnglish(){$url = "http://route.showapi.com/1211-1";$METHOD = "POST";$showapi_appid="你的showapi_id";//替换此值,你可以在这里找到 https://www.showapi.com/console#/myApp$showapi_sign="你的showapi_sign";//替换此值,你可以在这里找到 https://www.showapi.com/console#/myApp$req = new ShowapiRequest($url,$showapi_appid,$showapi_sign);$req->addTextPara("count","5");$response=$req->post();$data = $response->getContent();//返回的信息是json字符串,需要先转化为json对象$data = json_decode($data);$id = $data->showapi_res_id;$str = '';if($data->showapi_res_code==0){$arr  = $data->showapi_res_body->data;foreach($arr as $k=>$v){// 只有双引号下的\n才会被解析为换行$str.=$v->english."\n";$str.=$v->chinese."\n";$str.="\n";}// file_put_contents('english.txt',$str);}return $str;
}?>

微信公众号开发Day01: 消息回复 英文励志语录回复实战相关推荐

  1. 微信公众号开发:消息与事件处理

    在成功接入微信公众平台之后(如何接入请参考<微信公众号开发:账号申请与接入>),就可以对微信服务器POST过来的消息或者事件XML数据包进行监听与处理了. 在<微信公众号开发:账号申 ...

  2. 微信公众号开发---基础消息能力开发

    微信公众号接收普通消息 1.配置开发服务器 微信公众平台->开发->开发者工具->公众平台测试账号 appid:是微信公众号的唯一标识,通过和appsecret进行验证. URL:开 ...

  3. 微信公众号开发--图文消息发送不显示图片的问题

    使用微信公众号接口实现图文消息推送,遇到问题,不显示图片,且content内容中有英文双引号也不行 1.微信不允许有外链的图片,所以会自动过滤 2."双引号格式问题. 前端把双引号传给后端后 ...

  4. 微信公众号 服务器配置 token 测试,微信公众号开发:基本配置Token验证简单事件回复...

    公众号配置的URL就是你自己服务器上验证TOKEN的方法的地址 比如我验证TOKEN的方法访问地址是http://www.aaa.cn/wx_api.php 那我公众号配置的URL就要填http:// ...

  5. 微信公众号开发(消息推送)

    文章目录 微信公众号开发 运行效果 微信公众号简介 注册微信公众号 注册测试公众号 搭建微信本地调试环境 微信公众号接入(校验签名) 给指定用户推送消息 网页授权获取用户openid 给指定用户发送模 ...

  6. 微信公众号开发-素材/消息管理接口

    开始 本文是 微信公众号开发者模式介绍及接入 的后续,如没看过前文的话,可能看本文会有些懵逼.本文主要介绍微信公众平台的素材.消息管理接口的开发.由于个人的订阅号是没有大多数接口的权限的,所以我们需要 ...

  7. 微信公众号开发——模板消息

    一.测试环境(微信公众号测试号) 遇到很多坑MMP 1.1.首先在测试号配置如下模板样本 {{first.DATA}} 餐厅名称:{{keyword1.DATA}} 就餐人:{{keyword2.DA ...

  8. 微信公众号开发(四)——点击菜单回复图片和语音

    先讲回复图片,语音类似. 这个发送图片,并不是点击输入框右侧的"➕",去选择本地照片,而是要把图片上传的公众号的服务器. 按照开发文档https://developers.weix ...

  9. 微信公众号开发-----接送事件推送之关注/取消关注

    本文主要实现的功能包括 关注/取消关注事件 阅读本文之前请先认真阅读微信公众号技术文档之接受事件推送 需注意的是:验证消息的确来自微信服务器和接收事件时微信服务器都会发送请求到填写的服务器地址URL上 ...

最新文章

  1. window环境Visual Studio配置:OpenCV,Eigen,jsoncpp
  2. 11月21日spring mvc的表单校验培训日记
  3. 半双工、全双工以太网
  4. LightOJ - 1074 Extended Traffic(最短路+判断负环)
  5. 优秀的程序员是那种过单行线马路都要往两边看的人
  6. arcgis for android sdk下载地址,Arcgis Runtime sdk for android 授权
  7. siege4安装和使用介绍
  8. SAP云解决方案和企业本地部署(On-Premise)混合架构下的安全认证权限管理
  9. datagrid底部显示水平滚动_CSS flex 布局,头部和底部固定,中间出现滚动条
  10. 【交换安全】DAI - Dynamic ARP Inspection 详解/arp欺骗/gratuitous arp
  11. matlab模糊闭包,基于matlab的模糊聚类分析
  12. 景观专业设计师必备SketchUp插件合集,你都用过吗?
  13. 人工智能、大数据、云计算概念
  14. 带sex的net域名_域名劫持的几种方法、域名劫持有什么方式
  15. 过去七年,美国护照排名从榜首滑落至第7位
  16. 使用pymysql报错:pymysql.err.InternalError: Packet sequence number wrong - got 5 expected 1
  17. 【无标题】There was an unexpected error (type=Internal Server Error, status=500).
  18. 操作系统春招面试复习之:设备管理
  19. Microsoft Office Word 2010-2016中公式不能自动斜体的解决方法
  20. 前序中序、中序后序以及前序后序构造二叉树

热门文章

  1. 论文查重标题写错了怎么办?
  2. SQL insert插入中存在属性值为空
  3. mybatis根据传进来的参数执行不同的SQL语句
  4. excel 宏 汉子转拼音缩写
  5. java适合内向的人吗_内向者非常适合的10种工作,这些岗位非常适合不爱说话的人...
  6. 【方法】如何使用DeepMask和SharpMask
  7. e代理与和合首创达成战略合作,共创WealthTech生态圈
  8. 中国实验室管理系统行业研究与未来预测报告(2022版)
  9. Codeforces713D(二维RMQ)
  10. 小说作者推荐:渡山山合集