Db_model->getAllData("weixin"); $query = $this->db->query("SELECT * FROM weixin"); $row = $query->row(); $id = isset($row->id)?stripslashes($row->id):""; $token = isset($row->token)?stripslashes($row->token):""; $time = isset($row->updatetime)?stripslashes($row->updatetime):""; $now = date("Y-m-d H:i:s"); if ($token == "0" || $token == "") { $jsoninfo = json_decode($this->https_request($this->getTokenUrl($this->getAppid(), $this->getAppsecret())), true); $token = $jsoninfo["access_token"]; $this->db->query("UPDATE weixin SET token = '".$token."' ,updatetime = '".$now."' WHERE id = '".$id."'"); }else { $updatetime = $now; $now = strtotime((string)$now); $time = strtotime($time); $t = $now - $time; if ($t >= 7100) { $jsoninfo = json_decode($this->https_request($this->getTokenUrl($this->getAppid(), $this->getAppsecret())), true); $token = $jsoninfo["access_token"]; $this->db->query("UPDATE weixin SET token = '".$token."' ,updatetime = '".$updatetime."' WHERE id = '".$id."'"); } } return $token; } /* 根据openid 获取用户信息 */ public function getUserInfor($openid) { $_token = $this->getToken(); $_url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$_token."&openid=".$openid; $_result = json_decode($this->https_request($_url), true); return $_result; } /* oauth2 获取用户信息 */ public function getOauthUserInfor($oauthtoken, $openid) { $_url = "https://api.weixin.qq.com/sns/userinfo?access_token=" . $oauthtoken . "&openid=" . $openid; $_result = json_decode($this->https_request($_url), true); return $_result; } /* 使用code换取access_token */ public function getOauthToken($code) { $_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $this->getAppid() . "&secret=" . $this->getAppsecret() . "&code=" . $code . "&grant_type=authorization_code"; $_result = json_decode($this->https_request($_url), true); return $_result; } /* weixin 提交 */ private function https_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, 1); $output = curl_exec($curl); if (curl_errno($curl)) {return 'ERROR '.curl_error($curl);} curl_close($curl); return $output; } }