微信小程序access_token自动更新

protected function get_access_token()

{

    if(!cache('wx_access_token')){

        $appid = "wx0fsfgs2664edde";

        $appsecret = "7252bd2c6bc4400fgghfhgfh700c";

        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;

        $res = $this->http_request($url);

        $result = json_decode($res, true);

        $access_token = $result["access_token"];

        cache( 'wx_access_token', $access_token, (intval($result["expires_in"])-200) );

    }

    return cache('wx_access_token');

}

//将access_token存入memcached中

protected function http_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, TRUE);

       $output = curl_exec($curl);

       curl_close($curl);

       return $output;

}

你可能感兴趣的:(微信小程序access_token自动更新)