您的位置:首页 > 其它

新浪微博OAuth 2.0接口简单实例例

2011-11-02 16:30 337 查看
这几天一直在搞腾讯和新浪的开放平台~~整的快晕了~~两家基本差不多,整出来一小块放出来大家看看。

有意探讨的可邮件jackqn@126.com或者在下面留言~~

sina.html

<html lang="zh-cn">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<title>

</title>

<script type="text/javascript">

var childWindow;

function toQzoneLogin()

{

childWindow = window.open("sina_rge.php","TencentLogin","width=450,height=320,menubar=0,scrollbars=1, resizable=1,status=1,titlebar=0,toolbar=0,location=1");

}

function closeChildWindow()

{

childWindow.close();

}

</script>

</head>

<body>

<br><br>

<a href="#" onclick='toQzoneLogin()'>登录新浪</a>

<br><br>

</body>

</html>

sina_rge.php

<?php

require_once("sinalib/utils.php");

mysql_close($conn);

function redirect_to_login($appid, $callback)

{

echo $appid;

$redirect = "https://api.weibo.com/oauth2/authorize"."?response_type=code&client_id=$appid&redirect_uri=".$callback;

header("Location:$redirect");

}

redirect_to_login($sinaappid,"返回地址");

?>

sinaacc.php

<?php

require_once("sinalib/utils.php");

function redirect_to_acc($appid,$secret,$code,$callback){

$url="https://api.weibo.com/oauth2/access_token";

$data="client_id=$appid&client_secret=$secret&code=$code&grant_type=authorization_code&redirect_uri=".rawurlencode($callback);

$access=do_call($url,$data);

$arr=json_decode($access,true);

print_r($arr);

$access_token=$arr['access_token'];

echo $access_token."新浪授权成功,每次授权可用24小时,超出时限请重新授权";

}

redirect_to_acc($sinaappid,$sinaappkey,$_GET['code'], "返回地址");

?>

sinalib/utils.php

<?php

error_reporting(E_ALL || ~E_NOTICE);

$sinaappid=“申请到的appid”;

$sinaappkey="申请到的key";

function do_post($url, $postdata)

{

//http

$ch = curl_init();

curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

curl_setopt($ch, CURLOPT_POST, TRUE);

curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);

curl_setopt($ch, CURLOPT_URL, $url);

$ret =curl_exec($ch);

curl_close($ch);

return $ret;

}

function do_call($url, $postdata)

{

//https

$ch = curl_init();

curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

curl_setopt($ch, CURLOPT_POST, TRUE);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);

curl_setopt($ch, CURLOPT_URL, $url);

$ret =curl_exec($ch);

curl_close($ch);

return $ret;

}

function get_arr($data){

$arr = array();

$tem=substr($data,strpos($data,'{'),$data.len-3);

$arr=json_decode($tem,true);

return $arr;

}

function getMyuri(){

//获取当前目录

$baseUrl = str_replace('\\','/',dirname($_SERVER['SCRIPT_NAME']));

$baseUrl = empty($baseUrl) ? '/' : '/'.trim($baseUrl,'/').'/';

return $baseUrl;

}

?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: