关于在微信下授权登录的示例
程序员文章站
2022-03-20 13:25:14
...
微信平台功能 微信授权,必须是认证的服务号方可有此权限
public function _initialize(){
header("content-type:text/html;charset=utf-8");
$this->appid=''; //此处修改为自己的参数
$this->secret='';
}
//构造一个远程请求https的函数
function curlGet($url,$type="GET"){
$ch = curl_init();
$header = "Accept-Charset: utf-8";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $type);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$temp = curl_exec($ch);
return $temp;
}
public function get_authorize_url($redirect_uri = '', $state = '1')
{
$redirect_uri = urlencode($redirect_uri);
return "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->appid}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_base&state={$state}#wechat_redirect";
}
//引导用户点击的链接,从而获取code
public function user_index(){
$url="http://wxapp.no1ss.com/tpdemo/Index.php/App/getwxid";
$code=$this->get_authorize_url($url,'1');
echo "点击进入";
}
//通过点击以上的链接,从而获取粉丝的openid,获取成功以后开始业务代码
public function getwxid(){
$code = $_GET['code'];
//echo $code;
$url_get='https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx621853e4328ae9e3&secret=c13b63790b23cb5feae6c6de90b5293a&code='.$code.'&grant_type=authorization_code';
$json=json_decode($this->curlGet($url_get));
$this->wecha_id = $json->openid;
//echo $this->wecha_id;exit;
$_SESSION['wecha_id']=$this->wecha_id;
$info=M('member_app')->where(array('wecha_id'=>$this->wecha_id))->find();
//print_r($info);exit;
if(empty($info)){
$this->error('第一次使用请先绑定!','__URL__/reg');
}else{
$this->redirect('addorder');
}
public function _initialize(){
header("content-type:text/html;charset=utf-8");
$this->appid=''; //此处修改为自己的参数
$this->secret='';
}
//构造一个远程请求https的函数
function curlGet($url,$type="GET"){
$ch = curl_init();
$header = "Accept-Charset: utf-8";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $type);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$temp = curl_exec($ch);
return $temp;
}
public function get_authorize_url($redirect_uri = '', $state = '1')
{
$redirect_uri = urlencode($redirect_uri);
return "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->appid}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_base&state={$state}#wechat_redirect";
}
//引导用户点击的链接,从而获取code
public function user_index(){
$url="http://wxapp.no1ss.com/tpdemo/Index.php/App/getwxid";
$code=$this->get_authorize_url($url,'1');
echo "点击进入";
}
//通过点击以上的链接,从而获取粉丝的openid,获取成功以后开始业务代码
public function getwxid(){
$code = $_GET['code'];
//echo $code;
$url_get='https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx621853e4328ae9e3&secret=c13b63790b23cb5feae6c6de90b5293a&code='.$code.'&grant_type=authorization_code';
$json=json_decode($this->curlGet($url_get));
$this->wecha_id = $json->openid;
//echo $this->wecha_id;exit;
$_SESSION['wecha_id']=$this->wecha_id;
$info=M('member_app')->where(array('wecha_id'=>$this->wecha_id))->find();
//print_r($info);exit;
if(empty($info)){
$this->error('第一次使用请先绑定!','__URL__/reg');
}else{
$this->redirect('addorder');
}
AD:真正免费,域名+虚机+企业邮箱=0元