• 微信公众平台自定义菜单及高级接口PHP SDK


    本文介绍介绍微信公众平台自定义菜单及高级接口的PHP SDK及使用方法。

    作者

    方倍工作室

    修正记录:

    2014.05.03 v1.0

    方倍工作室 http://www.cnblogs.com/txw1958/

    SDK 源码:

      1 /*
      2     方倍工作室 http://www.cnblogs.com/txw1958/
      3     CopyRight 2014 www.doucube.com All Rights Reserved
      4 */
      5 
      6 class class_weixin_adv
      7 {
      8     var $appid = "";
      9     var $appsecret = "";
     10 
     11     //构造函数,获取Access Token
     12     public function __construct($appid = NULL, $appsecret = NULL)
     13     {
     14         if($appid){
     15             $this->appid = $appid;
     16         }
     17         if($appsecret){
     18             $this->appsecret = $appsecret;
     19         }
     20 
     21         //hardcode
     22         $this->lasttime = 1395049256;
     23         $this->access_token = "nRZvVpDU7LxcSi7GnG2LrUcmKbAECzRf0NyDBwKlng4nMPf88d34pkzdNcvhqm4clidLGAS18cN1RTSK60p49zIZY4aO13sF-eqsCs0xjlbad-lKVskk8T7gALQ5dIrgXbQQ_TAesSasjJ210vIqTQ";
     24 
     25         if (time() > ($this->lasttime + 7200)){
     26             $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appid."&secret=".$this->appsecret;
     27             $res = $this->https_request($url);
     28             $result = json_decode($res, true);
     29             //save to Database or Memcache
     30             $this->access_token = $result["access_token"];
     31             $this->lasttime = time();
     32         }
     33     }
     34 
     35     //获取关注者列表
     36     public function get_user_list($next_openid = NULL)
     37     {
     38         $url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=".$this->access_token."&next_openid=".$next_openid;
     39         $res = $this->https_request($url);
     40         return json_decode($res, true);
     41     }
     42 
     43     //获取用户基本信息
     44     public function get_user_info($openid)
     45     {
     46         $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$this->access_token."&openid=".$openid."&lang=zh_CN";
     47         $res = $this->https_request($url);
     48         return json_decode($res, true);
     49     }
     50 
     51     //创建菜单
     52     public function create_menu($data)
     53     {
     54         $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$this->access_token;
     55         $res = $this->https_request($url, $data);
     56         return json_decode($res, true);
     57     }
     58 
     59     //发送客服消息,已实现发送文本,其他类型可扩展
     60     public function send_custom_message($touser, $type, $data)
     61     {
     62         $msg = array('touser' =>$touser);
     63         switch($type)
     64         {
     65             case 'text':
     66                 $msg['msgtype'] = 'text';
     67                 $msg['text']    = array('content'=> urlencode($data));
     68                 break;
     69         }
     70         $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$this->access_token;
     71         return $this->https_request($url, urldecode(json_encode($msg)));
     72     }
     73 
     74     //生成参数二维码
     75     public function create_qrcode($scene_type, $scene_id)
     76     {
     77         switch($scene_type)
     78         {
     79             case 'QR_LIMIT_SCENE': //永久
     80                 $data = '{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": '.$scene_id.'}}}';
     81                 break;
     82             case 'QR_SCENE':       //临时
     83                 $data = '{"expire_seconds": 1800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": '.$scene_id.'}}}';
     84                 break;
     85         }
     86         $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$this->access_token;
     87         $res = $this->https_request($url, $data);
     88         $result = json_decode($res, true);
     89         return "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".urlencode($result["ticket"]);
     90     }
     91     
     92     //创建分组
     93     public function create_group($name)
     94     {
     95         $data = '{"group": {"name": "'.$name.'"}}';
     96         $url = "https://api.weixin.qq.com/cgi-bin/groups/create?access_token=".$this->access_token;
     97         $res = $this->https_request($url, $data);
     98         return json_decode($res, true);
     99     }
    100     
    101     //移动用户分组
    102     public function update_group($openid, $to_groupid)
    103     {
    104         $data = '{"openid":"'.$openid.'","to_groupid":'.$to_groupid.'}';
    105         $url = "https://api.weixin.qq.com/cgi-bin/groups/members/update?access_token=".$this->access_token;
    106         $res = $this->https_request($url, $data);
    107         return json_decode($res, true);
    108     }
    109     
    110     //上传多媒体文件
    111     public function upload_media($type, $file)
    112     {
    113         $data = array("media"  => "@".dirname(__FILE__).'\'.$file);
    114         $url = "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=".$this->access_token."&type=".$type;
    115         $res = $this->https_request($url, $data);
    116         return json_decode($res, true);
    117     }
    118 
    119     //https请求(支持GET和POST)
    120     protected function https_request($url, $data = null)
    121     {
    122         $curl = curl_init();
    123         curl_setopt($curl, CURLOPT_URL, $url);
    124         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    125         curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    126         if (!empty($data)){
    127             curl_setopt($curl, CURLOPT_POST, 1);
    128             curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    129         }
    130         curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    131         $output = curl_exec($curl);
    132         curl_close($curl);
    133         return $output;
    134     }
    135 }

    方倍工作室 http://www.cnblogs.com/txw1958/

    调用方法:

    初始化对象

    $weixin = new class_weixin_adv("wx6222221b11111111", "3079cb22ad383ae7371d12aed1b2d0cc");

    查看Access Token

    var_dump($weixin->access_token);

    创建二维码

    var_dump($weixin->create_qrcode("QR_SCENE", "134324234"));

    获取关注者列表

    var_dump($weixin->get_user_list());

    获取用户信息

    $openid = "oLVPpjkttuZTbwDwN7vjHNlqsmPs";
    var_dump($weixin->get_user_info($openid));

    创建菜单

    $data ='{"button":[{"name":"方倍工作室","sub_button":[{"type":"click","name":"公司简介","key":"公司简介"},{"type":"click","name":"社会责任","key":"社会责任"},{"type":"click","name":"联系我们","key":"联系我们"}]},{"name":"产品服务","sub_button":[{"type":"click","name":"微信平台","key":"微信平台"},{"type":"click","name":"微博应用","key":"微博应用"},{"type":"click","name":"手机网站","key":"手机网站"}]},{"name":"技术支持","sub_button":[{"type":"click","name":"文档下载","key":"文档下载"},{"type":"click","name":"技术社区","key":"技术社区"},{"type":"click","name":"服务热线","key":"服务热线"}]}]}';
    var_dump($weixin->create_menu($data));

    用户分组 方倍工作室 http://www.cnblogs.com/txw1958/

    var_dump($weixin->create_group("老师"));
    var_dump($weixin->update_group($openid, "100"));

    上传下载多媒体

    var_dump($weixin->upload_media("image","pondbay.jpg"));

    发送客服消息

    var_dump($weixin->send_custom_message($openid, "text", "asdf"));

     

    =========================================================

    方倍工作室微信公众平台账号关注方法:
    1. 微信通讯录-添加朋友-查找公众号-搜索“方倍工作室”
    2. 微信通讯录-添加朋友-搜号码-输入“pondbaystudio”
    3. 使用微信扫描下面的二维码

  • 相关阅读:
    VueJS promise
    VueJS vue-router-keep-alive
    VueJS电商管理系统(Element-UI)B站学习笔记
    VueJS 对象更新
    CSS 类名选择器
    VueJS 全局导航守卫
    VueJS v-model 双向绑定
    JS 高阶函数 filter/map/reduce
    VueJS filters过滤器
    UVA1601-双向广度优先搜索
  • 原文地址:https://www.cnblogs.com/lanzhi/p/6467496.html
Copyright © 2020-2023  润新知