zoukankan      html  css  js  c++  java
  • 微信公众号接口相关操作

    <?php
    /**
    * 微信公众号接口相关操作
    */
    class WeChat
    {
    private $_appid;
    private $_appsecret;
    private $_token;
    public function __construct($_appid,$_appsecret,$_token)
    {
    $this->_appid=$_appid;
    $this->_appsecret=$_appsecret;
    $this->_token=$_token;
    }
    //获得微信通信token
    public function requestUrl($curl,$https=true,$method='GET',$data=null)
    {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $curl);
    curl_setopt($ch, CURLOPT_HEADER, false);//这里不需要头信息只要内容
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    if($https){
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);//是否进行服务器主机验证
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,2);//是否进行https证书检查(2仅校验是否有CN字段)
    }
    if($method == 'POST'){
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
    $content = curl_exec($ch);
    curl_close($ch);
    return $content;
    }
    //获取token
    public function getAccessToken()
    {
    $file = './wxaccesstoken.json';//保存到文件不用每次都请求接口
    if(is_file($file)){
    $content = file_get_contents($file);
    $content = json_decode($content);
    if(time() - filemtime($file) < $content->expires_in){
    return $content->access_token;
    }
    }
    $curl='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->_appid.'&secret='.$this->_appsecret;
    $content = $this->requestUrl($curl);
    file_put_contents($file, $content);
    $content = json_decode($content);
    return $content->access_token;
    }
    //获取ticket
    public function getTicket($sceneid,$type='temp',$expire_seconds=604800)
    {
    if($type == 'temp'){//临时二维码
    $data = '{"expire_seconds": %s, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": %s}}}';
    $data = sprintf($data,$expire_seconds,$sceneid);
    }else{//永久二维码
    $data = '{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": %s}}}';
    $data = sprintf($data,$sceneid);
    }
    $curl = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token='.$this->getAccessToken();
    $content = $this->requestUrl($curl,true,'POST',$data);
    $content = json_decode($content);
    return $content->ticket;
    }
    //获取二微码
    public function getQRCode($sceneid,$type='temp',$expire_seconds=604800){
    $ticket = $this->getTicket($sceneid,$type,$expire_seconds);
    $content = $this->requestUrl('https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='.urlencode($ticket));
    return $content;
    }
    }
     
    //公众账号测试接口可免费申请https://mp.weixin.qq.com/wiki (下面请自行修改)
    $wechat = new wechat('wx487a542e3e7f3b84','5cddf41d16111222e321b48e213875b8','');
    //1. 获得token
    //echo $wechat->getAccessToken();
    //2. 获得二维码
    header('Content-type:image/jpeg');
    echo $wechat->getQRCode(123);
     
    ?>
  • 相关阅读:
    关于c语言变量的内存分布测试程序
    常用ARM指令集及汇编_破解
    CPU读取内存0x30000000地址4个字节数据
    打印十六进制字符串查看内存地址
    JZ2440串口打印字符作为调试
    搭建Linux3.4.2内核编辑环境
    网卡驱动程序
    同步互斥阻塞
    poll机制分析[转]
    安装、配置、启动FTP、SSH或NFS服务
  • 原文地址:https://www.cnblogs.com/samphp/p/8576065.html
Copyright © 2011-2022 走看看