国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

首頁 微信小程序 微信開發(fā) 微信現(xiàn)金紅包接口實現(xiàn)紅包發(fā)放的示例代碼分享

微信現(xiàn)金紅包接口實現(xiàn)紅包發(fā)放的示例代碼分享

Apr 13, 2017 am 10:30 AM

微信現(xiàn)金紅包接口實現(xiàn)紅包發(fā)放:

一:流程:【

流程:微信用戶訪問紅包活動頁面--》后端判斷是否是微信訪問的

【否:提示用微信打開連接,是:提示用戶是否授權(quán)允許,獲取其的用戶信息【openID等信息】】--》

進入紅包活動頁面---》用戶點擊領(lǐng)取紅包【判斷是否領(lǐng)取過紅包】是:【提示已領(lǐng)取過紅包】否?

--》后端程序調(diào)用接口發(fā)放紅包

--》微信用戶在微信中收到紅包

--》打開紅包

--》紅包金額會添加到錢包零錢里

--》完成紅包發(fā)放。

二:要實現(xiàn)微信現(xiàn)金紅包接口實現(xiàn)紅包發(fā)放,首先要符合以下條件:

1.用于發(fā)放紅包的微信公眾號要是服務(wù)類型

2.登錄微信公眾平臺官網(wǎng)后,在公眾平臺后臺管理頁面 - 開發(fā)者中心頁,

點擊“修改配置”按鈕,填寫服務(wù)器地址(URL)、Token和EncodingAESKey,

其中URL是開發(fā)者用來接收微信消息和事件的接口URL。Token可由開發(fā)者可以任意填寫,

用作生成簽名(該Token會和接口URL中包含的Token進行比對,從而驗證安全性)。

EncodingAESKey由開發(fā)者手動填寫或隨機生成,將用作消息體加解密密鑰。

3.獲取access_token:公眾號可以使用AppID和AppSecret調(diào)用本接口來獲取access_token。

【AppID和AppSecret可在微信公眾平臺官網(wǎng)-開發(fā)者中心頁中獲得(需要已經(jīng)成為開發(fā)者,且?guī)ぬ枦]有異常狀態(tài))?!?/p>

4.微信公眾號要開通 “網(wǎng)頁授權(quán)接口” 用戶獲取用戶基本信息【特別是openID ,發(fā)紅包時用到】

5.微信公眾號的 “微信支付“ ?中的 ?”商戶號” 要開通微信支付【發(fā)紅包的金額是該支付賬戶扣款】,開通“現(xiàn)金紅包”接口【調(diào)用該接口發(fā)放紅包】。

6. 登陸 ”商戶號”【微信公眾號分配的商戶號。第5 中有說明】 在 “API安全” 中 下載PHP版的 證書 【.pem格式】

以上如描述不清楚,請查看 微信開發(fā)者文檔 里面有詳細的秒殺。

部分代碼如下【其余的請查看附件】:

<?php
namespace RedClient\Controller;
use Think\Controller;
use RedClient\Redpack\WeiXinInfo;
use RedClient\Redpack\Oauth;
use RedClient\Redpack\SendRedPack;
use RedClient\Redpack\CreateRedPack;
class IndexController extends Controller 
    {
        public function index()
            {
                $this->isWeixin();//是否是微信打開
                if($this->access)
                    {
                        $this->display();
                         
                    }
                    else
                        {
                            $class=new Oauth();
                            $class->index(&#39;http://www.myweb.com/index.php/Index/oauth&#39;);
                        }
            }
         
        //獲取用戶信息 openID
        public function oauth()
            {
                $code=isset($_GET[&#39;code&#39;])?strip_tags(trim($_GET[&#39;code&#39;])):&#39;&#39;;
                $state=isset($_GET[&#39;state&#39;])?strip_tags(trim($_GET[&#39;state&#39;])):&#39;&#39;;
                 
                $class=new Oauth();
                $userInfo=$class->getCode($code,$state);//獲取用戶信息
                if(!empty($userInfo[&#39;data&#39;]))
                    {
                        //$model=M(&#39;wxuser&#39;);
                        //$result=$model->where(&#39;openid = "&#39;.$userInfo[&#39;data&#39;]->openid.&#39;"&#39;)->field(&#39;openid&#39;)->select();
                        if(empty($result))
                            {
                                $userInfo[&#39;data&#39;]=$this->object2array($userInfo[&#39;data&#39;]);
                                $model->data($userInfo[&#39;data&#39;])->add();
                                 
                            }
                    }
                $userInfo[&#39;data&#39;]=$this->object2array($userInfo[&#39;data&#39;]);
                 
                //創(chuàng)建紅包
                $class=new CreateRedPack();
                $red=$class->redval();
                 
                //發(fā)紅包
                $class=new SendRedPack();
                $configs=array(
                        &#39;send_name&#39;=>&#39;紅包發(fā)送者名稱&#39;,//紅包發(fā)送者名稱
                        &#39;re_openid&#39;=>$userInfo[&#39;data&#39;][&#39;openid&#39;],//接受紅包的用戶,用戶在wxappid下的openid
                        &#39;total_amount&#39;=>$red,//付款金額,單位分
                        &#39;total_num&#39;=>&#39;1&#39;,//紅包發(fā)放總?cè)藬?shù)
                        &#39;wishing&#39;=>&#39;紅包祝福語&#39;,//紅包祝福語
                        &#39;client_ip&#39;=>$_SERVER[&#39;SERVER_ADDR&#39;],//調(diào)用接口的機器Ip地址
                        &#39;act_name&#39;=>&#39;活動名稱&#39;,//活動名稱
                        &#39;remark&#39;=>&#39;備注信息&#39;,//  備注信息
                    );
                $class->setFields($configs);
                $result=$class->requestXml();
                 
                //微信返回信息處理
                if(strtoupper($result[&#39;return_code&#39;])==&#39;SUCCESS&#39;)
                    {
                        if(strtoupper($result[&#39;result_code&#39;])==&#39;SUCCESS&#39;)
                            {
                                //紅包發(fā)送成功!
                                                         
                                $datas[&#39;flag&#39;]=1;
                                echo $result[&#39;err_code_des&#39;];
                            }
                            else
                                {
                                    //紅包發(fā)送失敗
                                    $datas[&#39;flag&#39;]=0;
                                    $datas[&#39;msg&#39;]=$result[&#39;err_code_des&#39;];
                                    echo $result[&#39;err_code_des&#39;];
                                }
                    }
                    else
                        {
                            //紅包發(fā)送失敗
                            $datas[&#39;flag&#39;]=0;
                            $datas[&#39;msg&#39;]=$result[&#39;err_code_des&#39;];
                            echo $result[&#39;err_code_des&#39;];
                        }
            }
         
        public function isWeixin()
            {
                if (strpos($_SERVER[&#39;HTTP_USER_AGENT&#39;], &#39;MicroMessenger&#39;) !== false) 
                    { 
                        $this->access=true; 
                    } 
                return false; 
            }
         
        //類轉(zhuǎn)換成數(shù)組
        public function object2array($object) 
            {
              if (is_object($object)) 
                  {
                    foreach ($object as $key => $value) 
                        {
                          $array[$key] = $value;
                        }
                  }
                  else 
                      {
                        $array = $object;
                      }
              return $array;
            }
             
    }
    ?>
<?php
namespace RedClient\Redpack;
/**
    簡單紅包算法類
**/
class CreateRedPack
    {
        private $rid=0;//當(dāng)前紅包隨機數(shù)
        private $rand_arr=array();//35%是1.0到1.1 紅包隨機數(shù)
        private $rand_arr1=array();//35%是1.1到1.2 紅包隨機數(shù)
        private $rand_arr2=array();//30%是1.2到1.95 紅包隨機數(shù)
        private $red=0;//紅包金額
        private $simplered=0;
        private $red_config=array(
                                array(&#39;min&#39;=>1.0,&#39;max&#39;=>1.1),
                                array(&#39;min&#39;=>1.1,&#39;max&#39;=>1.2),
                                array(&#39;min&#39;=>1.2,&#39;max&#39;=>1.95)
                            );
                             
        public function __construct()
            {
                $this->rid=mt_rand(1,10000);//當(dāng)前紅包隨機數(shù)
                $this->rand_arr=range(1,3500);//35%是1.0到1.1
                $this->rand_arr1=range(3501,7000);//35%是1.1到1.2
                $this->rand_arr2=range(7001,10000);//30%是1.2到1.95
                $this->simplered=666;
            }
             
        public function redval()
            {
                $maxrp=$this->maxred();//隨機最大紅包金額
                if($maxrp!=$this->simplered)
                    {
                        if(in_array($this->rid,$this->rand_arr))
                            {
                                $red_val=$this->red_config[0];
                                $min=$red_val[&#39;min&#39;]*100;
                                $max=$red_val[&#39;max&#39;]*100;
                                $this->red=mt_rand($min,$max);
                                $this->red=$this->red/100;
                            }
                            elseif(in_array($this->rid,$this->rand_arr1))
                                {
                                    $red_val=$this->red_config[1];
                                    $min=$red_val[&#39;min&#39;]*100;
                                    $max=$red_val[&#39;max&#39;]*100;
                                    $red=mt_rand($min,$max);
                                    $this->red=$this->red/100;
                                }
                                elseif(in_array($this->rid,$this->rand_arr2))
                                    {
                                        $red_val=$this->red_config[2];
                                        $min=$red_val[&#39;min&#39;]*100;
                                        $max=$red_val[&#39;max&#39;]*100;
                                        $this->red=mt_rand($min,$max);
                                        $this->red=$this->red/100;
                                    }
                    }
                    else
                        {
                            $this->red=$maxrp;
                        }
                return $this->red?$this->red:1;
            }
                 
        protected function maxred()
            {
                $rid=mt_rand(1,100000);
                $rid1=mt_rand(1,100000);
                $red=0;
                $dff=$rid-$rid1;
                 
                if($dff > 0)
                    {
                        if(($rid1%$rid1)==$this->simplered)
                            {
                                $red=$this->simplered;
                            }
                    }
                    else
                        {
                            if(($rid1%$rid)==$this->simplered)
                                {
                                    $red=$this->simplered;
                                }
                        }
                         
                return $red;
            }
    }
 
?>
<?php
namespace RedClient\Redpack;
/*********
 
    先在公共平臺配置授權(quán)的域名;
    然后才能通過,
    網(wǎng)頁授權(quán)方式獲取微信用戶基本信息
     
    網(wǎng)頁授權(quán)流程分為四步: 
 
    1.引導(dǎo)用戶進入授權(quán)頁面同意授權(quán),獲取code 
    2.通過code換取網(wǎng)頁授權(quán)access_token(與基礎(chǔ)支持中的access_token不同) 
    3.如果需要,開發(fā)者可以刷新網(wǎng)頁授權(quán)access_token,避免過期 
    4.通過網(wǎng)頁授權(quán)access_token和openid獲取用戶基本信息 
 
**********/
class Oauth
    {
        protected $appid=&#39;&#39;;
        protected $redirect_uri=&#39;&#39;;
        protected $state=&#39;&#39;;//mt_rand(100,999);
        protected $appsecret=&#39;&#39;;
        protected $data=array(&#39;flag&#39;=>0,&#39;msg&#39;=>&#39;&#39;,&#39;data&#39;=>&#39;&#39;);
         
         
        public function __construct()
            {
                $this->appid=&#39;appid&#39;;//
                $this->appsecret=&#39;secret&#39;;//;
                $this->state=mt_rand(100,999);
            }
             
        //引導(dǎo)用戶訪問鏈接處理函數(shù) 
        public function index($redirect_uri)
            {
                $this->redirect_uri=urlencode($redirect_uri);//微信自動跳轉(zhuǎn)到$redirect_uri
                header(&#39;location:https://open.weixin.qq.com/connect/oauth2/authorize?appid=&#39;.$this->appid.&#39;&redirect_uri=&#39;.$this->redirect_uri.&#39;/oauth.php&response_type=code&scope=snsapi_userinfo&state=&#39;.$this->state.&#39;&connect_redirect=1#wechat_redirect&#39;);
            }
             
        public function getCode($code=&#39;&#39;,$state=&#39;&#39;,$token=&#39;&#39;,$webToken=&#39;&#39;)
            {
                $data=array(
                    &#39;errorno&#39;=>&#39;&#39;,
                    &#39;errormsg&#39;=>&#39;&#39;,
                    &#39;data&#39;=>&#39;&#39;
                );
                 
                if(empty($code))
                    {
                        $this->data[&#39;flag&#39;]=0;
                        $this->data[&#39;msg&#39;]=&#39;授權(quán)失??!&#39;;
                    }
                    else
                        {   /* 
                                 
                                1獲取webtoken
                                2獲取reflash_token
                                3獲取用戶信息 
                            */
  $token_url = &#39;https://api.weixin.qq.com/sns/oauth2/access_token?appid=&#39;.$this->appid.&#39;&secret=&#39;.$this->appsecret.&#39;&code=&#39;.$code.&#39;&grant_type=authorization_code&#39;;
                         $token = json_decode(file_get_contents($token_url));
                             
                            if (isset($token->errcode)) 
                                {
                                    $data[&#39;errorno&#39;]=$token->errcode;
                                    $data[&#39;errormsg&#39;]=$token->errmsg;
                                }
                                else
                                    {
  $access_token_url = &#39;https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=&#39;.$this->appid.&#39;&grant_type=refresh_token&refresh_token=&#39;.$token->refresh_token;
                                        //轉(zhuǎn)成對象
                                        $accessToken = json_decode(file_get_contents($access_token_url));
                                         
                                        if (isset($accessToken->errcode)) 
                                            {
                                                $data[&#39;errorno&#39;]=$token->errcode;
                                                $data[&#39;errormsg&#39;]=$token->errmsg;
                                            }
                                            else
                                                {
     $user_info_url = &#39;https://api.weixin.qq.com/sns/userinfo?access_token=&#39;.$accessToken->access_token.&#39;&openid=&#39;.$accessToken->openid.&#39;&lang=zh_CN&#39;;
                                                    //轉(zhuǎn)成對象
                                                    $userInfo = json_decode(file_get_contents($user_info_url));
                                                     
                                                    if (isset($userInfo->errcode)) 
                                                        {
                                                            $data[&#39;errorno&#39;]=$token->errcode;
                                                            $data[&#39;errormsg&#39;]=$token->errmsg;
                                                        }
                                                        else
                                                            {
                                                                $data[&#39;data&#39;]=$userInfo;
                                                            }
                                                }
                                    }
                        }
                return $data;
            }
             
    }
 
?>
<?php
/******
 
    用于企業(yè)向微信用戶個人發(fā)現(xiàn)金紅包
    目前支持向指定微信用戶的openid發(fā)放指定金額紅包。
    (獲取openid參見微信公眾平臺開發(fā)者文檔:
    網(wǎng)頁授權(quán)獲取用戶基本信息)
     
****/
namespace RedClient\Redpack;
 
class SendRedPack
    {
        private $config=array(
            &#39;nonce_str&#39;=>&#39;&#39;,//隨機字符串,不長于32位
            &#39;sign&#39;=>&#39;&#39;,//簽名
            &#39;mch_billno&#39;=>&#39;&#39;,//商戶訂單號
            &#39;mch_id&#39;=>&#39;1111sdfsafsaddf&#39;,//微信支付分配的商戶號
            &#39;wxappid&#39;=>&#39;sddafdsadfdsafdsdd&#39;,//微信分配的公眾賬號ID
            &#39;send_name&#39;=>&#39;&#39;,//紅包發(fā)送者名稱
            &#39;re_openid&#39;=>&#39;&#39;,//接受紅包的用戶,用戶在wxappid下的openid
            &#39;total_amount&#39;=>&#39;&#39;,//付款金額,單位分
            &#39;total_num&#39;=>&#39;&#39;,//紅包發(fā)放總?cè)藬?shù)
            &#39;wishing&#39;=>&#39;&#39;,//紅包祝福語
            &#39;client_ip&#39;=>&#39;&#39;,//調(diào)用接口的機器Ip地址
            &#39;act_name&#39;=>&#39;&#39;,//活動名稱
            &#39;remark&#39;=>&#39;&#39;,//  備注信息
        );
         
        protected $key=&#39;&#39;;
        protected $url=&#39;https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack&#39;;
        protected $requestXml=&#39;&#39;;
         
        //設(shè)置必要參數(shù)
        public function setFields($conf=array())
            {
                foreach($conf as $k=>$v)
                    {
                        if(isset($this->config[$k]) && !empty($v))
                            {
                                $this->config[$k]=$v;
                            }
                    }
                $this->config[&#39;mch_billno&#39;]=$this->billno();
                $this->config[&#39;nonce_str&#39;]=$this->createNonceStr();
                $this->createSign($conf);
                $this->createXml();
                //echo $this->requestXml;
            }
         
        protected function billno()
            {
                return $this->config[&#39;mch_id&#39;].time().md5(mt_rand(1000,9999));
            }
         
        /* //檢查必要參數(shù)是否為空!
        public function checkConfig()
            {
                $flag=true;
                foreach($this->config as $k=>$v)
                    {
                        if(empty($v))
                            {
                                $flag=false;
                                break;
                            }
                    }
            } */
         
        //隨機字符串,不長于32位
        public function createNonceStr( $len=32 )
            {
                $strs = "abcdefghijklmnopqrstuvwxyz0123456789";
                $str ="";
                $len=$len<=32?$len:32;
                for ( $i = 0; $i < $len; $i++ )
                    {
                      $str.= substr($strs, mt_rand(0, strlen($strs)-1), 1);
                    }
                return $str;
            }
             
        //格式化參數(shù)
        public function formatParam($config=array(),$flag=1)
            {
                $format=&#39;&#39;;
                if(!empty($config))
                    {
                        ksort($config);
                        foreach($config as $k=>$v)
                            {
                                if($flag)
                                    {
                                        $v=urlencode($v);
                                    }
                                     
                                    if($flag==0 && strtolower($k)==&#39;sign&#39;)
                                        {
                                            continue;
                                        }
                                $format.=$k.&#39;=&#39;.$v.&#39;&&#39;;
                            }
                        $format=trim($format,&#39;&&#39;);
                    }
                return $format;
            }
             
        //創(chuàng)建SIGNATURE  
        protected function createSign($config)
            {
                $format=$this->formatParam($config,0);
                $format.=&#39;&key=&#39;.$this->key;
                $signature=strtoupper(md5($format));
                $this->config[&#39;sign&#39;]=$signature;
                return true;//$signature;
            }
             
        //創(chuàng)建xml格式數(shù)據(jù)
        protected function createXml()
            {
                $xml=&#39;<xml>&#39;;
                foreach($this->config as $k=>$v)
                    {
                        if(!empty($v))
                            {
                                $xml.=&#39;<&#39;.$k.&#39;><![CDATA[&#39;.$v.&#39;]]></&#39;.$k.&#39;>&#39;;
                            }
                    }
                $xml.=&#39;</xml>&#39;;
                $this->requestXml=$xml;
            }
         
        //XML格式數(shù)據(jù)轉(zhuǎn)換為數(shù)組
        public function createArray($xml=&#39;&#39;)
            {
                $arr = json_decode(json_encode(simplexml_load_string($xml, &#39;SimpleXMLElement&#39;, LIBXML_NOCDATA)), true);
                return $arr;
            }
         
        //發(fā)送紅包
        public function requestXml($timeout=30,$header=array())
            {
                $ch = curl_init();
                //超時時間
                curl_setopt($ch,CURLOPT_TIMEOUT,$timeout);
                curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch,CURLOPT_URL,$this->url);
                curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
                curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
                //默認格式為PEM,可以注釋
                curl_setopt($ch,CURLOPT_SSLCERTTYPE,&#39;PEM&#39;);
                curl_setopt($ch,CURLOPT_SSLCERT,dirname(__FILE__).&#39;/apiclient_cert.pem&#39;);//pem證書地址
                //默認格式為PEM,可以注釋
                curl_setopt($ch,CURLOPT_SSLKEYTYPE,&#39;PEM&#39;);
                curl_setopt($ch,CURLOPT_SSLKEY,dirname(__FILE__).&#39;/apiclient_key.pem&#39;);//key證書地址
                curl_setopt($ch,CURLOPT_CAINFO,&#39;PEM&#39;);
                curl_setopt($ch,CURLOPT_CAINFO,dirname(__FILE__).&#39;/rootca.pem&#39;);//CA證書地址
                //兩個文件合成一個.pem文件
                //curl_setopt($ch,CURLOPT_SSLCERT,getcwd().&#39;/all.pem&#39;);
                if( count($header) >= 1 )
                    {
                        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
                    }
                curl_setopt($ch,CURLOPT_POST, 1);
                curl_setopt($ch,CURLOPT_POSTFIELDS,$this->requestXml);
                $data = curl_exec($ch);
                var_dump($data);
                if($data)
                    {
                        curl_close($ch);
                        var_dump($data);
                        return $data;
                    }
                    else 
                        {
                            $error = curl_errno($ch);
                            echo &#39;ERROR:&#39;.$error;
                            curl_close($ch);
                            return false;
                        }
            }
         
        //返回非空值,非NULL
        public function notEmpty($val=&#39;&#39;)
            {
                $return=&#39;&#39;;
                if(null !=$val && strlen($val) != 0)
                    {
                        $return=$val;
                    }
                return $return?$return:false;
            }
         
    }
 
 
?>
<?php
namespace RedClient\Redpack;
class WeiXinInfo
    {
        private $AppID=&#39;appid&#39;;
        private $AppSecret=&#39;secret&#39;;
        private $grant_type=&#39;client_credential&#39;;
         
        private $url=&#39;https://api.weixin.qq.com/cgi-bin/token&#39;;
         
        public function __construct()
            {
                //$arr=array(&#39;access_token&#39;=>&#39;1&#39;,&#39;expires&#39;=>&#39;7200&#39;);
                 
            }
             
        //設(shè)置獲取ACCESSTOKEN 的URL
        public function setUrl()
            {
                return $this->url=$this->url.&#39;?grant_type=&#39;.$this->grant_type.&#39;&appid=&#39;.$this->AppID.&#39;&secret=&#39;.$this->AppSecret;
            }
         
        //獲取APPID,SECRET
        public function getAppid()
            {
                $sql=&#39;select appid,secret from ly_appid where flag=1&#39;;
                $model=M();
                $result=$model->query($sql);
                if(!empty($result))
                    {
                        $this->AppID=$result[0][&#39;appid&#39;];
                        $this->AppSecret=$result[0][&#39;secret&#39;];
                        return $result[0];
                    }
            }
             
        public function object2array($object) 
            {
              if (is_object($object)) 
                  {
                    foreach ($object as $key => $value) 
                        {
                          $array[$key] = $value;
                        }
                  }
                  else 
                      {
                        $array = $object;
                      }
              return $array;
            }
             
        //檢驗URL有效
        public function checkUrl()
            {
                $signature=isset($_GET[&#39;signature&#39;])?strip_tags(trim($_GET[&#39;signature&#39;])):&#39;&#39;;
                $timestamp=isset($_GET[&#39;timestamp&#39;])?strip_tags(trim($_GET[&#39;timestamp&#39;])):&#39;&#39;;
                $nonce=isset($_GET[&#39;nonce&#39;])?strip_tags(trim($_GET[&#39;nonce&#39;])):&#39;&#39;;
                $echostr=isset($_GET[&#39;echostr&#39;])?$_GET[&#39;echostr&#39;]:&#39;&#39;;
                if(!empty($signature) && !empty($timestamp) && !empty($nonce))
                    {
                        if($this->checkSign($signature,$timestamp,$nonce))
                            {
                                echo $echostr;
                                return true;
                            }
                            else
                                {
                                    return false;
                                }
                    }
            }  
             
        //驗證SIGNATURE是否有效
        private function checkSign($sign,$timestamp,$nonce)
            {
                $token=$this->getAccessToken();
                //$token=$this->object2array($token);
                $sign_arr=array($token,$timstamp,$nonce);
                sort($sign_arr,SORT_STRING);
                $signStr=implode($sign_arr);
                $signStr=sha1($signStr);
                if(strtoupper($signStr)==strtoupper($sign))
                    {
                        return true;
                    }
                return false;
            }
             
        //通過微信接口獲取微信Access_Token   
        public function getAccessToken()
            {
                $token=&#39;&#39;;
                $this->setUrl();
                $check=$this->checkToken();
                 
                if(session(&#39;?&#39;.md5(&#39;getaccesstoken&#39;)) && $check)
                    {
                        $tokens=session(md5(&#39;getaccesstoken&#39;));
                        $token=$tokens->access_token;
                    }
                    else
                        {
                            $result=file_get_contents($this->url);
                            $result=json_decode($result);
                            $token=$result->access_token;
                            $result->expires_in=$result->expires_in+time();
                            session(md5(&#39;getaccesstoken&#39;),$result);
                        }
                         
                return $token;
            }
         
        //檢查微信ACCESS_TOKEN是否有效
        public function checkToken()
            {
                if(session(&#39;?&#39;.md5(&#39;getaccesstoken&#39;)) && session(md5(&#39;getaccesstoken&#39;)))
                    {
                        $time=time();
                        $token=session(md5(&#39;getaccesstoken&#39;));
                         
                        if($token->expires_in-$time > 30)
                            {
                                return true;
                            }
                            else
                                {
                                    session(md5(&#39;getaccesstoken&#39;),null);
                                }
                    }
                return false;
            }
             
        //保存微信ACCESSTOKEN到數(shù)據(jù)庫  
        public function saveAccessToken()
            {
                $token=$this->getAccessToken();
                $sql=&#39;select `id`,`rate`,token,ex_time,createtime from ly_token where token="&#39;.$token.&#39;" where appid="&#39;.$this->AppID.&#39;" AND secret="&#39;.$this->AppSecret.&#39;"&#39;;
                $model=M();
                $result=$model->query($sql);
                if(!empty($result))
                    {
                        $time=time();
                        $expires=$time-$result[0][&#39;createtime&#39;];
                        if($result[0][&#39;ex_time&#39;]-$expires > 0)
                            {
                                return $result[0][&#39;token&#39;];                       
                            }
                            else
                                {
                                    $token=$this->getAccessToken();
                                    if(!empty($token))
                                        {
                                            $token=json_decode($token);
                                            if(!isset($token[&#39;errcode&#39;]) or !$token[&#39;errcode&#39;])
                                                {
                                                    if(isset($token[&#39;access_token&#39;]) && $token[&#39;access_token&#39;])
                                                        {
                                                            $data[&#39;access_token&#39;]=$token[&#39;access_token&#39;];
                                                            $data[&#39;createtime&#39;]=$time;
                                                            $data[&#39;ex_time&#39;]=$token[&#39;expires_in&#39;];
                                                            $data[&#39;rate&#39;]=$result[0][&#39;rate&#39;]+1;
                                                            //$sql=&#39;update ly_token set token="&#39;.$token[&#39;access_token&#39;].&#39;" ,createtime=&#39;.$time.&#39;,ex_time=&#39;.$token[&#39;expires_in&#39;].&#39; where `id`=&#39;.$result[0][&#39;id&#39;];
                                                            $model=M(&#39;token&#39;);
                                                            $update=$model->where(&#39;`id`=&#39;.$result[0][&#39;id&#39;])->save($data);
                                                            if($update)
                                                            {
                                                                return $token[&#39;access_token&#39;];
                                                            }
                                                        }
                                                }
                                                else
                                                    {//微信返回的錯誤信息
                                                                $data[&#39;errcode&#39;]=$token[&#39;errcode&#39;];
                                                                $data[&#39;errmsg&#39;]=$token[&#39;errmsg&#39;];
                                                                $data[&#39;appid&#39;]=$this->AppID;
                                                                $data[&#39;secret&#39;]=$this->AppSecret;
                                                                $data[&#39;createtime&#39;]=time();
                                                                $data[&#39;rate&#39;]=$result[0][&#39;rate&#39;]+1;
                                                                $ein=$model->where(&#39;`id`=&#39;.$result[0][&#39;id&#39;])->save($data);
                                                                if($ein)
                                                                    {
                                                                        return false;
                                                                    }
                                                    }
                                        }
                                }
                    }
                    else
                        {//新插入數(shù)據(jù)庫保存
                            $token=$this->getAccessToken();
                            $model=M(&#39;token&#39;);
                            if(!empty($token))
                                        {
                                            $data=array();
                                            $token=json_decode($token);
                                            if(!isset($token[&#39;errcode&#39;]) or !$token[&#39;errcode&#39;])
                                                {
                                                    if(isset($token[&#39;access_token&#39;]) && $token[&#39;access_token&#39;])
                                                        {
                                                            $data[&#39;access_token&#39;]=$token[&#39;access_token&#39;];
                                                            $data[&#39;createtime&#39;]=$time;
                                                            $data[&#39;ex_time&#39;]=$token[&#39;expires_in&#39;];
                                                            //$sql=&#39;insert into ly_token() vlaues()&#39;;
                                                            $data[&#39;rate&#39;]=1;
                                                            $in=$model->data($data)->add();
                                                            if($ein)
                                                            {
                                                                $token[&#39;access_token&#39;];
                                                            }
                                                        }
                                                }
                                                else
                                                    {//微信返回的錯誤信息
                                                        $data[&#39;errcode&#39;]=$token[&#39;errcode&#39;];
                                                        $data[&#39;errmsg&#39;]=$token[&#39;errmsg&#39;];
                                                        $data[&#39;appid&#39;]=$this->AppID;
                                                        $data[&#39;secret&#39;]=$this->AppSecret;
                                                        $data[&#39;createtime&#39;]=time();
                                                        $data[&#39;rate&#39;]=1;
                                                        $ein=$model->data($data)->add();
                                                        if($ein)
                                                            {
                                                                return false;
                                                            }
                                                    }
                                        }
                        }
                return false;
            }  
             
             
             
             
             
    }
 
?>

以上是 微信現(xiàn)金紅包接口實現(xiàn)紅包發(fā)放的示例代碼分享的詳細內(nèi)容。更多信息請關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

本站聲明
本文內(nèi)容由網(wǎng)友自發(fā)貢獻,版權(quán)歸原作者所有,本站不承擔(dān)相應(yīng)法律責(zé)任。如您發(fā)現(xiàn)有涉嫌抄襲侵權(quán)的內(nèi)容,請聯(lián)系admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣服圖片

Undresser.AI Undress

Undresser.AI Undress

人工智能驅(qū)動的應(yīng)用程序,用于創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用于從照片中去除衣服的在線人工智能工具。

Clothoff.io

Clothoff.io

AI脫衣機

Video Face Swap

Video Face Swap

使用我們完全免費的人工智能換臉工具輕松在任何視頻中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的代碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

功能強大的PHP集成開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級代碼編輯軟件(SublimeText3)

熱門話題

Laravel 教程
1601
29
PHP教程
1502
276
抖音網(wǎng)頁版入口登錄鏈接地址https 抖音網(wǎng)頁版入口網(wǎng)址免費 抖音網(wǎng)頁版入口登錄鏈接地址https 抖音網(wǎng)頁版入口網(wǎng)址免費 May 22, 2025 pm 04:24 PM

抖音網(wǎng)頁版的登錄入口是https://www.douyin.com/。登錄步驟包括:1.打開瀏覽器;2.輸入網(wǎng)址https://www.douyin.com/;3.點擊“登錄”按鈕并選擇登錄方式;4.輸入賬號密碼;5.完成登錄。網(wǎng)頁版提供了瀏覽、搜索、互動、上傳視頻和個人主頁管理等功能,具有大屏幕體驗、多任務(wù)處理、便捷的賬號管理和數(shù)據(jù)統(tǒng)計等優(yōu)勢。

拷貝漫畫(官網(wǎng)入口)_拷貝漫畫(nba)正版在線閱讀入口 拷貝漫畫(官網(wǎng)入口)_拷貝漫畫(nba)正版在線閱讀入口 Jun 05, 2025 pm 04:12 PM

拷貝漫畫無疑是一個不容錯過的寶藏。在這里,你可以找到各種風(fēng)格的籃球漫畫,從熱血勵志的競技故事,到輕松幽默的日常喜劇,應(yīng)有盡有。無論是想重溫經(jīng)典,還是想發(fā)掘新作,拷貝漫畫都能滿足你的需求。通過拷貝漫畫提供的正版在線閱讀入口,你將告別盜版資源的困擾,享受高清流暢的閱讀體驗,更能支持你喜愛的漫畫作者,為正版漫畫的發(fā)展貢獻一份力量。

uc瀏覽器與qq瀏覽器哪個好用 uc和qq瀏覽器深度對比評測 uc瀏覽器與qq瀏覽器哪個好用 uc和qq瀏覽器深度對比評測 May 22, 2025 pm 08:33 PM

選擇UC瀏覽器還是QQ瀏覽器取決于你的需求:1.UC瀏覽器適合追求快速加載和豐富娛樂功能的用戶;2.QQ瀏覽器適合需要穩(wěn)定性和與騰訊產(chǎn)品無縫連接的用戶。

AI寫作軟件排行榜單前十名推薦 AI寫作軟件哪些免費 AI寫作軟件排行榜單前十名推薦 AI寫作軟件哪些免費 Jun 04, 2025 pm 03:27 PM

結(jié)合 2025 年最新行業(yè)動態(tài)與多維度評測數(shù)據(jù),以下為綜合排名前十的 AI 寫作軟件推薦,涵蓋通用創(chuàng)作、學(xué)術(shù)研究、商業(yè)營銷等主流場景,同時兼顧中文優(yōu)化與本地化服務(wù):

奈斯漫畫官方頁面免費漫畫在線看 奈斯漫畫登錄頁面免費入口網(wǎng)站 奈斯漫畫官方頁面免費漫畫在線看 奈斯漫畫登錄頁面免費入口網(wǎng)站 Jun 12, 2025 pm 08:18 PM

奈斯漫畫,一個致力于為漫畫愛好者打造的沉浸式閱讀體驗平臺,匯聚了海量國內(nèi)外優(yōu)質(zhì)漫畫資源。它不僅僅是一個漫畫閱讀平臺,更是一個連接漫畫家與讀者、分享漫畫文化的社區(qū)。通過簡潔直觀的界面設(shè)計和強大的搜索功能,奈斯漫畫讓你能夠輕松找到心儀的作品,享受流暢舒適的閱讀體驗。告別漫長的等待和繁瑣的操作,即刻進入奈斯漫畫的世界,開啟你的漫畫之旅吧!

蛙漫 網(wǎng)址在線看入口 漫蛙漫畫(網(wǎng)頁入口)在線觀看 蛙漫 網(wǎng)址在線看入口 漫蛙漫畫(網(wǎng)頁入口)在線觀看 Jun 12, 2025 pm 08:06 PM

蛙漫漫畫,憑借其豐富多元的漫畫資源和便捷流暢的在線閱讀體驗,已成為眾多漫畫愛好者的首選。它就像一個充滿活力的池塘,源源不斷地涌現(xiàn)出新鮮有趣的故事,等待著你去發(fā)現(xiàn)和探索。蛙漫漫畫涵蓋了各種題材,從熱血冒險到甜蜜戀愛,從奇幻科幻到懸疑推理,無論你喜歡哪種類型,都能在這里找到心儀的作品。其簡潔直觀的界面設(shè)計,更讓你能夠輕松上手,快速找到想看的漫畫,沉浸在精彩紛呈的漫畫世界中。

包子漫畫(入口)_包子漫畫(新入口)2025 包子漫畫(入口)_包子漫畫(新入口)2025 Jun 05, 2025 pm 04:18 PM

在這里,您可以盡情暢游于浩瀚的漫畫海洋,探索各種題材和風(fēng)格的作品,從熱血激昂的少年漫,到細膩動人的少女漫,從懸疑燒腦的推理漫,到輕松搞笑的日常漫,應(yīng)有盡有,總有一款能夠觸動您的心弦。我們不僅擁有海量的正版漫畫資源,還不斷引進和更新最新的作品,確保您能夠第一時間閱讀到您喜愛的漫畫。

b安最新注冊地址_怎么注冊b安交易所 b安最新注冊地址_怎么注冊b安交易所 May 26, 2025 pm 07:12 PM

2025b安最新官網(wǎng)入口地址:https://www.marketwebb.co/zh-CN/join?ref=507720986&amp;type=wenzi;幣安(Binance)交易所是一家全球性的加密貨幣交易所,服務(wù)包括北美、歐洲、臺灣、中東、香港、馬來西亞在內(nèi)的180個國家地區(qū),提供超過600種加密貨幣,在全球擁有2.7億注冊用戶。

See all articles