通过微博授权接入网站,让微博用户也可以在网站发表文章,评论。
大略的步骤可以看这里via@bigfa 将微博接入WordPress:微博授权登录
本篇是在其基础上进行拓展,获取微博用户的头像和微博链接。首先加入functions.php完整的代码,见注释
<?php
// via@ bigfa
// url@ http://fatesinger.com/74619
function oauth_http($method,$header,$url,$data){
$method = $method ? $method : 'get';
if( $method == 'get') {
$ch = curl_init ();
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, TRUE );
curl_setopt ( $ch, CURLOPT_URL, $url );
$result = curl_exec ( $ch );
curl_close ( $ch );
return $result;
} elseif ( $method == 'post' ) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, TRUE );
curl_setopt ( $ch, CURLOPT_POST, TRUE );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$ret = curl_exec ( $ch );
curl_close ( $ch );
return $ret;
}
}
// weibo
define('WB_APPID','');//appkey
define('WB_APPSECRET','');//appsecret
function weibo_oauth(){
$code = $_GET['code'];
$url = "https://api.weibo.com/oauth2/access_token";
$data = "client_id=" . WB_APPID . "&client_secret=" . WB_APPSECRET . "&grant_type=authorization_code&redirect_uri=".urlencode (home_url())."&code=".$code;
$output = json_decode(oauth_http('post',array(),$url,$data));
$sina_access_token = $output->access_token;
$sina_uid = $output->uid;
if(empty($sina_uid)){
wp_redirect(home_url('/?3'.$sina_uid));
exit;
}
if(is_user_logged_in()){
$this_user = wp_get_current_user();
update_user_meta($this_user->ID ,"sina_uid",$sina_uid);
update_user_meta($this_user->ID ,"sina_access_token",$sina_access_token);
echo '<script>if( window.opener ) {window.opener.location.reload();
window.close();
}else{
window.location.reload()";
}</script>';
}else{
$user_weibo = get_users(array("meta_key "=>"sina_uid","meta_value"=>$sina_uid));
if(is_wp_error($user_weibo) || !count($user_weibo)){
$get_user_info = "https://api.weibo.com/2/users/show.json?uid=".$sina_uid."&access_token=".$sina_access_token;
$data = file_get_contents( $get_user_info );
$str = json_decode($data , true);
$username = $str['screen_name'];
$login_name = wp_create_nonce($sina_uid);
$random_password = wp_generate_password( $length=12, $include_standard_special_chars=false );
$userdata=array(
'user_login' => $login_name,
'display_name' => $username,
'user_pass' => $random_password,
'nick_name' => $username
);
$user_id = wp_insert_user( $userdata );
wp_signon(array("user_login"=>$login_name,"user_password"=>$random_password),false);
update_user_meta($user_id ,"sina_uid",$sina_uid);
update_user_meta($user_id ,"sina_access_token",$sina_access_token);
echo '<script>if( window.opener ) {window.opener.location.reload();
window.close();
}else{
window.location.href = "'.home_url().'";
}</script>';
}else{
update_user_meta($user_weibo[0]->ID ,"sina_access_token",$sina_access_token);
wp_set_auth_cookie($user_weibo[0]->ID);
echo '<script>if( window.opener ) {window.opener.location.reload();
window.close();
}else{
window.location.href = "'.home_url().'";
}</script>';
}
}
}
// 授权链接
function weibo_oauth_url(){
$url = 'https://api.weibo.com/oauth2/authorize?client_id=' . WB_APPID . '&response_type=code&redirect_uri=' . urlencode (home_url('/?type=sina'));
return $url;
}
function social_oauth_weibo(){
if (isset($_GET['code']) && isset($_GET['type']) && $_GET['type'] == 'sina'){
weibo_oauth();
}
}
add_action('init','social_oauth_weibo');
// via@ INLOJV微博用户头像函数
// url@ http://www.inlojv.com/
function inlo_weibo_avatar($userid,$size="32"){
global $current_user;
$sina_uid = get_user_meta($userid, 'sina_uid', true);
$username = get_user_meta($userid, 'nickname', true);
// 输出普通邮箱用户头像
$host = 'http://secure.gravatar.com'; // 可以替换为多说服务器:http://gravatar.duoshuo.com,http://www.gravatar.com , https://secure.gravatar.com
$email = $current_user->user_email;
$email_hash = md5( strtolower( trim( $email ) ) );
$out_avatar= "$host/avatar/";
$out_avatar .= $email_hash;
$out_avatar .= '?s=32';
$input_alt = $current_user->display_name;
if (!$sina_uid) {// 若不是微博用户登录(而是普通邮箱用户)
$user_avatar = $out_avatar;
$username = $input_alt;
}else{ // 若是微博用户
$user_avatar = 'http://tp4.sinaimg.com/'.$sina_uid.'/180/1.jpg';
}
$img = '<img width="'.$size.'" height="'.$size.'" class="avatar" src="'.$user_avatar.'" alt="'.$username.'">';
echo $img;
//return $img;
}
?>
主要是后面的头像函数,输出方式为
<?php inlo_weibo_avatar($userid,$size="32") ?>
其中的$userid是用户id,登录用户可以用这个
<?php global $current_user; inlo_weibo_avatar($current_user->ID);?>
评论用户可以用这个
<?php inlo_weibo_avatar($comment->user_id);?>
默认大小是32px 。
具体输出位置自己看着办吧。
本站文章除注明转载/出处外,均为本站原创或翻译。若要转载但请务必注明出处,尊重他人劳动成果。
转载请注明出处链接 : https://www.inlojv.com/4757.html
