<?php
/**
 * 微信SDK 
 * pan041ymail@gmail.com
 */
class class_weixin_adv
{
  var $appid = "";
  var $appsecret = "";
  //构造函数，获取Access Token
  public function __construct($appid = NULL, $appsecret = NULL)
  {
    if($appid){
      $this->appid = $appid;
    }
    if($appsecret){
      $this->appsecret = $appsecret;
    }
    $this->lasttime = 1395049256;
    $this->access_token = "nRZvVpDU7LxcSi7GnG2LrUcmKbAECzRf0NyDBwKlng4nMPf88d34pkzdNcvhqm4clidLGAS18cN1RTSK60p49zIZY4aO13sF-eqsCs0xjlbad-lKVskk8T7gALQ5dIrgXbQQ_TAesSasjJ210vIqTQ";
    if (time() > ($this->lasttime + 7200)){
     /*
	  $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appid."&secret=".$this->appsecret;
      $res = $this->https_request($url);
      $result = json_decode($res, true);
      $this->access_token = $result["access_token"];
      $this->lasttime = time();
     */
	$oauth_config=require("./config.php");
	$wid=$oauth_config['wid'];
	require_once '../../lib/ConnectPDO.class.php';
	$pdo=new  ConnectPDO();
	get_weixin_info($wid,$pdo);
	
	$this->access_token =  $_POST['monxin_weixin'][$wid]['token'];
	$this->lasttime =  $_POST['monxin_weixin'][$wid]['expires_in'];
	  
	  
    }
  }
//获取用户基本信息
  public function get_user_info($openid)
  {
    $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$this->access_token."&openid=".$openid."&lang=zh_CN";
    $res = $this->https_request($url);
	//var_dump($res);exit;
	$r=json_decode($res, true);
	$r['nickname']=removeEmoji($r['nickname']);
    return $r;
  }

//https请求
  public 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);
    curl_close($curl);
    return $output;
  }
}