class WxAuthModel extends BaseModel
{
var $appId = APPID;
var $appSecret = APPSECRET;
/*微信x小程序,获取微信openid
*前端的回调url:填写成你的请求地址,然后微信会自己在这个回调url后加一个code,获取通过url传过来的code来换取access_token
*/
public function get_access_token($code){
$token_url = "https://api.weixin.qq.com/sns/jscode2session?appid=".$this->appId."&secret=".$this->appSecret."&js_code=".$code."&grant_type=authorization_code";
$token_info = $this->http_request($token_url);
return json_decode($token_info, true);
}
//HTTP请求(支持HTTP/HTTPS,支持GET/POST)
private function http_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, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($curl);
curl_close($curl);
file_put_contents('/tmp/heka_weixin.' . date("Ymd") . '.log', date('Y-m-d H:i:s') . " " . $output . "
", FILE_APPEND); //记录微信请求日志
return $output;
}
}