php实现新浪微博授权登录
2021-06-22 15:04
标签:ror ret 新浪 color cep src alt 操作 sign 第一步: 前往微博开放平台申请应用,地址:http://open.weibo.com/,需要企业账号并审核通过。(微博授权审核是需要在你的web或者app上需要有微博登陆的按钮,否者审核不会通过)
拿到App Key和App Secret之后就可以开始调取接口。 第二步: 用户表新加字段存储微博返回信息,如图:
第三步: 去官方下载一个sdk,下载地址 https://github.com/xiaosier/libweibo,直接上代码: /** $app_secret = "7d26eeb3fd46815a1d1ecda1cd786d29"; //app secert $my_url = "http://".$_SERVER[‘HTTP_HOST‘]."/Home/User/weiboLogin"; //第一步:用户同意授权,获取code echo(""); //第二步:用code换取Access Token $keys = array(); } } //第三步,用Access Token获取账号信息 $m = M("Member"); $user = M("Member")->where(array("xl_openid"=> $usr_info[‘idstr‘]))->find(); }else{ }else{ }else { } php实现新浪微博授权登录 标签:ror ret 新浪 color cep src alt 操作 sign 原文地址:http://www.cnblogs.com/tongl/p/7169737.html
* 微博授权登录
*/
public function weiboLogin(){
$app_key = "3495468843"; //app key
$code = $_REQUEST["code"];//存放code
if(empty($code)){
$url = "https://api.weibo.com/oauth2/authorize?client_id=".$app_key."&response_type=code&redirect_uri=". urlencode($my_url);
}
if(!empty($code)){
//include_once( ‘saetv2.ex.class.php‘ );
vendor("Weibo.saetv2"); //引入微博sdk
$o = new \SaeTOAuthV2( $app_key , $app_secret );
$keys[‘code‘] = $code;
$keys[‘redirect_uri‘] = $my_url;
try {
$token = $o->getAccessToken( ‘code‘, $keys ) ;
catch (\OAuthException $e) {
if ($token) {
//$_SESSION[‘token‘] = $token;
//setcookie( ‘weibojs_‘.$o->client_id, http_build_query($token) );
$c = new \saetclientv2($app_key,$app_secret, $token[‘access_token‘]);
$ms =$c->home_timeline();
$uid_get = $c->get_uid();
$uid = $uid_get[‘uid‘];
$usr_info=$c->show_user_by_id($uid); //微博sdk方法获取用户的信息
$data = array();
$data[‘sex‘] = $usr_info[‘gender‘]==‘m‘?1:2;
$data[‘province‘] = $usr_info[‘province‘];
$data[‘city‘] = $usr_info[‘city‘];
$data[‘person_name‘] = $usr_info[‘name‘];
$data[‘xl_openid‘] = $usr_info[‘idstr‘];
$data[‘xl_img‘] = $usr_info[‘profile_image_url‘];
$data[‘addtime‘] = date("Y-m-d H:i:s", time());
$data[‘person_img‘] = $usr_info[‘profile_image_url‘];
$data[‘signtime‘] = date("Y-m-d H:i:s", time());
if($user){
$res = M("Member")->where(array("xl_openid"=> $usr_info[‘idstr‘]))->setField("signtime", date("Y-m-d H:i:s", time()));
if($res){
//登录成功业务逻辑
$this->error("操作异常,拒绝访问!", U(‘user/login‘));
}
}else{
$res=$m->add($data);
if($res){
//登录成功业务逻辑
$this->error("操作异常,拒绝访问!", U(‘user/login‘));
}
}
$this->error("授权失败。", U(‘user/login‘));
}
}