微博php sdk,php sdk微博第三方授权登入简单使用

简单说一下OAuth协议:

OAUTH是一种开放的协议,为桌面、手机或web应用提供了一种简单的,标准的方式去访问需要用户授权的API服务。

46c02af436e08f76fcd0d2f3458460ce.png

下面我们去微博开发者平台登入微博。如图填写信息:

0e8a55bc4d50b9b1512179bbe7f133ca.png

94196a2c70bfa6945ca1a075fc2cb7c0.png

网站接入->创建新应用

7baedf443cfcb431772cbadd3a9a6aba.png

c778c08168babcad71c794c85a609a07.png

填写信息后可以拿到微博给我们分配的App Key、App Secret

72aba8815ce2bf93f11976228a73269a.png

填写回调地址

07d562dc5d30cc902f94f1802c740ec9.png

下载php 版本的sdk包并解压

8627ec4fa1469e29c535598f5abca0d6.png

d3325a1d81b7ddc08ac66ad16e7d9167.png

我这里把核心类文件saetv2.ex.class.php和weibo_login.png单独拿出来。

index.php<?php

require_once 'config.php';

require_once 'saetv2.ex.class.php';

$redis = new \Redis();

$redis->connect('127.0.0.1');

$cache_key = 'weibo_accessToken';

$isLogin = $redis->get($cache_key) ? true : false;

?>

微博登入测试

登入成功

退出


//$o = new SaeTClientV2(WB_KEY, WB_SEC, $redis->get($cache_key));

//debug($o->update('我的测试微博'));

?>

weibo_login.png

config.php<?php

/**

* Created by ZhengNiu.

* User: admin

* Date: 2019/8/28

* Time: 9:37

*/

define("WB_KEY", 'xxxxxxxx');//App Key

define("WB_SEC", 'xxxxxxxxx');//App Secret

define("CALLBACK","https://xxx.com/callback.php");//回调地址,需要和上面在微博平台配置的保持一致。

//调试工具暂时放这里

function debug($val, $dump = false, $exit = true)

{

if ($dump) {

$func = 'var_dump';

} else {

$func = (is_array($val) || is_object($val)) ? 'print_r' : 'printf';

}

header('Content-type:text-html;charset=utf-8');

echo '

debug output:';

$func($val);

echo "

";

if ($exit) exit;

}

callback.php<?php

/**

* Created by ZhengNiu.

* User: admin

* Date: 2019/8/28

* Time: 10:20

*/

require_once 'config.php';

require_once 'saetv2.ex.class.php';

$redis = new \Redis();

$redis->connect('127.0.0.1');

$cache_key = 'weibo_accessToken';

$access_token = $redis->get($cache_key);

if (!$access_token) {

$keys['code'] = $_GET['code'];

$keys['redirect_uri'] = CALLBACK;

$o = new SaeTOAuthV2(WB_KEY, WB_SEC);

$res = $o->getAccessToken($keys);

$redis->setex($cache_key,$res['expires_in'], $res['access_token']);

$access_token = $res['access_token'];

}

header('Location:index.php');

loginout.php<?php

/**

* Created by ZhengNiu.

* User: admin

* Date: 2019/8/28

* Time: 11:10

*/

$redis = new \Redis();

$redis->connect('127.0.0.1');

$cache_key = 'weibo_accessToken';

$redis->delete($cache_key);

header('Location:index.php');

wblogin.php<?php

/**

* Created by ZhengNiu.

* User: admin

* Date: 2019/8/28

* Time: 10:10

*/

require_once 'config.php';

require_once 'saetv2.ex.class.php';

$o = new SaeTOAuthV2(WB_KEY, WB_SEC);

$oauth = $o->getAuthorizeURL(CALLBACK);

header('Location:'.$oauth);

上面只是粗略的写了代码演示了一下使用方法,应用中请根据自己的需求结合到框架里面。

演示:

fe8929223a414c022f7dc741d4ae7c0d.gif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值