위챗 개발 5(위챗 js-SDK 어음 얻기,ticket)

1574 단어
public function getTicket($access_token)
    {
        // ticket
        $ticket_info = Trend_Model_Access_Token::instance()->fetchByPK(2);
        $ticket_info = current($ticket_info);
        $now_time = strtotime(date('Y-m-d H:i:s',time()));
        $ctime = strtotime($ticket_info['ctime']);
        $time_pass = ceil($now_time-$ctime);
        
        if ($time_pass >= $ticket_info['expires_in']) {
            $access_token = $access_token;
            $get_token_url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$access_token.'&type=jsapi';
            $ch = curl_init();
            curl_setopt($ch,CURLOPT_URL,$get_token_url);
            curl_setopt($ch,CURLOPT_HEADER,0);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
            $res = curl_exec($ch);
            curl_close($ch);
            $json_obj = json_decode($res,true);
            $ticket = $json_obj["ticket"];
            
            // 
            $new_data = array(
                    'access_token' => $json_obj["ticket"],
                    'expires_in' => $json_obj["expires_in"],
                    'ctime' => date('Y-m-d H:i:s')
            );
            Trend_Model_Access_Token::instance()->updateForEntity($new_data, 2);
        } else {
            $ticket = $ticket_info['access_token'];
        }
        
        return $ticket;
    }

좋은 웹페이지 즐겨찾기