Friendships/exists

跳转到: 导航, 搜索

目录

friendships/exists

判断两个用户是否有关注关系,如果user_a关注了user_b则返回true,否则返回false.

URL

http://api.t.sina.com.cn/friendships/exists.format

格式

xml, json

HTTP请求方式

GET

是否需要身份验证

true

请求数限制

true

请求参数

  • user_a. 必填参数,要判断的用户UID
  • user_b. 必填参数,要判断的被关注人用户UID


返回结果

XML示例:

<friends>true</friends>

JSON示例:

{"friends":true}

使用示例

  • xml

curl http://api.t.sina.com.cn/friendships/exists.xml?user_a=10501&user_b=10502

  • json

curl http://api.t.sina.com.cn/friendships/exists.json?user_a=10501&user_b=10502

Java示例

请从 微博SDK开发包下载 下载Java SDK
代码示例如下:

package weibo4j.examples;

import weibo4j.Weibo;
import weibo4j.WeiboException;

public class ExistsFriendship {

	/**
	 * 是否关注某用户
	 * @param args
	 */
	public static void main(String[] args) {
		System.setProperty("weibo4j.oauth.consumerKey", Weibo.CONSUMER_KEY);
    	System.setProperty("weibo4j.oauth.consumerSecret", Weibo.CONSUMER_SECRET);
	 	try {
		//args[2]:自己的id;args[3]:关注对象的id
		boolean bool = getWeibo(true,args).existsFriendship(args[2],args[3]);//args[2]:关注用户的id
		System.out.println(bool);
		} catch (WeiboException e) {
			e.printStackTrace();
		}
	}
	
	private static Weibo getWeibo(boolean isOauth,String[] args) {
		Weibo weibo = new Weibo();
		if(isOauth) {//oauth验证方式 args[0]:访问的token;args[1]:访问的密匙
			weibo.setToken(args[0], args[1]);
		}else {//用户登录方式
    		weibo.setUserId(args[0]);//用户名/ID
    		weibo.setPassword(args[1]);//密码
	 	}
		return weibo;
	}
}

PHP示例

请从 微博SDK开发包下载 处下载PHP SDK(支持OAuth验证之版本)
代码示例如下:

//friendships/exists
//是否关注某用户
$c = new WeiboClient( WB_AKEY , 
                      WB_SKEY , 
                      $_SESSION['last_key']['oauth_token'] , 
                      $_SESSION['last_key']['oauth_token_secret']  );
//自己的id
$user_a = 'u_id';
//关注对象的id
$user_b= "u_id";
$msg = $c->oauth->get('http://api.t.sina.com.cn/friendships/exists.json?user_a='.$user_a.'&user_b='.$user_b);
if ($msg === false || $msg === null){
	echo "Error occured";
	return false;
}
if (isset($msg['error_code']) && isset($msg['error'])){
	echo ('Error_code: '.$msg['error_code'].';  Error: '.$msg['error'] );
	return false;
}
if (isset($msg['friends'])){
	if ($msg['friends'] === true){
		echo "true";
	} else {
		echo "false";
	}
}