search
Homephp教程php手册php版微信公众平台账号自定义菜单类

微信公众平台服务号可申请自定义菜单了,其它的号暂时不支持自定义菜单了,这个不但可以使用api来操作,还可以直接在后台定义菜单与参数哦。

申请自定义菜单

服务号可以申请自定义菜单;使用QQ登录的公众号,可以升级为邮箱登录;使用邮箱登录的公众号,可以修改登录邮箱;群发消息可以同步到腾讯微博。

微信公众平台升级:服务号可申请自定义菜单

微信公众平台账号api程序

<?php
//define your token
define("TOKEN", "chenxiang"); //改成自己的TOKEN
define(&#39;APP_ID&#39;, &#39;&#39;); //改成自己的APPID
define(&#39;APP_SECRET&#39;, &#39;&#39;); //改成自己的APPSECRET
$wechatObj = new wechatCallbackapiTest(APP_ID, APP_SECRET);
$wechatObj->Run();
class wechatCallbackapiTest {
    private $fromUsername;
    private $toUsername;
    private $times;
    private $keyword;
    private $app_id;
    private $app_secret;
    public function __construct($appid, $appsecret) {
        // code...
        $this->app_id = $appid;
        $this->app_secret = $appsecret;
    }
    public function valid() {
        $echoStr = $_GET["echostr"];
        if ($this->checkSignature()) {
            echo $echoStr;
            exit;
        }
    }
    /**
     * 运行程序
     * @param string $value [description]
     */
    public function Run() {
        $this->responseMsg();
        $arr[] = "您好,这是自动回复,我现在不在,有事请留言,我会尽快回复你的^_^";
        echo $this->make_xml("text", $arr);
    }
    public function responseMsg() {
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; //返回回复数据
        if (!empty($postStr)) {
            $access_token = $this->get_access_token(); //获取access_token
            $this->createmenu($access_token); //创建菜单
            //$this->delmenu($access_token);//删除菜单
            $postObj = simplexml_load_string($postStr, &#39;SimpleXMLElement&#39;, LIBXML_NOCDATA);
            $this->fromUsername = $postObj->FromUserName; //发送消息方ID
            $this->toUsername = $postObj->ToUserName; //接收消息方ID
            $this->keyword = trim($postObj->Content); //用户发送的消息
            $this->times = time(); //发送时间
            $MsgType = $postObj->MsgType; //消息类型
            if ($MsgType == &#39;event&#39;) {
                $MsgEvent = $postObj->Event; //获取事件类型
                if ($MsgEvent == &#39;subscribe&#39;) { //订阅事件
                    $arr[] = "你好,我是xxx,现在我们是好友咯![愉快][玫瑰]";
                    echo $this->make_xml("text", $arr);
                    exit;
                } elseif ($MsgEvent == &#39;CLICK&#39;) { //点击事件
                    $EventKey = $postObj->EventKey; //菜单的自定义的key值,可以根据此值判断用户点击了什么内容,从而推送不同信息
                    $arr[] = $EventKey;
                    echo $this->make_xml("text", $arr);
                    exit;
                }
            }
        } else {
            echo "this a file for weixin API!";
            exit;
        }
    }
    /**
     * 获取access_token
     */
    private function get_access_token() {
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $this->app_id . "&secret=" . $this->app_secret;
        $data = json_decode(file_get_contents($url) , true);
        if ($data[&#39;access_token&#39;]) {
            return $data[&#39;access_token&#39;];
        } else {
            return "获取access_token错误";
        }
    }
    /**
     * 创建菜单
     * @param $access_token 已获取的ACCESS_TOKEN
     */
    public function createmenu($access_token) {
        $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" . $access_token;
        $arr = array(
            &#39;button&#39; => array(
                array(
                    &#39;name&#39; => urlencode("生活查询") ,
                    &#39;sub_button&#39; => array(
                        array(
                            &#39;name&#39; => urlencode("天气查询") ,
                            &#39;type&#39; => &#39;click&#39;,
                            &#39;key&#39; => &#39;VCX_WEATHER&#39;
                        ) ,
                        array(
                            &#39;name&#39; => urlencode("身份证查询") ,
                            &#39;type&#39; => &#39;click&#39;,
                            &#39;key&#39; => &#39;VCX_IDENT&#39;
                        )
                    )
                ) ,
                array(
                    &#39;name&#39; => urlencode("轻松娱乐") ,
                    &#39;sub_button&#39; => array(
                        array(
                            &#39;name&#39; => urlencode("刮刮乐") ,
                            &#39;type&#39; => &#39;click&#39;,
                            &#39;key&#39; => &#39;VCX_GUAHAPPY&#39;
                        ) ,
                        array(
                            &#39;name&#39; => urlencode("幸运大转盘") ,
                            &#39;type&#39; => &#39;click&#39;,
                            &#39;key&#39; => &#39;VCX_LUCKPAN&#39;
                        )
                    )
                ) ,
                array(
                    &#39;name&#39; => urlencode("我的信息") ,
                    &#39;sub_button&#39; => array(
                        array(
                            &#39;name&#39; => urlencode("关于我") ,
                            &#39;type&#39; => &#39;click&#39;,
                            &#39;key&#39; => &#39;VCX_ABOUTME&#39;
                        ) ,
                        array(
                            &#39;name&#39; => urlencode("工作信息") ,
                            &#39;type&#39; => &#39;click&#39;,
                            &#39;key&#39; => &#39;VCX_JOBINFORMATION&#39;
                        )
                    )
                )
            )
        );
        $jsondata = urldecode(json_encode($arr));
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $jsondata);
        curl_exec($ch);
        curl_close($ch);
    }
    /**
     * 查询菜单
     * @param $access_token 已获取的ACCESS_TOKEN
     */
    private function getmenu($access_token) {
        // code...
        $url = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=" . $access_token;
        $data = file_get_contents($url);
        return $data;
    }
    /**
     * 删除菜单
     * @param $access_token 已获取的ACCESS_TOKEN
     */
    private function delmenu($access_token) {
        // code...
        $url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=" . $access_token;
        $data = json_decode(file_get_contents($url) , true);
        if ($data[&#39;errcode&#39;] == 0) {
            // code...
            return true;
        } else {
            return false;
        }
    }
    /**
     *@param type: text 文本类型, news 图文类型
     *@param value_arr array(内容),array(ID)
     *@param o_arr array(array(标题,介绍,图片,超链接),...小于10条),array(条数,ID)
     */
    private function make_xml($type, $value_arr, $o_arr = array(
        0
    )) {
        //=================xml header============
        $con = "<xml>
                    <ToUserName><![CDATA[{$this->fromUsername}]]></ToUserName>
                    <FromUserName><![CDATA[{$this->toUsername}]]></FromUserName>
                    <CreateTime>{$this->times}</CreateTime>
                    <MsgType><![CDATA[{$type}]]></MsgType>";
        //=================type content============
        switch ($type) {
            case "text":
                $con.= "<Content><![CDATA[{$value_arr[0]}]]></Content>
                    <FuncFlag>{$o_arr}</FuncFlag>";
                break;

            case "news":
                $con.= "<ArticleCount>{$o_arr[0]}</ArticleCount>
                     <Articles>";
                foreach ($value_arr as $id => $v) {
                    if ($id >= $o_arr[0]) break;
                    else null; //判断数组数不超过设置数
                    $con.= "<item>
                         <Title><![CDATA[{$v[0]}]]></Title> 
                         <Description><![CDATA[{$v[1]}]]></Description>
                         <PicUrl><![CDATA[{$v[2]}]]></PicUrl>
                         <Url><![CDATA[{$v[3]}]]></Url>
                         </item>";
                }
                $con.= "</Articles>
                     <FuncFlag>{$o_arr[1]}</FuncFlag>";
                break;
            } //end switch
            //=================end return============
            $con.= "</xml>";
            return $con;
        }
        private function checkSignature() {
            $signature = $_GET["signature"];
            $timestamp = $_GET["timestamp"];
            $nonce = $_GET["nonce"];
            $token = TOKEN;
            $tmpArr = array(
                $token,
                $timestamp,
                $nonce
            );
            sort($tmpArr);
            $tmpStr = implode($tmpArr);
            $tmpStr = sha1($tmpStr);
            if ($tmpStr == $signature) {
                return true;
            } else {
                return false;
            }
        }
}
?>


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Java ArrayList遍历时使用foreach和iterator删除元素的区别是什么?Java ArrayList遍历时使用foreach和iterator删除元素的区别是什么?Apr 27, 2023 pm 03:40 PM

一、Iterator和foreach的区别多态差别(foreach底层就是Iterator)Iterator是一个接口类型,他不关心集合或者数组的类型;for和foreach都需要先知道集合的类型,甚至是集合内元素的类型;1.为啥说foreach底层就是Iterator编写的代码:反编译代码:二、foreach与iterator时remove的区别先来看阿里java开发手册但1的时候不会报错,2的时候就会报错(java.util.ConcurrentModificationException)首

php如何判断foreach循环到第几个php如何判断foreach循环到第几个Jul 10, 2023 pm 02:18 PM

​php判断foreach循环到第几个的步骤:1、创建一个“$fruits”的数组;2、创建一个计数器变量“$counter”初始值为0;3、使用“foreach”循环遍历数组,并在循环体中增加计数器变量的值,再输出每个元素和它们的索引;4、在“foreach”循环体外输出计数器变量的值,以确认循环到了第几个元素。

PHP返回一个键值翻转后的数组PHP返回一个键值翻转后的数组Mar 21, 2024 pm 02:10 PM

这篇文章将为大家详细讲解有关PHP返回一个键值翻转后的数组,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。PHP键值翻转数组键值翻转是一种对数组进行的操作,它将数组中的键和值进行交换,生成一个新的数组,其中原始键作为值,原始值作为键。实现方法在php中,可以通过以下方法对数组进行键值翻转:array_flip()函数:array_flip()函数专门用于键值翻转操作。它接收一个数组作为参数,并返回一个新的数组,其中键和值已交换。$original_array=[

PHP返回数组中的当前元素PHP返回数组中的当前元素Mar 21, 2024 pm 12:36 PM

这篇文章将为大家详细讲解有关PHP返回数组中的当前元素,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。获取PHP数组中的当前元素php为访问和操作数组提供了多种方法,其中包括获取数组中的当前元素。以下介绍几种常用的技术:1.current()函数current()函数返回数组内部指针当前指向的元素。指针最初指向数组的第一个元素。使用以下语法:$currentElement=current($array);2.key()函数key()函数返回数组内部指针当前指向元

如何使用forEach函数遍历对象的属性?如何使用forEach函数遍历对象的属性?Nov 18, 2023 pm 06:10 PM

如何使用forEach函数遍历对象的属性?在JavaScript中,我们经常需要对对象的属性进行遍历操作。如果你想使用一种简洁的方法来遍历对象的属性,forEach函数是一个非常好的选择。在本文中,我们将介绍如何使用forEach函数来遍历对象的属性,并提供具体的代码示例。首先,让我们来了解一下forEach函数的基本用法。forEach函数是Java

foreach和for循环的区别是什么foreach和for循环的区别是什么Jan 05, 2023 pm 04:26 PM

区别:1、for通过索引来循环遍历每一个数据元素,而forEach通过JS底层程序来循环遍历数组的数据元素;2、for可以通过break关键词来终止循环的执行,而forEach不可以;3、for可以通过控制循环变量的数值来控制循环的执行,而forEach不行;4、for在循环外可以调用循环变量,而forEach在循环外不能调用循环变量;5、for的执行效率要高于forEach。

php foreach循环中怎么获取当前元素的索引值php foreach循环中怎么获取当前元素的索引值Mar 23, 2023 am 09:17 AM

在PHP中,foreach语句被广泛用于对数组和对象进行遍历。在进行循环的过程中,我们有时需要获取当前循环到第几个元素。本文将为大家介绍怎样在PHP foreach循环中获取当前元素的索引值。

PHP中break关键字的作用和用法详解PHP中break关键字的作用和用法详解Jun 28, 2023 pm 06:39 PM

PHP中break关键字的作用和用法详解在PHP编程中,break是一种控制流语句,用于中断当前循环或switch语句,并跳出该循环或switch。本文将详细介绍break关键字的作用和用法。一、循环中的break在循环结构中,break的作用是提前终止循环,跳出循环体执行循环后的代码。常见的循环结构有for、while和do...while。在for循环中

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.