Beispiel #1
0
 /**
 * 获取JS API Ticket
 *
 * @return {
        "errcode":0,
        "errmsg":"ok",
        "ticket":"bxLdikRXVbTPdHSM05e5u5sUoXNKdvsdshFKA",
        "expires_in":7200
    }
 */
 public function getJsTicket()
 {
     static $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=%s";
     $ticket = Utility::getJsTicket($this);
     if (empty($ticket) || time() > $ticket['expired']) {
         $token = $this->getAccessToken();
         $body = Utility::http(sprintf($url, $token));
         $json = json_decode($body, true);
         if (!$json || !empty($json['errcode'])) {
             throw new Exception('Error - WeChat Can not get JsTicket.');
         } else {
             $ticket['ticket'] = $json['ticket'];
             $ticket['expired'] = time() + $json['expires_in'] - 120;
             Utility::setJsTicket($ticket, $this);
         }
     }
     return $ticket['ticket'];
 }