您好,欢迎来到互劦cms 官方网站-内容管理系统,让你体验不一样的建站方式,更简单,更安全,更灵活
  • 登陆-注册
  • 使用手册
  • 常见问题
  • 全国服务热线:18173153117

    软件名称:微信自定义开发demo
    软件大小: 7.0kb
    支持系统:iis/apache/nginx
    下载地址:本地下载
    代码解析:
    1. include("weixin.php");
    2. require_once 'hyphp/config.ini.php';//调用自定义配置文件
    3. define("ACCOUNT", "");//微信公众平台的用户名
    4. define("PASSWORD", "");//微信公众平台的密码
    5. define("TOKEN", "");//开发模式的TOKEN码
    6. define('DEBUG', true);//是否开启调试模式
    7. $param = array('username'=>'微信公众平台的用户名', 'pwd'=>'微信公众平台的密码');
    8. $weixin = new Weixin(TOKEN,DEBUG);
    9. if($weixin->login($param)){
    10.   你可以猥琐欲为了,哈哈哈哈
    11. }
    12. else{
    13.   配置信息不对,别想模拟登录
    14. }
    复制代码
    1. 如果你需要自定义菜单,就生成吧,我写在这里做了注释,因为一般只需要生成一次即可,除非你老是修改菜单,生成后,不会立即显示,因为有24小时的缓存,除非你取消关注,然后重新关注即可!
    2. /*生成自定义菜单开始*/
    3.     /*$xjson = '{ 
    4.      "button":[
    5.          {
    6.                "name":"篮球",
    7.                "sub_button":[
    8.                     {
    9.                        "type":"click",
    10.                        "name":"nba",
    11.                        "key":"V1001_NBA"
    12.                     },
    13.                     {
    14.                        "type":"click",
    15.                        "name":"cba",
    16.                        "key":"V1001_CBA"
    17.                     }
    18.                 ]
    19.            },
    20.            {
    21.                "name":"体育",
    22.                "sub_button":[
    23.                     {
    24.                        "type":"click",
    25.                        "name":"足球",
    26.                        "key":"V1001_ZUQIU"
    27.                     },
    28.                     {
    29.                        "type":"click",
    30.                        "name":"排球",
    31.                        "key":"V1001_PAIQIU"
    32.                     },
    33.                     {
    34.                        "type":"click",
    35.                        "name":"网球",
    36.                        "key":"V1001_WANGQIU"
    37.                     },
    38.                     {
    39.                        "type":"click",
    40.                        "name":"乒乓球",
    41.                        "key":"V1001_PPQ"
    42.                     },
    43.                     {
    44.                        "type":"click",
    45.                        "name":"台球",
    46.                        "key":"V1001_TAIQIU"
    47.                     }
    48.                 ]
    49.            },
    50.            {
    51.                "name":"新闻",
    52.                "sub_button":[
    53.                     {
    54.                        "type":"click",
    55.                        "name":"国内新闻",
    56.                        "key":"V1001_GNNEWS"
    57.                     },
    58.                     {
    59.                        "type":"click",
    60.                        "name":"国际新闻",
    61.                        "key":"V1001_GJNEWS"
    62.                     },
    63.                     {
    64.                        "type":"click",
    65.                        "name":"地方新闻",
    66.                        "key":"V1001_AREANEWS"
    67.                     },
    68.                     {
    69.                        "type":"click",
    70.                        "name":"家庭新闻",
    71.                        "key":"V1001_HOMENEWS"
    72.                     }
    73.                 ]
    74.            }
    75.        ]
    76.     }';
    77.     $jsonMenu = json_encode($xjson);
    78.     $get_url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=开发者模式中的AppId串&secret=开发者模式中的AppSecret串';
    79.     $get_return = file_get_contents($get_url);
    80.     $get_return = (array)json_decode($get_return);
    81.     if( !isset($get_return['access_token']) ){exit( '获取access_token失败!' );}
    82.     $post_url = 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token='.$get_return['access_token'];
    83.     $ch = curl_init($post_url);
    84.     curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    85.     curl_setopt($ch, CURLOPT_POSTFIELDS,$xjson);
    86.     curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
    87.     curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    88.     'Content-Type: application/json',
    89.     'Content-Length: ' . strlen($xjson))
    90.     );
    91.     $respose_data = curl_exec($ch);
    92.     echo $respose_data;exit;*/
    93. /*生成自定义菜单结束*/
    复制代码
    1. 你会看懂的,点击菜单事件回复,文本消息回复,智能语音识别
    2. $weixin->getMsg();
    3. $type = $weixin->msgtype; //消息类型
    4. $username = $weixin->msg['FromUserName'];//哪个用户给你发的消息,这个$username是微信加密之后的,每个用户都是一一对应的
    5. if ($type === 'event') {//点击菜单事件
    6.     $eventkey = $weixin->eventkey;//获取当前菜单key
    7.     if($eventkey=='V1001_NBA') {
    8.         $hongye_bqq = '您点击的NBA菜单,哈哈';
    9.     }
    10.     if($eventkey=='V1001_CBA') {
    11.         $hongye_bqq = '您点击的CBA菜单,哈哈';
    12.     }
    13.     else{
    14.         $hongye_bqq = '欢迎关注bqq!';
    15.     }
    16.  }
    17. if ($type === 'text') {//文本输入
    18.     $kwds=$weixin->msg['Content'];
    19.     $sql="select * from `wx_text_msg` where `question` like '%$kwds%' ";
    20.     $res=getOne($sql);
    21.     if($res) {
    22.         $reply = $weixin->makeText($res['answer']);
    23.     }
    24.     else{
    25.         $reply = $weixin->makeText('抱歉,根据您输入的文本,暂时未找到相关匹配信息');
    26.     }
    27. }
    28. if ($type === 'voice') {//语音输入
    29.     $kwds = substr($weixin->msg['Recognition'],0,-3);
    30.     $sql="select * from `wx_voice_msg` where `question` like '%$kwds%' ";//根据语音输入关键词,查询数据表匹配答案
    31.     $res=getOne($sql);
    32.     if($res) {
    33.         $reply = $weixin->makeText($res['answer']);
    34.     }
    35.     else{
    36.         $reply = $weixin->makeText('抱歉,根据您输入的语音,暂时未找到相关匹配信息');
    37.     }
    38. }
    39. $weixin->reply($reply);

    站内地图

    Copyright 2018-2028 hlcms. All Rights Reserved. 湘ICP备13008873号-1