zoukankan      html  css  js  c++  java
  • 微信config

    <?php
    class share{
    
        private $wxuser = '';
        public $error = array();
    
        function __construct($wxuser) {
            $this->wxuser = $wxuser;    
        }
    
        public function getSgin($access_token) 
        {
            //$wxuser = getCache('wx_'.$this->_mid.'_user');
            $filename = PIGCMS_PATH.'upload/token/share_wxinfo.php';
            
            //var_dump($wxuser);die;
            if (!file_exists($wxuser)) {
                if (empty($wxuser) || !isset($wxuser['appid'])) {
                $wxuser1 = array(
                    'appid' => $this->wxuser['appid'],
                    'appsecret' => $this->wxuser['appSecret'],
                    'share_ticket' => '',
                    'share_dated' => 0
                );
                $str="<?php 
     return ".var_export($wxuser1,true).';';
                file_put_contents($filename,$str);
               }
            }
            $wxuser = include $filename;
            //var_dump($wxuser);die;
            $now = time();
            if($wxuser['share_ticket'] == '' || (file_exists($wxuser) && (time()-filemtime($wxuser) > 5000))){
                //echo 1;die;
                $ticketData = $this->getTicket($access_token);
                if ($ticketData['errcode'] > 0) {
                    $errorarr['ticket_error'] = array('errcode' => $ticketData['errcode'], 'errmsg' => $ticketData['errmsg']);
                } else {
                    $wxuser1['share_ticket'] = $ticketData['ticket'];
                    $wxuser1['share_dated'] = $now + $ticketData['expires_in'];
    
                    file_put_contents($wxuser,$str);
                    $str="<?php 
     return ".var_export($wxuser1,true).';';
                    file_put_contents($filename,$str);
                    //setCache('wx_'.$this->_mid.'_user', $wxuser);
                    $ticket = $ticketData['ticket'];
                }
            } else {
                $ticket = $wxuser['share_ticket'];
            }
            $url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
            //echo $url;die;
            $sign_data = $this->addSign($ticket, $url);
            return $sign_data;
        }
    
    
        public function addSign($ticket, $url) {
            $timestamp = time();
            $nonceStr = rand(100000, 999999);
            $array = array(
                "noncestr" => $nonceStr,
                "jsapi_ticket" => $ticket,
                "timestamp" => $timestamp,
                "url" => $url,
            );
            
            ksort($array);
            $signPars = '';
            
            foreach ($array as $k => $v) {
                if ("" != $v && "sign" != $k) {
                    if ($signPars == '') {
                        $signPars .= $k . "=" . $v;
                    } else {
                        $signPars .= "&" . $k . "=" . $v;
                    }
                }
            }
            
            $result = array(
                'appId' => $this->wxuser['appid'],
                'timestamp' => $timestamp,
                'nonceStr' => $nonceStr,
                'url' => $url,
                'signature' => SHA1($signPars),
            );
            
            return $result;
        }
    
        public function getToken()
        {
            //getToken
              $filename = PIGCMS_PATH.'/upload/token/accesstoken';
           // echo $filename;die;
            if(!file_exists($filename) || (file_exists($filename) && (time()-filemtime($filename)) > 5000)){
              //1.url地址
              $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->wxuser['appid'].'&secret='.$this->wxuser['appSecret'];
              //2.判断是否为post请求
              //3.发送请求
              $content = $this->post($url);
              //4.处理返回值
              //返回数据格式为json,php不可以直接操作json格式,需要json_decode转化一下
              $content = json_decode($content);
              $access_token = $content->access_token;
              //把access_token保存到文件
              file_put_contents($filename, $access_token);
            }
            //如果没有过期,那么就去读取缓存文件里的access_token
            else{
              $access_token = file_get_contents($filename);
            }
            //把access_token返回
              return $access_token;
        }
    
     public function post($url)
     {
        // 创建curl对象
        $ch = curl_init ();
        // 配置这个对象
        curl_setopt ($ch, CURLOPT_URL, $url);  // 请求的URL地址
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // 对认证证书来源的检查
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // 从证书中检查SSL加密算法是否存在
    
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);  // 返回接口的结果,而不是输出
    
        // 发出请求
        $data = curl_exec ( $ch );
        if(curl_errno($ch))
        {
            return 'error'.curl_error($ch);
        }
        // 关闭对象
        curl_close ( $ch );
        // 返回数据
        return $data;
        }
    
         public function getTicket($token) {
            $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" . $token . "&type=jsapi";
            return $this->https_request($url);
        }
        
    
         protected function https_request($url, $data = null,$noprocess=false) {
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
            curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:39.0) Gecko/20100101 Firefox/39.0");
            $header = array("Accept-Charset: utf-8");
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
            //curl_setopt($curl, CURLOPT_SSLVERSION, 3);
            curl_setopt($curl, CURLOPT_HTTPHEADER, $header); /* * *$header 必须是一个数组** */
            curl_setopt($curl, CURLOPT_HEADER, FALSE);
            curl_setopt($curl, CURLINFO_HEADER_OUT, true);
            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);
            if($noprocess) return $output;
            $errorno = curl_errno($curl);
            if ($errorno) {
                return array('curl' => false, 'errorno' => $errorno);
            } else {
                $res = json_decode($output, 1);
                if ($res['errcode']) {
                    return array('errcode' => $res['errcode'], 'errmsg' => $res['errmsg']);
                } else {
                    return $res;
                }
            }
            curl_close($curl);
        }
    }
  • 相关阅读:
    面向对象进阶
    20191011作业
    2019.10.10作业
    类的继承
    面向对象
    2019.10.09作业
    pandas模块
    [BZOJ 2190][SDOI2008]仪仗队(欧拉函数)
    [BZOJ 2729][HNOI2012]排队(组合数学+高精)
    [BZOJ 1491][NOI2007]社交网络(Floyd)
  • 原文地址:https://www.cnblogs.com/lujiang/p/8397985.html
Copyright © 2011-2022 走看看