微信公衆平臺自定義菜單及高級接口PHP SDK

本文介紹介紹微信公衆平臺自定義菜單及高級接口的PHP SDK及使用方法。

/*
    方倍工作室 http://www.cnblogs.com/txw1958/
    CopyRight 2014 www.doucube.com All Rights Reserved
*/


class class_weixin_adv
{
    var $appid = "";
    var $appsecret = "";


    //構造函數,獲取Access Token
    public function __construct($appid = NULL, $appsecret = NULL)
    {
        if($appid){
            $this->appid = $appid;
        }
        if($appsecret){
            $this->appsecret = $appsecret;
        }


        //hardcode
        $this->lasttime = 1395049256;
        $this->access_token = "nRZvVpDU7LxcSi7GnG2LrUcmKbAECzRf0NyDBwKlng4nMPf88d34pkzdNcvhqm4clidLGAS18cN1RTSK60p49zIZY4aO13sF-eqsCs0xjlbad-lKVskk8T7gALQ5dIrgXbQQ_TAesSasjJ210vIqTQ";


        if (time() > ($this->lasttime + 7200)){
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appid."&secret=".$this->appsecret;
            $res = $this->https_request($url);
            $result = json_decode($res, true);
            //save to Database or Memcache
            $this->access_token = $result["access_token"];
            $this->lasttime = time();
        }
    }


    //獲取關注者列表
    public function get_user_list($next_openid = NULL)
    {
        $url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=".$this->access_token."&next_openid=".$next_openid;
        $res = $this->https_request($url);
        return json_decode($res, true);
    }


    //獲取用戶基本信息
    public function get_user_info($openid)
    {
        $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$this->access_token."&openid=".$openid."&lang=zh_CN";
        $res = $this->https_request($url);
        return json_decode($res, true);
    }


    //創建菜單
    public function create_menu($data)
    {
        $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$this->access_token;
        $res = $this->https_request($url, $data);
        return json_decode($res, true);
    }


    //發送客服消息,已實現發送文本,其他類型可擴展
    public function send_custom_message($touser, $type, $data)
    {
        $msg = array('touser' =>$touser);
        switch($type)
        {
            case 'text':
                $msg['msgtype'] = 'text';
                $msg['text']    = array('content'=> urlencode($data));
                break;
        }
        $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$this->access_token;
        return $this->https_request($url, urldecode(json_encode($msg)));
    }


    //生成參數二維碼
    public function create_qrcode($scene_type, $scene_id)
    {
        switch($scene_type)
        {
            case 'QR_LIMIT_SCENE': //永久
                $data = '{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": '.$scene_id.'}}}';
                break;
            case 'QR_SCENE':       //臨時
                $data = '{"expire_seconds": 1800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": '.$scene_id.'}}}';
                break;
        }
        $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$this->access_token;
        $res = $this->https_request($url, $data);
        $result = json_decode($res, true);
        return "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".urlencode($result["ticket"]);
    }
    
    //創建分組
    public function create_group($name)
    {
        $data = '{"group": {"name": "'.$name.'"}}';
        $url = "https://api.weixin.qq.com/cgi-bin/groups/create?access_token=".$this->access_token;
        $res = $this->https_request($url, $data);
        return json_decode($res, true);
    }
    
    //移動用戶分組
    public function update_group($openid, $to_groupid)
    {
        $data = '{"openid":"'.$openid.'","to_groupid":'.$to_groupid.'}';
        $url = "https://api.weixin.qq.com/cgi-bin/groups/members/update?access_token=".$this->access_token;
        $res = $this->https_request($url, $data);
        return json_decode($res, true);
    }
    
    //上傳多媒體文件
    public function upload_media($type, $file)
    {
        $data = array("media"  => "@".dirname(__FILE__).'\\'.$file);
        $url = "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=".$this->access_token."&type=".$type;
        $res = $this->https_request($url, $data);
        return json_decode($res, true);
    }


    //https請求(支持GET和POST)
    protected function https_request($url, $data = null)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }
}


調用方法:

初始化對象

$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"));


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章