新浪微博开放平台旧版API中的PHP例程

相关文章:

一、PHP+新浪微博开放平台+新浪云平台(SAE)方案的基础
二、建立微博应用的过程
三、PHP SDK中Demo程序简析
四、进一步学习的走向和有用的资源
五、必须交待的几个问题
【PDF全文下载】


  本文是新浪微博旧版API中的PHP例程,可以作为初学时阅读和试探的参考材料。

  API 文档的旧版接口文档提供了一些PHP示例(新浪对PHP的支持最好),很方便用于学习。但是在新版API文档中,这些例子找不到了。另外,例子是通过接口组织的,这给初学者带来困难,我们希望通过要实现的功能来组织这些材料。例如,要实现转发一篇微博,能够很快地找到,这显然很诱人。

  烟台大学2008级学生高娜娜、孙芳丽、张肖对例程进行了整理并加入了适当的注释。

  本文的使用方法:阅读每一段代码,将代码写入前面的weibolist.php文件中,或者另建一个文件,在已经建好的应用中运行,调试、观察、改写。以此为基础,学习过程将加快,成为微博开发的高手。

  需要说明的是,下面的条目分类有与API文档V2不一致的地方,请带着质疑,对照着API文档使用下面的例子。


微博

 1.读取接口
//Statuses/counts
//批量获取指定微博的转发数评论数(读取接口)
$c = new WeiboClient( WB_AKEY , 
                      WB_SKEY , 
                      $_SESSION['last_key']['oauth_token'] , 
                      $_SESSION['last_key']['oauth_token_secret']  );
$u_id = "u_id";
$msg = $c->user_timeline($u_id);
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 (count($msg)> 1){
	$sid1 = $msg[0]['id'];
	$sid2 = $msg[1]['id'];
	$sid_total = $sid1.",".$sid2;
	$msg  = $c->get_count_info_by_ids($sid_total);
	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;
	}
	foreach($msg as $data){
		$id = $data['id'];
		$num_comments = $data['comments'];
		$num_rts = $data['rt'];
		echo $id."=".$num_comments."&".$num_rts.";";
	}
}


//Statuses/show
//获取单条ID的微博信息,作者信息将同时返回(读取接口)
$c = new WeiboClient( WB_AKEY , 
                      WB_SKEY , 
                      $_SESSION['last_key']['oauth_token'] , 
                      $_SESSION['last_key']['oauth_token_secret']  );

$u_id = "用户ID";
$msg = $c->user_timeline($u_id);
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 (count($msg)>0){
	$t_id = $msg[0]['id'];
	$msg = $c->show_status($t_id);
	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['id']) && isset($msg['text'])){
		echo($msg['id'].' : '.$msg['text']);
	}
}



2.写入接口

//Statuses/repost
// 转发一条微博信息(写入接口)
$c = new WeiboClient( WB_AKEY , 
                      WB_SKEY , 
                      $_SESSION['last_key']['oauth_token'] , 
                      $_SESSION['last_key']['oauth_token_secret']  );
$msg = $c->user_timeline();
if ($msg === false || $msg === null){
	echo "Error occured";
	return false;
} 
if (count($msg)> 1){
	$sid = $msg[1]['id'];
	echo($sid);
	$msg  = $c->repost($sid);
	if ($msg === false || $msg === null){
		echo "Error occured";
		return false;
	}
	echo($msg['id'].' : '.$msg['text']);
}

//Statuses/reply
//回复一条微博(写入接口)
$c = new WeiboClient( WB_AKEY , 
                      WB_SKEY , 
                      $_SESSION['last_key']['oauth_token'] , 
                      $_SESSION['last_key']['oauth_token_secret']  );
//to be filled in
$sid='sid';
$comment='comment';
$cid='cid';
$msg=$c->reply($sid, $comment,$cid);
echo($msg['text'];

//Statuses/comment destroy
//删除一条评论(写入接口)
$c = new WeiboClient( WB_AKEY , 
                      WB_SKEY , 
                      $_SESSION['last_key']['oauth_token'] , 
                      $_SESSION['last_key']['oauth_token_secret']  );
//comment ID
$c_id = "Comment ID";
$msg = $c->oauth->post("http://api.t.sina.com.cn/statuses/comment_destroy/".$c_id.".json");
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;
}

//Statuses/comment(旧接口)
// 对一条微博信息进行评论
//comments/create 评论一条微博(写入接口)
$c = new WeiboClient( WB_AKEY , 
                      WB_SKEY , 
                      $_SESSION['last_key']['oauth_token'] , 
                      $_SESSION['last_key']['oauth_token_secret']  );
//发表新微博信息
$msg = $c->update("测试发表微博");
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;
}
//微博id
$sid = $msg['id'];
echo($sid." : ".$msg['text']." ".$msg["created_at"]);
sleep(5);
//对刚发表的微博进行评论
$msg = $c->send_comment($sid,"测试评论",null);
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;
}
echo($msg['id'].' : '.$msg['text'].'  '.$msg['created_at']);


//Statuses/repost
//转发一条微博信息(写入接口)
$c = new WeiboClient( WB_AKEY , 
                      WB_SKEY , 
                      $_SESSION['last_key']['oauth_token'] , 
                      $_SESSION['last_key']['oauth_token_secret']  );
$msg = $c->user_timeline();
if ($msg === false || $msg === null){
	echo "Error occured";
	return false;
} 
if (count($msg)> 1){
	$sid = $msg[1]['id'];
	echo($sid);
	$msg  = $c->repost($sid);
	if ($msg === false || $msg === null){
		echo "Error occured";
		return false;
	}
	echo($msg['id'].' : '.$msg['text']);
}


//Statuses/destroy
//删除微博信息(写入接口)
$c = new WeiboClient( WB_AKEY , 
                      WB_SKEY , 
                      $_SESSION['last_key']['oauth_token'] , 
                      $_SESSION['last_key']['oauth_token_secret']  );
//先发表一篇微博
$msg = $c->update("测试发表微博");
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;
} 
echo($msg['id']." : ".$msg['text']." ".$msg["created_at"]);
//删除刚发表的微博
$c->destroy($msg['id']);
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;
}
//user id
$uid = "User ID";
$msg = $c->user_timeline($uid);
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;
} 
//遍历当前微博信息
foreach($msg as $data){
	echo($data['id']." : ".$data['text']." ".$data["created_at"]);
}

//statuses/upload
//上传图片并发布一条微博(写入接口)
<?php
$boundary = uniqid('------------------');
$MPboundary = '--'.$boundary;
$endMPboundary = $MPboundary. '--';

// 需要上传的图片所在路径
$filename = '/tmp/wiki.png';
$file = file_get_contents($filename);

$multipartbody .= $MPboundary . "\r\n";
$multipartbody .= 'Content-Disposition: form-data; name="pic"; filename="wiki.png"'. "\r\n";
$multipartbody .= 'Content-Type: image/png'. "\r\n\r\n";
$multipartbody .= $file. "\r\n";

$k = "source";
// 这里改成 appkey
$v = "appkey";
$multipartbody .= $MPboundary . "\r\n";
$multipartbody.='content-disposition: form-data; name="'.$k."\"\r\n\r\n";
$multipartbody.=$v."\r\n";

$k = "status";
$v = "要上传的文件,这里是描述文字";
$multipartbody .= $MPboundary . "\r\n";
$multipartbody.='content-disposition: form-data; name="'.$k."\"\r\n\r\n";
$multipartbody.=$v."\r\n";
$multipartbody .= "\r\n". $endMPboundary;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://api.t.sina.com.cn/statuses/upload.xml' );
curl_setopt($ch , CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$multipartbody );
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: multipart/form-data; boundary=$boundary"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// 修改成当前用户名及密码
curl_setopt($ch, CURLOPT_USERPWD, "username:password");
$msg = curl_exec($ch);
//echo $multipartbody;
echo 'ok.';
?>


//Statuses/update
//发布一条微博信息(写入接口)
$c = new WeiboClient( WB_AKEY , 
                      WB_SKEY , 
                      $_SESSION['last_key']['oauth_token'] , 
                      $_SESSION['last_key']['oauth_token_secret']  );
$msg = $c->update("测试发表微博");
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;
} 
echo($msg['id']." 

用户

读取接口
//users/show
//根据用户ID获取用户资料(授权用户)
//获取用户信息(读取接口)
$c = new WeiboClient( WB_AKEY , 
                      WB_SKEY , 
                      $_SESSION['last_key']['oauth_token'] , 
                      $_SESSION['last_key']['oauth_token_secret']  );
//用户ID
$u_id = "u_id";
$msg = $c->show_user($u_id);
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;
}
echo($msg['id'].' : '.$msg['name'].'  '.$msg['created_at']);




关系

关系读取接口

//Statuses/friends
//获取用户的关注列表(关注读取接口)
$c = new WeiboClient( WB_AKEY , 
                      WB_SKEY , 
                      $_SESSION['last_key']['oauth_token'] , 
                      $_SESSION['last_key']['oauth_token_secret']  );
//用户ID
$u_id = "User_ID";
$msg = $c->friends(false, false, $u_id);
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;
}
foreach($msg as $friend){
	echo($friend['id'].' : '.$friend['name'].'  '.$friend['created_at'].' ;');
}


//Friendships/show
//获取两个用户关系的详细情况(关系状态读取接口)
$c = new WeiboClient( WB_AKEY , 
                      WB_SKEY , 
                      $_SESSION['last_key']['oauth_token'] , 
                      $_SESSION['last_key']['oauth_token_secret']  );

//关注对象的id
$u_id = "u_id";
$msg = $c->is_followed($u_id);
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['target'])){
	if (isset($msg['target']['followed_by'])){
		if ($msg['target']['followed_by'] === true){
			echo "Target is followed: true";
		} else {
			echo "Target is followed: false";
		}
	}
}


//friendships/exists(关注接口)
//是否关注某用户
//是否关注某用户(推荐使用friendships/show)
$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";
	}
}


粉丝读取接口

//Statuses/followers
//返回用户关注对象列表,并返回最新微博文章。
//获取用户粉丝列表(粉丝读取接口)
$c = new WeiboClient( WB_AKEY , 
                      WB_SKEY , 
                      $_SESSION['last_key']['oauth_token'] , 
                      $_SESSION['last_key']['oauth_token_secret']  );

//用户ID
$u_id = "u_id";
$msg = $c->followers(false, false, $u_id);
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;
}
foreach($msg as $follower){
	echo($follower['id'].' : '.$follower['name'].'  '.$follower['created_at'].' ;');
}


关系链读取接口
写入接口
//friendships/destroy
//消关注某用户(关系  写入接口)
$c = new WeiboClient( WB_AKEY , 
                      WB_SKEY , 
                      $_SESSION['last_key']['oauth_token'] , 
                      $_SESSION['last_key']['oauth_token_secret']  );
//关注用户的id
$u_id = "u_id";
$msg = $c->unfollow($u_id);
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['screen_name'])){
	echo('Unfollow Friend:'.$msg['screen_name']);
}

//friendships/create
//关注某用户(关系  写入接口)
$c = new WeiboClient( WB_AKEY , 
                      WB_SKEY , 
                      $_SESSION['last_key']['oauth_token'] , 
                      $_SESSION['last_key']['oauth_token_secret']  );
//关注用户的id
$u_id = "U_ID";
$msg = $c->follow($u_id);
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['screen_name'])){
	echo('New Friend:'.$msg['screen_name']);
}


账号
读取接口

//account/verify_credentials(旧版接口)
//account/get_uid(账号  读取接口)
//OAuth授权之后获取用户UID
$c = new WeiboClient( WB_AKEY , 
                      WB_SKEY , 
                      $_SESSION['last_key']['oauth_token'] , 
                      $_SESSION['last_key']['oauth_token_secret']  );

$msg = $c->verify_credentials();
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['name'])){
	echo($msg['name']);
}

//account/rate_limit_status(账号 读取接口)
//获取当前用户API访问频率限制
$c = new WeiboClient( WB_AKEY , 
                      WB_SKEY , 
                      $_SESSION['last_key']['oauth_token'] , 
                      $_SESSION['last_key']['oauth_token_secret']  );

$msg = $c->oauth->get("http://api.t.sina.com.cn/account/rate_limit_status.json");
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['hourly_limit'])){
	echo($msg['hourly_limit']);
}

收藏

读取接口

//favorites(收藏 读取接口)
//获取当前用户的收藏列表 
$c = new WeiboClient( WB_AKEY , 
                      WB_SKEY , 
                      $_SESSION['last_key']['oauth_token'] , 
                      $_SESSION['last_key']['oauth_token_secret']  );
$msg = $c->get_favorites();
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;
}
foreach($msg as $status){
	if (isset($status['id']) && isset($status['text'])){
		echo($status['id'].' : '.$status['text'].' ; ');
	}	
}

写入接口
//favorites/destroy(收藏  写入接口)
//删除收藏
$c = new WeiboClient( WB_AKEY , 
                      WB_SKEY , 
                      $_SESSION['last_key']['oauth_token'] , 
                      $_SESSION['last_key']['oauth_token_secret']  );

//微博信息id
$sid='sid';
$msg = $c->remove_from_favorites( $sid );
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['text'])){
	echo($msg['text']);
}


//favorites/create(收藏 写入接口)
//添加收藏 
$c = new WeiboClient( WB_AKEY , 
                      WB_SKEY , 
                      $_SESSION['last_key']['oauth_token'] , 
                      $_SESSION['last_key']['oauth_token_secret']  );
//收藏id
$sid='sid';
$msg = $c->add_to_favorites( $sid );
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 ($msg['text']){
	echo($msg['text']);
}


话题
  读取接口
  写入接口
标签
  读取接口
  写入接口
注册
  读取接口
搜索
  搜索联想接口
  搜索话题接口
推荐
  读取接口
  写入接口
提醒
  读取接口
公共服务
  读取接口)
地理信息
  基础位置读取接口
//location/Base/get_map_image(地理信息  基础位置读取接口)
//生成一张静态的地图图片
<?php
//改为你的微博账号,用户名和密码之间用:隔开
$userPwd = 'user@sina.com:pwd';
//改为你微博应用的appkey
$appkey = '0123456789';

$url = "http://api.t.sina.com.cn/location/base/get_map_image.xml?center_coordinates=116.3136,39.9824&zoom=12&img_format=png&coordinates=116.3136,39.98&names=hello&size=400x400&icons=105&px=1&traffic=on&source=$appkey";

$rst = curlSample($url,$userPwd);

echo $rst;

function curlSample($url,$userPwd,$postFields = '',$header = ''){
	$ch = curl_init() or die (curl_error()) ;
	curl_setopt($ch,CURLOPT_URL,$url);
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
	curl_setopt($ch,CURLOPT_TIMEOUT,30);
	if(!empty($userPwd)){
		curl_setopt($ch,CURLOPT_USERPWD,$userPwd);
	}
	if(!empty($postFields)){
		curl_setopt($ch,CURLOPT_POST,true);
		curl_setopt($ch,CURLOPT_POSTFIELDS,$postFields);
	}
	if(!empty($header)){
		curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
	}
	$result = curl_exec($ch) or die (curl_error($ch));
	curl_close($ch);
	return $result;
}
?>

地标转换接口
POI数据读取接口

//pois/get_poi(POI数据处理接口)
//获取新增POI数据
<?php
//改为你的微博账号,用户名和密码之间用:隔开
$userPwd = 'user@sina.com:pwd';
//改为你微博应用的appkey
$appkey = '0123456789';

$url = "http://api.t.sina.com.cn/location/pois/get_poi.xml?srcids=test001&source=$appkey";

$rst = curlSample($url,$userPwd);

echo $rst;

function curlSample($url,$userPwd,$postFields = '',$header = ''){
	$ch = curl_init() or die (curl_error()) ;
	curl_setopt($ch,CURLOPT_URL,$url);
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
	curl_setopt($ch,CURLOPT_TIMEOUT,30);
	if(!empty($userPwd)){
		curl_setopt($ch,CURLOPT_USERPWD,$userPwd);
	}
	if(!empty($postFields)){
		curl_setopt($ch,CURLOPT_POST,true);
		curl_setopt($ch,CURLOPT_POSTFIELDS,$postFields);
	}
	if(!empty($header)){
		curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
	}
	$result = curl_exec($ch) or die (curl_error($ch));
	curl_close($ch);
	return $result;
}
?>

//Pois/add_poi(POI数据处理接口)
//新增POI数据
<?php
//改为你的微博账号,用户名和密码之间用:隔开
$userPwd = 'user@sina.com:pwd';
//改为你微博应用的appkey
$appkey = '0123456789';

$url = "http://api.t.sina.com.cn/location/pois/add_poi.xml?srcid=test001&name=123&address=abc&city=north&category=010000&longitude=116.30995&latitude=39.98465&source=$appkey";

$rst = curlSample($url,$userPwd);

echo $rst;

function curlSample($url,$userPwd,$postFields = '',$header = ''){
	$ch = curl_init() or die (curl_error()) ;
	curl_setopt($ch,CURLOPT_URL,$url);
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
	curl_setopt($ch,CURLOPT_TIMEOUT,30);
	if(!empty($userPwd)){
		curl_setopt($ch,CURLOPT_USERPWD,$userPwd);
	}
	if(!empty($postFields)){
		curl_setopt($ch,CURLOPT_POST,true);
		curl_setopt($ch,CURLOPT_POSTFIELDS,$postFields);
	}
	if(!empty($header)){
		curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
	}
	$result = curl_exec($ch) or die (curl_error($ch));
	curl_close($ch);
	return $result;
}
?>

POI数据搜索接口

//Pois/view(POI数据搜索接口)
//根据关键字和(或)分类,在一个矩形里进行搜索,返回相关的poi点信息
<?php
//改为你的微博账号,用户名和密码之间用:隔开
$userPwd = 'user@sina.com:pwd';
//改为你微博应用的appkey
$appkey = '0123456789';
//查询关键词
$q = urlencode("123");

$url = "http://api.t.sina.com.cn/location/pois/view.xml?q=$q&coordinate=116.37666,39.93258,116.43943,39.91171&source=$appkey";

$rst = curlSample($url,$userPwd);

echo $rst;

function curlSample($url,$userPwd,$postFields = '',$header = ''){
	$ch = curl_init() or die (curl_error()) ;
	curl_setopt($ch,CURLOPT_URL,$url);
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
	curl_setopt($ch,CURLOPT_TIMEOUT,30);
	if(!empty($userPwd)){
		curl_setopt($ch,CURLOPT_USERPWD,$userPwd);
	}
	if(!empty($postFields)){
		curl_setopt($ch,CURLOPT_POST,true);
		curl_setopt($ch,CURLOPT_POSTFIELDS,$postFields);
	}
	if(!empty($header)){
		curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
	}
	$result = curl_exec($ch) or die (curl_error($ch));
	curl_close($ch);
	return $result;
}
?>

//pois/round(POI数据搜索接口)
//根据关键字和(或)分类,在中心点附近搜索,返回相关的poi点信息
<?php
//改为你的微博账号,用户名和密码之间用:隔开
$userPwd = 'user@sina.com:pwd';
//改为你微博应用的appkey
$appkey = '0123456789';
//查询关键词
$q = urlencode("123");

$url = "http://api.t.sina.com.cn/location/pois/round.xml?coordinate=116.36993,39.97646&q=$q&city=0010&source=$appkey";

$rst = curlSample($url,$userPwd);

echo $rst;

function curlSample($url,$userPwd,$postFields = '',$header = ''){
	$ch = curl_init() or die (curl_error()) ;
	curl_setopt($ch,CURLOPT_URL,$url);
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
	curl_setopt($ch,CURLOPT_TIMEOUT,30);
	if(!empty($userPwd)){
		curl_setopt($ch,CURLOPT_USERPWD,$userPwd);
	}
	if(!empty($postFields)){
		curl_setopt($ch,CURLOPT_POST,true);
		curl_setopt($ch,CURLOPT_POSTFIELDS,$postFields);
	}
	if(!empty($header)){
		curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
	}
	$result = curl_exec($ch) or die (curl_error($ch));
	curl_close($ch);
	return $result;
}
?>

//Pois/keyword(POI数据搜索接口)
//根据关键字和(或)分类进行搜索,返回相关的poi点信息
<?php
//改为你的微博账号,用户名和密码之间用:隔开
$userPwd = 'user@sina.com:pwd';
//改为你微博应用的appkey
$appkey = '0123456789';
//查询关键词
$q = urlencode("123");

$url = "http://api.t.sina.com.cn/location/pois/keyword.xml?source=$appkey&q=$q";

$rst = curlSample($url,$userPwd);

echo $rst;

function curlSample($url,$userPwd,$postFields = '',$header = ''){
	$ch = curl_init() or die (curl_error()) ;
	curl_setopt($ch,CURLOPT_URL,$url);
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
	curl_setopt($ch,CURLOPT_TIMEOUT,30);
	if(!empty($userPwd)){
		curl_setopt($ch,CURLOPT_USERPWD,$userPwd);
	}
	if(!empty($postFields)){
		curl_setopt($ch,CURLOPT_POST,true);
		curl_setopt($ch,CURLOPT_POSTFIELDS,$postFields);
	}
	if(!empty($header)){
		curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
	}
	$result = curl_exec($ch) or die (curl_error($ch));
	curl_close($ch);
	return $result;
}
?>

POI数据写入接口
移动服务读取接口

//Loc/get_location(移动定位接口)
// 根据基站、WIFI等其它数据,返回当前位置
//获取定位数据
//改为你的微博账号,用户名和密码之间用:隔开
$userPwd = 'user@sina.com:pwd';
//改为你微博应用的appkey
$appkey = '0123456789';
$url = "http://api.t.sina.com.cn/location/loc/get_location.json?source=$appkey";

$data = array(
 "version"=> "1.1.0",
 "host"=> "api.t.sina.com.cn/location/loc/",
 "radio_type"=> "gsm",
 "request_address"=> true,
 "decode_pos"=>true,
  "location"=>array( 
   "latitude"=>39.08943,
   "longitude"=>116.36843,
   "accuracy" =>678.0
 ),
"cell_towers"=>array(
	array (
     "cell_id"=> 4466,
     "location_area_code"=> 26630,
     "mobile_country_code"=> 460,
     "mobile_network_code"=> 0,
     "signal_strength"=> -60,
   ),
   	array (
     "cell_id"=> 4466,
     "location_area_code"=> 25054,
     "mobile_country_code"=> 460,
     "mobile_network_code"=> 0,
     "signal_strength"=> -70,
   )
 ),
 
 "wifi_towers"=>array(
   array(
     "mac_address"=> "00:0B:86:27:99:B0",
     "mac_name"=>"Sina-web",
     "signal_strength"=> 78,   
   ),
   array(
     "mac_address"=> "00:0B:86:28:7B:F0",
     "mac_name"=>"CMCC",
     "signal_strength"=> 90,
   )
 ) 
 );
$data = json_encode($data);

function curlPost($url,$userPwd,$data = '',$header = ''){
    $ch = curl_init() or die (curl_error()) ;
	curl_setopt($ch,CURLOPT_URL,$url);
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
	curl_setopt($ch,CURLOPT_TIMEOUT,30);
	if(!empty($userPwd)){
		curl_setopt($ch,CURLOPT_USERPWD,$userPwd);
	}
	if(!empty($data)){
		curl_setopt($ch,CURLOPT_POST,true);
		curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
	}
	if(!empty($header)){
		curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
	}
	$result = curl_exec($ch) or die (curl_error($ch));
	curl_close($ch);
	return $result;
}
$header = array('Content-type: application/json');  

$return = curlPost($url,$userPwd,$data,$header);


交通路线读取接口

地理位置信息接口错误代码及解释

OAuth 2.0接口(V2版接口将仅支持OAuth 2.0授权方式)
//Oauth2/authorize
response_type为token
//请求用户授权Token(登录/Oauth 2.0接口)
https://api.t.sina.com.cn/oauth2/authorize?client_id=123050457758183&redirect_uri=http://www.example.com/response&response_type=token

//同意授权后会重定向
http://www.example.com/response#access_token=ACCESS_TOKEN&expires_in=250327040&refresh_token=REFRESH_TOKEN

response_type为code
//请求
https://api.t.sina.com.cn/oauth2/authorize?client_id=123050457758183&redirect_uri=http://www.example.com/response&response_type=code

//同意授权后会重定向
http://www.example.com/response&code=CODE

//oauth/access_token
//获取授权过的Access Token
$o = new WeiboOAuth( WB_AKEY , 
                     WB_SKEY , 
                     $_SESSION['keys']['oauth_token'] , 
                     $_SESSION['keys']['oauth_token_secret']  );

$last_key = $o->getAccessToken(  $_REQUEST['oauth_verifier'] ) ;
echo($last_key['oauth_token']);


旧版API接口

微博访问接口

//user/statuses/id(微博访问接口)
//根据微博ID和用户ID跳转到单条微博页面
$c = new WeiboClient( WB_AKEY , 
                      WB_SKEY , 
                      $_SESSION['last_key']['oauth_token'] , 
                      $_SESSION['last_key']['oauth_token_secret']  );
$msg = $c->user_timeline();
if ($msg === false || $msg === null){
	echo "Error occured";
	return false;
} 
if (count($msg)> 0){
	$uid = 'User ID';
	$sid = $msg[0]['id'];
	$msg  = $c->get_comments_by_sid($sid);
	if ($msg === false || $msg === null){
		echo "Error occured";
		return false;
	}
	$url = "http://api.t.sina.com.cn/".$uid."/statuses/".$sid;
	//To achieve redirection, header() must be called before any actual output is sent, such as HTML tags
	header( 'Location:'.$url );
}


账号接口
//account/end_session
$c = new WeiboClient( WB_AKEY , 
                      WB_SKEY , 
                      $_SESSION['last_key']['oauth_token'] , 
                      $_SESSION['last_key']['oauth_token_secret']  );

$msg = $c->oauth->post("http://api.t.sina.com.cn/account/end_session.json");
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['name'])){
	echo($msg['name']);
}

//account/update_profile_image
//更改头像(账号接口)(从旧版接口找到的)
$c = new WeiboClient( WB_AKEY , 
                      WB_SKEY , 
                      $_SESSION['last_key']['oauth_token'] , 
                      $_SESSION['last_key']['oauth_token_secret']  );
$pic_path = 'pic_path';
$msg = $c->update_avatar($pic_path);
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['name'])){
	echo("Successfully upload the status to [".$msg['name'].'].');
}

//account/update_profile
//更改资料(同上)
$c = new WeiboClient( WB_AKEY , 
                      WB_SKEY , 
                      $_SESSION['last_key']['oauth_token'] , 
                      $_SESSION['last_key']['oauth_token_secret']  );
//name you want to change to
$name = 'name';
//gender you want to change to
$gender = 'f';
$param = array();
$param['name'] = $name;
$param['gender']= $gender;
$msg = $c->oauth->post('http://api.t.sina.com.cn/account/update_profile.json',$param);
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['name'])){
	echo($msg['name']);
}


登录/OAuth接口
//oauth/access_token
//获取授权过的Access Token
$o = new WeiboOAuth( WB_AKEY , 
                     WB_SKEY , 
                     $_SESSION['keys']['oauth_token'] , 
                     $_SESSION['keys']['oauth_token_secret']  );

$last_key = $o->getAccessToken(  $_REQUEST['oauth_verifier'] ) ;
echo($last_key['oauth_token']);

//oauth/authorize
//请求用户授权Token
$o = new WeiboOAuth( WB_AKEY , WB_SKEY  );
$keys = $o->getRequestToken();
$aurl = $o->getAuthorizeURL( $keys['oauth_token'] ,false , 'http://localhost/callback.php');
echo($aurl);

//oauth/request_token
// 获取未授权的Request Token
$o = new WeiboOAuth( WB_AKEY , WB_SKEY  );
$keys = $o->getRequestToken();
echo($keys['oauth_token'].' : '.$keys['oauth_token_secret']);

//oauth/access_token(登录/OAuth接口)
//获取授权过的Access Token
$o = new WeiboOAuth( WB_AKEY , 
                     WB_SKEY , 
                     $_SESSION['keys']['oauth_token'] , 
                     $_SESSION['keys']['oauth_token_secret']  );

$last_key = $o->getAccessToken(  $_REQUEST['oauth_verifier'] ) ;
echo($last_key['oauth_token']);

空间计算接口
//Distance/distance line(空间计算接口)
//计算地图上线的长度
<?php
//改为你的微博账号,用户名和密码之间用:隔开
$userPwd = 'user@sina.com:pwd';
//改为你微博应用的appkey
$appkey = '0123456789';

$url = "http://api.t.sina.com.cn/location/distance/distance_line.json?xs=116.1111111,118.23321&ys=39.1122222,40.23323&source=$appkey";

$rst = curlSample($url,$userPwd);

echo $rst;

function curlSample($url,$userPwd,$postFields = '',$header = ''){
	$ch = curl_init() or die (curl_error()) ;
	curl_setopt($ch,CURLOPT_URL,$url);
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
	curl_setopt($ch,CURLOPT_TIMEOUT,30);
	if(!empty($userPwd)){
		curl_setopt($ch,CURLOPT_USERPWD,$userPwd);
	}
	if(!empty($postFields)){
		curl_setopt($ch,CURLOPT_POST,true);
		curl_setopt($ch,CURLOPT_POSTFIELDS,$postFields);
	}
	if(!empty($header)){
		curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
	}
	$result = curl_exec($ch) or die (curl_error($ch));
	curl_close($ch);
	return $result;
}
?>


//Distance/distance po li(空间计算接口)
//计算地图上(x,y)到线的最近距离
<?php
//改为你的微博账号,用户名和密码之间用:隔开
$userPwd = 'user@sina.com:pwd';
//改为你微博应用的appkey
$appkey = '0123456789';

$url = "http://api.t.sina.com.cn/location/distance/distance_po_li.xml?x1=116.1111111&y1=39.1111111&xs=116.1111111,118.23321&ys=39.1122222,40.23323&source=$appkey";

$rst = curlSample($url,$userPwd);

echo $rst;

function curlSample($url,$userPwd,$postFields = '',$header = ''){
	$ch = curl_init() or die (curl_error()) ;
	curl_setopt($ch,CURLOPT_URL,$url);
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
	curl_setopt($ch,CURLOPT_TIMEOUT,30);
	if(!empty($userPwd)){
		curl_setopt($ch,CURLOPT_USERPWD,$userPwd);
	}
	if(!empty($postFields)){
		curl_setopt($ch,CURLOPT_POST,true);
		curl_setopt($ch,CURLOPT_POSTFIELDS,$postFields);
	}
	if(!empty($header)){
		curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
	}
	$result = curl_exec($ch) or die (curl_error($ch));
	curl_close($ch);
	return $result;
}
?>

//Distance/distance point(空间计算接口)
//计算地图上两点之间的距离
<?php
//改为你的微博账号,用户名和密码之间用:隔开
$userPwd = 'user@sina.com:pwd';
//改为你微博应用的appkey
$appkey = '0123456789';

$url = "http://api.t.sina.com.cn/location/distance/distance_point.json?x1=116.1111111&y1=39.1111111&x2=116.1111111&y2=39.1122222&source=$appkey";

$rst = curlSample($url,$userPwd);

echo $rst;

function curlSample($url,$userPwd,$postFields = '',$header = ''){
	$ch = curl_init() or die (curl_error()) ;
	curl_setopt($ch,CURLOPT_URL,$url);
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
	curl_setopt($ch,CURLOPT_TIMEOUT,30);
	if(!empty($userPwd)){
		curl_setopt($ch,CURLOPT_USERPWD,$userPwd);
	}
	if(!empty($postFields)){
		curl_setopt($ch,CURLOPT_POST,true);
		curl_setopt($ch,CURLOPT_POSTFIELDS,$postFields);
	}
	if(!empty($header)){
		curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
	}
	$result = curl_exec($ch) or die (curl_error($ch));
	curl_close($ch);
	return $result;
}
?>


公交搜索接口
//Bus/station(公交搜索接口)
//公交站点名称搜索
<?php
//改为你的微博账号,用户名和密码之间用:隔开
$userPwd = 'user@sina.com:pwd';
//改为你微博应用的appkey
$appkey = '0123456789';
//查询关键词
$q = urlencode("海淀南路");

$url = "http://api.t.sina.com.cn/location/bus/station.xml?city=0010&q=$q&source=$appkey";

$rst = curlSample($url,$userPwd);

echo $rst;

function curlSample($url,$userPwd,$postFields = '',$header = ''){
	$ch = curl_init() or die (curl_error()) ;
	curl_setopt($ch,CURLOPT_URL,$url);
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
	curl_setopt($ch,CURLOPT_TIMEOUT,30);
	if(!empty($userPwd)){
		curl_setopt($ch,CURLOPT_USERPWD,$userPwd);
	}
	if(!empty($postFields)){
		curl_setopt($ch,CURLOPT_POST,true);
		curl_setopt($ch,CURLOPT_POSTFIELDS,$postFields);
	}
	if(!empty($header)){
		curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
	}
	$result = curl_exec($ch) or die (curl_error($ch));
	curl_close($ch);
	return $result;
}
?>

//Bus/line(公交搜索接口)
//线路名称搜索
<?php
//改为你的微博账号,用户名和密码之间用:隔开
$userPwd = 'user@sina.com:pwd';
//改为你微博应用的appkey
$appkey = '0123456789';
//查询关键词
$q = urlencode("320");

$url = "http://api.t.sina.com.cn/location/bus/line.xml?q=$q&city=0010&source=$appkey";

$rst = curlSample($url,$userPwd);

echo $rst;

function curlSample($url,$userPwd,$postFields = '',$header = ''){
	$ch = curl_init() or die (curl_error()) ;
	curl_setopt($ch,CURLOPT_URL,$url);
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
	curl_setopt($ch,CURLOPT_TIMEOUT,30);
	if(!empty($userPwd)){
		curl_setopt($ch,CURLOPT_USERPWD,$userPwd);
	}
	if(!empty($postFields)){
		curl_setopt($ch,CURLOPT_POST,true);
		curl_setopt($ch,CURLOPT_POSTFIELDS,$postFields);
	}
	if(!empty($header)){
		curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
	}
	$result = curl_exec($ch) or die (curl_error($ch));
	curl_close($ch);
	return $result;
}
?>

//Bus/transfer(公交搜索接口)
//根据起始id或坐标搜索公交换乘路线
<?php
//改为你的微博账号,用户名和密码之间用:隔开
$userPwd = 'user@sina.com:pwd';
//改为你微博应用的appkey
$appkey = '0123456789';

$url = "http://api.t.sina.com.cn/location/bus/transfer.xml?begin_id=P010A00CHR9&end_id=P010A00CWWJ&source=$appkey";

$rst = curlSample($url,$userPwd);

echo $rst;

function curlSample($url,$userPwd,$postFields = '',$header = ''){
	$ch = curl_init() or die (curl_error()) ;
	curl_setopt($ch,CURLOPT_URL,$url);
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
	curl_setopt($ch,CURLOPT_TIMEOUT,30);
	if(!empty($userPwd)){
		curl_setopt($ch,CURLOPT_USERPWD,$userPwd);
	}
	if(!empty($postFields)){
		curl_setopt($ch,CURLOPT_POST,true);
		curl_setopt($ch,CURLOPT_POSTFIELDS,$postFields);
	}
	if(!empty($header)){
		curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
	}
	$result = curl_exec($ch) or die (curl_error($ch));
	curl_close($ch);
	return $result;
}
?>



地址转换接口
//Geocode/geo_to_address(地址转换接口)
//根据坐标返回地址信息
<?php
//改为你的微博账号,用户名和密码之间用:隔开
$userPwd = 'user@sina.com:pwd';
//改为你微博应用的appkey
$appkey = '0123456789';

$url = "http://api.t.sina.com.cn/location/geocode/geo_to_address.xml?coordinate=116.30987,39.98437&source=$appkey";

$rst = curlSample($url,$userPwd);

echo $rst;

function curlSample($url,$userPwd,$postFields = '',$header = ''){
	$ch = curl_init() or die (curl_error()) ;
	curl_setopt($ch,CURLOPT_URL,$url);
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
	curl_setopt($ch,CURLOPT_TIMEOUT,30);
	if(!empty($userPwd)){
		curl_setopt($ch,CURLOPT_USERPWD,$userPwd);
	}
	if(!empty($postFields)){
		curl_setopt($ch,CURLOPT_POST,true);
		curl_setopt($ch,CURLOPT_POSTFIELDS,$postFields);
	}
	if(!empty($header)){
		curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
	}
	$result = curl_exec($ch) or die (curl_error($ch));
	curl_close($ch);
	return $result;
}
?>


//Geocode/address_to_geo(地址转换接口)
//根据地址返回坐标的接口
<?php
//改为你的微博账号,用户名和密码之间用:隔开
$userPwd = 'user@sina.com:pwd';
//改为你微博应用的appkey
$appkey = '0123456789';
//查询关键词
$q = urlencode("海淀");

$url = "http://api.t.sina.com.cn/location/geocode/address_to_geo.xml?address=$q&source=$appkey";

$rst = curlSample($url,$userPwd);

echo $rst;

function curlSample($url,$userPwd,$postFields = '',$header = ''){
	$ch = curl_init() or die (curl_error()) ;
	curl_setopt($ch,CURLOPT_URL,$url);
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
	curl_setopt($ch,CURLOPT_TIMEOUT,30);
	if(!empty($userPwd)){
		curl_setopt($ch,CURLOPT_USERPWD,$userPwd);
	}
	if(!empty($postFields)){
		curl_setopt($ch,CURLOPT_POST,true);
		curl_setopt($ch,CURLOPT_POSTFIELDS,$postFields);
	}
	if(!empty($header)){
		curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
	}
	$result = curl_exec($ch) or die (curl_error($ch));
	curl_close($ch);
	return $result;
}
?>

//geocode/ip_to_geo(地址转换接口)
//根据ip返回地理信息
<?php
//改为你的微博账号,用户名和密码之间用:隔开
$userPwd = 'user@sina.com:pwd';
//改为你微博应用的appkey
$appkey = '0123456789';

$url = "http://api.t.sina.com.cn/location/geocode/ip_to_geo.xml?ip=60.194.172.177&source=$appkey";

$rst = curlSample($url,$userPwd);

echo $rst;

function curlSample($url,$userPwd,$postFields = '',$header = ''){
	$ch = curl_init() or die (curl_error()) ;
	curl_setopt($ch,CURLOPT_URL,$url);
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
	curl_setopt($ch,CURLOPT_TIMEOUT,30);
	if(!empty($userPwd)){
		curl_setopt($ch,CURLOPT_USERPWD,$userPwd);
	}
	if(!empty($postFields)){
		curl_setopt($ch,CURLOPT_POST,true);
		curl_setopt($ch,CURLOPT_POSTFIELDS,$postFields);
	}
	if(!empty($header)){
		curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
	}
	$result = curl_exec($ch) or die (curl_error($ch));
	curl_close($ch);
	return $result;
}
?>




评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

迂者-贺利坚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值