易信公众平台自定义菜单及响应php
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
//将本部分代码保存为menu.php,填写自己的appid和appsecret,在post处修改自己的菜单,在浏览器端输入..../menu.php,返回errmsg":"请求成功","errcode":0,即为成功。
$appid="";//填写appid
$secret="";//填写appsecret
$url = "https://api.yixin.im/cgi-bin/token?grant_type=client_credential&appid={ $appid}&secret={$secret}";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$a = curl_exec($ch);
$strjson=json_decode($a);
$token = $strjson->access_token;
$post = ' { "button":[ { "name":"在线客服", "sub_button":[ { "name":"召唤精灵", "type":"click", "key":"/dt2/ai.yx" }, { "name":"人工服务", "type":"click", "key":"/dt2/chat.yx" } ] }, { "name":"自助服务", "type":"click", "key":"/dt2/self.yx?id=36001" }, { "name":"帐号服务", "type":"click", "key":"/dt2/self.yx" }, { "name":"其他",
"sub_button":[ { "name":"將军令", "type":"click", "key":"/dt2/self.yx?id=77011" }, { "name":"防盜教程", "type":"click", "key":"/dt2/self.yx" }, { "name":"防骗教程", "type":"click", "key":"/dt2/self.yx" }, { "name":"其他介紹", "type":"click", "key":"/dt2/self.yx" }, { "name":"切换帐号", "type":"click", "key":"/dt2/logout.yx" } ] } ] } ';
$url = "https://api.yixin.im/cgi-bin/menu/create?access_token={$token}"; $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_exec($ch);
curl_close($ch);
?>
以下为响应自定义菜单,在elseif($customevent=="CLICK")部分添加。
//易信公众平台地址https://plus.yixin.im/login,复制后后缀改为.php
define("TOKEN", "yixin");
$wechatObj = new wechatCallbackapiTest();
if (isset($_GET['echostr'])) {
$wechatObj->valid();
}else{
$wechatObj->responseMsg();
}
class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET["echostr"];
//valid signature , option
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
public function responseMsg()
{
//get post data, May be due to the different environments
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
//extract post data
if (!empty($postStr)){
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDA TA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$type = $postObj->MsgType;
$key=$postObj->EventKey;
$customevent = $postObj->Event;
$latitude = $postObj->Location_X;
$longitude = $postObj->Location_Y;
$keyword = trim($postObj->Content);
$time = time();
$textTpl = "
";
switch ($type)
{ case "event";
if ($customevent=="subscribe")
{$contentStr = "感谢你的关注\n回复1查看账号简介\n回复2查看联系方式\n回复3查看法律文书";}
elseif($customevent=="CLICK")
{
if ($key== "/dt2/ai.yx")
{