123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- <?php
- namespace app\components\OpenAuth\core;
- use app\common\helpers\Session;
- use Yii;
- class WX extends WXOAuth {
- function __construct() {
- parent::__construct();
- }
- function verify() {
- if (isset($_SESSION['wx_token']) && $_SESSION['wx_token'] && isset($_SESSION['wx_token']['uid'])) {
- return true;
- } else {
- return false;
- }
- }
- }
- class WXOAuth {
- public $appid;
- public $appsecret;
- public $access_token;
- public $refresh_token;
- public $timeout = 30;
- public $connecttimeout = 30;
- public $ssl_verifypeer = FALSE;
- public $format = 'json';
- public $decode_json = TRUE;
- public $useragent = 'WX OAuth2.0';
- public $debug = FALSE;
- public static $boundary = '';
- function __construct() {
- $this->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;
- }
- }
|