使用新浪微博开放平台api同步微博内容至自己网站(微博登录访问第三方网站的高级进阶干货需要注意哪些? )
优采云 发布时间: 2022-02-22 18:00使用新浪微博开放平台api同步微博内容至自己网站(微博登录访问第三方网站的高级进阶干货需要注意哪些?
)
在平时的项目开发过程中,除了注册这个网站账号登录外,还可以调用第三方接口登录网站。这里我们以微博登录为例。微博登录包括身份认证、用户关系和内容传播。允许用户登录微博访问第三方网站、分享内容、同步信息。
1、首先需要将需要授权的用户引导到以下地址:
https://api.weibo.com/oauth2/authorize?client_id=YOUR_CLIENT_ID&response_type=code&redirect_uri=YOUR_REGISTERED_REDIRECT_URI
如果用户同意授权,页面跳转到YOUR_REGISTERED_REDIRECT_URI/?code=CODE:
2、接下来需要根据上面得到的代码兑换Access Token:
https://api.weibo.com/oauth2/access_token?client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET&grant_type=authorization_code&redirect_uri=YOUR_REGISTERED_REDIRECT_URI&code=CODE
返回值:
JSON
{
"access_token": "SlAV32hkKG",
"remind_in": 3600,
"expires_in": 3600
}
3、最后使用获取到的OAuth2.0 Access Token调用API获取用户身份,完成用户登录。
为了方便,我们先将get和post封装到application下的common.php中:
应用通用文件common.php:
function get( $url, $_header = NULL )
{
$curl = curl_init();
//curl_setopt ( $curl, CURLOPT_SAFE_UPLOAD, false);
if( stripos($url, 'https://') !==FALSE )
{
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
}
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
if ( $_header != NULL )
{
curl_setopt($curl, CURLOPT_HTTPHEADER, $_header);
}
$ret = curl_exec($curl);
$info = curl_getinfo($curl);
curl_close($curl);
if( intval( $info["http_code"] ) == 200 )
{
return $ret;
}
return false;
}
/*
* post method
*/
function post( $url, $param )
{
$oCurl = curl_init ();
curl_setopt ( $oCurl, CURLOPT_SAFE_UPLOAD, false);
if (stripos ( $url, "https://" ) !== FALSE) {
curl_setopt ( $oCurl, CURLOPT_SSL_VERIFYPEER, FALSE );
curl_setopt ( $oCurl, CURLOPT_SSL_VERIFYHOST, false );
}
curl_setopt ( $oCurl, CURLOPT_URL, $url );
curl_setopt ( $oCurl, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $oCurl, CURLOPT_POST, true );
curl_setopt ( $oCurl, CURLOPT_POSTFIELDS, $param );
$sContent = curl_exec ( $oCurl );
$aStatus = curl_getinfo ( $oCurl );
curl_close ( $oCurl );
if (intval ( $aStatus ["http_code"] ) == 200) {
return $sContent;
} else {
return false;
}
}
控制器处理代码Login.php:
class Login extends \think\Controller
{
public function index()
{
$key = "****";
$redirect_uri = "***微博应用安全域名***/?backurl=***项目本地域名***/home/login/webLogin?";
//授权后将页面重定向到本地项目
$redirect_uri = urlencode($redirect_uri);
$wb_url = "https://api.weibo.com/oauth2/authorize?client_id={$key}&response_type=code&redirect_uri={$redirect_uri}";
$this -> assign('wb_url',$wb_url);
return view('login');
}
public function webLogin(){
$key = "*****";
//接收code值
$code = input('get.code');
//换取Access Token: post方式请求 替换参数: client_id, client_secret,redirect_uri, code
$secret = "********";
$redirect_uri = "********";
$url = "https://api.weibo.com/oauth2/access_token?client_id={$key}&client_secret={$secret}&grant_type=authorization_code&redirect_uri={$redirect_uri}&code={$code}";
$token = post($url, array());
$token = json_decode($token, true);
//获取用户信息 : get方法,替换参数: access_token, uid
$url = "https://api.weibo.com/2/users/show.json?access_token={$token['access_token']}&uid={$token['uid']}";
$info = get($url);
if($info){
echo "<p>登录成功";
}
}
}</p>
模板代码 login.html:
DOCTYPE html>
微博登录
点击这里进行微博登录
渲染:
希望以上内容对您有所帮助。很多PHPer进阶的时候总会遇到一些问题和瓶颈。编写业务代码时没有方向感。我不知道从哪里开始改进。我整理了其中一些。材料,包括但不限于:分布式架构、高扩展性、高性能、高并发、服务器性能调优、TP6、laravel、YII2、Redis、Swoole、Swoft、Kafka、Mysql优化、shell脚本、Docker、微服务、Nginx和其他进阶干货知识点可以免费分享给大家,需要的可以点这里链接或者关注下方我们的专栏
转载:
转载:
----------------------------------- ---------- ------------自有项目-------------- --------- ----------------------------------------- ----------------
//新浪授权登录
public function weibo_login()
{
$key = "3700xxx";
$redirect_uri = "http://xxxx.xxxxx.com/wpapi/register/web_login_back";
//授权后将页面重定向到本地项目
$redirect_uri = urlencode($redirect_uri);
$wb_url = "https://api.weibo.com/oauth2/authorize?client_id={$key}&response_type=code&redirect_uri={$redirect_uri}";
$this -> assign('wb_url',$wb_url);
return $this->fetch('weibo_register');
}
//新浪授权登录--回到地址
public function web_login_back(){
$key = "370xxx";
//接收code值
$code = input('get.code');
//换取Access Token: post方式请求 替换参数: client_id, client_secret,redirect_uri, code
$secret = "7124de4bdaa82ca2ccc52xxxxx";
$redirect_uri = "http://xxx.xxxxx.com/wpapi/register/web_login_back";
$url_post = "https://api.weibo.com/oauth2/access_token?client_id={$key}&client_secret={$secret}&grant_type=authorization_code&redirect_uri={$redirect_uri}&code={$code}";
$client = new \GuzzleHttp\Client();
$response = $client->request('POST', $url_post);
$response->getStatusCode(); // 200
$response->getHeaderLine('content-type'); // 'application/json; charset=utf8'
$data = json_decode($response->getBody(), true);
// dump($data);die;
// $token = json_decode($data, true);
$token = $data;
//获取用户信息 : get方法,替换参数: access_token, uid
$url_get = "https://api.weibo.com/2/users/show.json?access_token={$token['access_token']}&uid={$token['uid']}";
$response_get = $client->request('get', $url_get);
$response_get->getStatusCode(); // 200
$response_get->getHeaderLine('content-type'); // 'application/json; charset=utf8'
$info_info = json_decode($response_get->getBody(), true);
$info = $info_info;
dump($info);
if($info){
echo "登录成功";
}
}
DOCTYPE html>
Title
微博授权登录
点击登录