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

本文涉及的产品
公网NAT网关,每月750个小时 15CU
简介: 相关文章: 一、PHP+新浪微博开放平台+新浪云平台(SAE)方案的基础二、建立微博应用的过程三、PHP SDK中Demo程序简析四、进一步学习的走向和有用的资源五、必须交待的几个问题【PDF全文下载】   本文是新浪微博旧版API中的PHP例程,可以作为初学时阅读和试探的参考材料。   API 文档的旧版接口文档提供了一些PHP示例(新浪对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;
}
?>




目录
相关文章
|
24天前
|
JSON 数据挖掘 API
如何使用PHP开发1688商品详情API接口
本文详细介绍了如何使用PHP开发1688商品详情API接口,涵盖从注册账号、申请权限、配置环境到代码实现的全过程。通过设置请求头、参数及生成签名,利用cURL或GuzzleHttp库发送请求并处理响应,最终实现商品详情数据的获取与应用,助力电商发展。
31 1
|
1月前
|
API PHP 数据库
PHP中哪个框架最适合做API?
在数字化时代,API作为软件应用间通信的桥梁至关重要。本文探讨了PHP中适合API开发的主流框架,包括Laravel、Symfony、Lumen、Slim、Yii和Phalcon,分析了它们的特点和优势,帮助开发者选择合适的框架,提高开发效率、保证接口稳定性和安全性。
59 3
|
1月前
|
XML JSON API
【PHP开发专栏】PHP RESTful API设计与开发
随着互联网技术的发展,前后端分离成为Web开发的主流模式。本文介绍RESTful API的基本概念、设计原则及在PHP中的实现方法。RESTful API是一种轻量级、无状态的接口设计风格,通过HTTP方法(GET、POST、PUT、DELETE)操作资源,使用JSON或XML格式传输数据。在PHP中,通过定义路由、创建控制器、处理HTTP请求和响应等步骤实现RESTful API,并强调了安全性的重要性。
31 2
|
1月前
|
JSON API PHP
如何使用PHP开发API接口?
本文详细介绍了如何使用PHP开发API接口,涵盖从基础概念到实战步骤的全过程。首先解释了API接口的基本原理,包括HTTP协议、REST架构风格、JSON格式和OAuth认证机制。接着介绍了开发环境的设置,包括PHP安装、Web服务器配置、数据库设置等。文章还探讨了API开发的完整流程,从需求确定、框架选择、端点设计到代码编写、测试、安全性考量及性能优化。最后通过一个实战案例演示了如何创建一个简单的API端点,并讨论了部署与监控的方法。
77 0
|
5月前
|
Java API PHP
【亲测有效,官方提供】php版本企查查api接口请求示例代码,php请求企查查api接口,thinkphp请求企查查api接口
【亲测有效,官方提供】php版本企查查api接口请求示例代码,php请求企查查api接口,thinkphp请求企查查api接口
170 1
|
6月前
|
JSON 安全 API
实战指南:使用PHP构建高性能API接口服务端
构建RESTful API的简要指南:使用PHP和Laravel,先安装Laravel并配置数据库,接着在`api.php`中定义资源路由,创建`PostController`处理CRUD操作,定义`Post`模型与数据库交互。使用Postman测试API功能,如创建文章。别忘了关注安全性、错误处理和性能优化。
160 2
|
5月前
|
API PHP 开发者
​邮件发送API示例代码,AOKSend关于PHP示例代码
**邮件发送API简化了开发中的邮件功能,如自动化通知和营销邮件。常见示例包括SendGrid和Mailgun的Python代码。AOKSend在PHP中的使用示例展示了如何使用Guzzle库发送邮件,其特点包括高可靠性、易于集成和强大功能。通过API,开发者能高效、可靠地发送大规模邮件。**
|
6月前
|
存储 程序员 API
老程序员分享:PHP对接饿了么开放平台【企业】接单
老程序员分享:PHP对接饿了么开放平台【企业】接单
|
7月前
|
人工智能 自然语言处理 API
零一万物API开放平台,正式向开发者开放了!
零一万物API开放平台向开发者开放,提供多领域AI模型,包括自然语言处理、图像识别和语音识别,助力开发者轻松实现智能化功能。平台以简单API调用实现易用性,高性能计算资源保证服务稳定性。按需付费模式降低成本,免费体验机会鼓励尝试。全面的开发者支持包括详细文档、技术支持和定期技术交流会,构建友好社区。开发者需注意账户余额管理。访问平台:&lt;https://platform.lingyiwanwu.com/playground&gt;。
190 6
零一万物API开放平台,正式向开发者开放了!
|
7月前
|
XML JSON API
【PHP开发专栏】PHP RESTful API设计与开发
【4月更文挑战第29天】本文探讨了在Web开发中流行的前后端分离模式,重点介绍了RESTful API的设计与实现。REST是一种基于HTTP协议的架构风格,核心概念包括资源、表述和状态转换。RESTful API设计遵循无状态、统一接口等原则,使用GET、POST、PUT、DELETE等HTTP方法执行操作,并通过状态码和JSON/XML传输数据。在PHP中实现RESTful API,可通过定义路由、创建控制器、处理请求和响应,同时注意安全性措施,如使用HTTPS。文中还提供了一个用户管理API的实战示例,以帮助读者更好地理解和应用RESTful API。
105 0