新浪微博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;
}
?>
有意探讨的可邮件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;
}
?>
相关文章推荐
- 微信企业号简单的OAuth2验证接口实例(使用SpringMVC非注解方式)
- 在范型中接口和实例间转换的问题Why No? .net 2.0 Generic cast Type From Instance to Interface
- php新浪微博登录接口用法实例
- [置顶] 简单的实例理解接口的伟大意义
- 百度地图最简单的完成接口,和实例代码
- 设计模式学习(二):接口的作用1-动态加载实例-简单工厂模式
- JSON文件的封装和解析和API接口的简单实例
- 一个实例来简单的说明接口
- 接口实例_模拟简单的播放器解码插件扩展
- python3爬虫简单小实例2.0
- ASP.NET 2.0 新手SQL存储过程简单实例入门。绝对经典入门
- 使用Delphi调用WebServices接口的简单应用实例
- 调用新浪微博接口实现新浪微博简单搜索
- webservice接口简单实例及调用
- 接口实例_模拟简单的播放器解码插件扩展
- PHP接口与继承的简单实例
- 最简单的动态代理实例(spring基于接口代理的AOP原理)
- OAuth 2.0 . 第三方 登录 (新浪微博、腾讯QQ)
- Java Iterator接口简单实例
- 案例4-3 Ajax的响应处理简单实例(征服Ajax——Web 2.0快速入门与项目实践Java)