本文主要是介绍php之sina登陆,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
先下载新浪官方给出的代码实例:phpsdk,我没下下来,去另外的地方找的,51cto,
我是基于ecshop开发的,所有我的是新建了一个文件为oaulogin.php,一个文件夹为oauth,
oauth.config.php是配置文件,saetv2.ex.class.php就是sina phpsdk中的一个文件,我们就需要这个文件,其他的可以用他的代码改,也可以自己写;
<?php
/* PHP SDK
* @version 2.0.0
* @author connect@qq.com
* @copyright © 2013, Tencent Corporation. All rights reserved.
*/
define('IN_ECS', true);
class sinaClass{
protected $appid = "";
protected $callback = "";
protected $baseurl = "";
protected $appkey = "";
public function __construct()
{
require(dirname(__FILE__) . '/oauth.config.php');
require(dirname(__FILE__) . '/saetv2.ex.class.php');
$this->appid = $sina['appid'];
$this->callback = urlencode($sina['callback']);
$this->appkey = $sina['appkey'];
}
public function sina_login(){
$response_type = "code";
$_SESSION['state'] = md5(uniqid(rand(), TRUE));
$login_url = "https://api.weibo.com/oauth2/authorize?client_id=".$this->appid.
"&response_type=".$response_type.
"&redirect_uri=".$this->callback.
"&state=".$_SESSION['state'];
header("Location:$login_url");
}
public function sina_calkback(){
//--------验证state防止CSRF攻击
if($_REQUEST['state'] == $_SESSION['state'])
{
$grant_type = "authorization_code";
$code = $_REQUEST['code'];
$o = new SaeTOAuthV2( $this->appid , $this->appkey );
$keys = array();
$keys['code'] = $_REQUEST['code'];
//$keys['redirect_uri'] = $this->callback;
$keys['redirect_uri'] = "http://www.xxxxx.com";
try {
$token = $o->getAccessToken( 'code', $keys ) ;
} catch (OAuthException $e) {
}
return $token;
}
else
{
return "";
}
}
function get_user_info($access_token, $uid)
{
if(!empty($access_token) && !empty($uid))
{
$c = new SaeTClientV2( $this->appid , $this->appkey , $access_token );
$user_message = $c->show_user_by_id( $uid);
return $user_message;
}
else
{
return "";
}
}
}
做这个时遇到了一个问题: error :redirect_uri_mismatch ,新浪解释是:
这是由于回调地址填写错误造成的,解决办法:
站内应用:redirect_uri等于应用信息中的“站内应用地址”而非“应用实际地址”;
其他应用:redirect_uri需与http://open.weibo.com/apps/30871*****/info/advanced (30871*****替换成你应用的AppKey)应用高级信息中的“应用回调页”完全匹配或在绑定的域名下。
我申请时回调地址和取消回调地址都写的我们网站的网址。这块应该没错,但是还是现实这个错误,然后我把上面代码中橙色的部分改为红色的那样就通过了,获取access_token时回调地址应该和申请时填写的回调地址一致吧,不知道是不是这个原因。
oaulogin.php部分代码
elseif($type == "sinalogin")
{
require(dirname(__FILE__) . '/oauth/sina.class.php');
$qc = new sinaClass();
$qc->sina_login();
}
elseif($type == "sina_callback")
{
require(dirname(__FILE__) . '/oauth/sina.class.php');
$qc = new sinaClass();
$arr = $qc->sina_calkback();
$userinfo = $qc->get_user_info($arr['access_token'], $arr['uid']);
if(empty($userinfo))
{
show_message("登陆错误","首页","index.php");
}
//根据openid判断用户是否第一次登陆
$have_openid = $db->getRow("SELECT user_id, email, user_name FROM ". $ecs->table('users') ." WHERE openid='SINA_".$arr['uid']."'");
if(!empty($have_openid))
{
//更新用户信息
$_SESSION['user_id'] = $have_openid['user_id'];
$_SESSION['user_name'] = $have_openid['user_name'];
update_user_info();
ecs_header("Location: index.php\n");
exit;
}else
{
//插入用户信息
$email = "youremail@type.com";
$user_name = $userinfo['screen_name'];
$sex = $userinfo['gender'] == 'm'?0:1;
$password=md5("sinaxxx");
otherinsert($email, $user_name, $sex, $password, "SINA_".$arr['uid']);
ecs_header("Location: user.php\n");
exit;
}
}
这篇关于php之sina登陆的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!