小程序授权代码示例
public function authlogin(){
if($_GET){
$appid = ''; //微信的appid
$secret= ''; //微信的secret秘钥
$code= $_GET['code'];//小程序传来的code值
$url = "https://api.weixin.qq.com/sns/jscode2session?appid={$appid}&secret={$secret}&js_code={$code}&grant_type=authorization_code"; //请求接口获取openid
$open = $this->http_curl($url);
//yourAppid为开发者appid.appSecret为开发者的appsecret,都可以从微信公众平台获取
$openid = $open['openid'];
$sid = $_POST['sid'];//邀请人id
$where['openid']=$openid;
$list=M('user')->where($where)->find();
if ($list !=null) { //如果数据库中存在此用户的信息,则不需要重新获取
// $list['status']=1;
$arr=array('msg'=>'已经授权过了','code'=>'0','status'=>'1','list'=>$list,'openid'=>$openid);
echo json_encode($arr,JSON_UNESCAPED_UNICODE);
}else {
$arr=array('msg'=>'还没有授权','code'=>'0','status'=>'0','list'=>$list,'openid'=>$openid);
echo json_encode($arr,JSON_UNESCAPED_UNICODE);
}
}else{
$arr=array('msg'=>'请求错误','code'=>'1');
echo json_encode($arr,JSON_UNESCAPED_UNICODE);
}
}
public function http_curl($url){
//用curl传参
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
//关闭ssl验证
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch,CURLOPT_HEADER, 0);
$output = curl_exec($ch);
curl_close($ch);
return json_decode($output, true);
}