appid = WX_APPID; $this->appsecret = WX_APPSECRET; } function codeUrl($redirectUrl,$state){ return 'https://open.weixin.qq.com/connect/qrconnect?appid='.$this->appid.'&redirect_uri='.urlencode($redirectUrl).'&response_type=code&scope=snsapi_login&state='.$state.'#'.time(); } function accessTokenUrl($code) { return 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$this->appid.'&secret='.$this->appsecret.'&code='.$code.'&grant_type=authorization_code'; } function getAccessToken($code, $keys = array()) { $response = $this->https_request($this->accessTokenUrl($code)); $token = json_decode($response,true); if (is_array($token)) { $this->access_token = $token['access_token']; $this->refresh_token = isset($token['refresh_token']) ? $token['refresh_token'] : ''; } else { throw new \Exception("读取access_token错误:{$token['error']}"); } return $token; } //Curl function https_request($url,$options = null,$data = null,$header = 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); } (isset($options['header'])&&$options['header'])?curl_setopt($curl, CURLOPT_HEADER, 1): curl_setopt($curl, CURLOPT_HEADER, 0);//如果有响应头 if(!empty($header))//如果有请求头 { $curl_header = array(); foreach($header as $key=>$value) { $curl_header[] = "$key:$value,"; } curl_setopt($curl,CURLOPT_HTTPHEADER, $curl_header); } if(isset($options['gzip'])&&$options['gzip']==1) curl_setopt($curl, CURLOPT_ENCODING, "gzip"); //如果页面开启了GZIP压缩 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0'); if(!empty($options['time_out'])) { curl_setopt($curl, CURLOPT_TIMEOUT, $options['time_out']); } else { curl_setopt($curl, CURLOPT_TIMEOUT, 15); } $output = curl_exec($curl); curl_close($curl); return $output; } }