嘀咕接口示范
一共有三个文件。分别用于“登录”、“跳转”和“更新嘀咕”。如下:
index.php文件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>请登录</title> </head> <body> <form method="post" name="mblog" action="redirect.php"> <p>用名:<input name="username"/></p> <p>密码:<input name="password" type="password"/></p> <p><input type="checkbox" name="m[digu]" value="1" checked="checked"/> 嘀咕</p> <p><input type="submit" value="发布"/></p> </form> </body> </html>
redirect.php文件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>验证中</title>
</head>
<body>
<?
if(isset($_POST['username']) && $_POST['username']!=''&& isset($_POST['password']) && $_POST['password']!=''){
$user=$_POST['username'];
$pass=$_POST['password'];
$mblog="http://api.minicloud.com.cn/account/verify.xml?isAllInfo=false";//嘀咕接口地址,以XML格式返回
$curl=curl_init();//curl初始化
//curl请求
curl_setopt($curl,CURLOPT_URL,$mblog);
curl_setopt($curl,CURLOPT_USERPWD,$user.":".$pass);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
$s=curl_exec($curl);
curl_close($curl);
if(strpos($s,"true")!==false){
echo "正在跳转……";
setcookie("user",$user);//可以用md5()等进行加密保护
setcookie("psd",$pass);
header("location:update.php");
}
else{
echo "密码错误。";
}
}
else{
echo "请填写完整";
}
?>
</body>
</html>
update.php文件
<?
if(isset($_COOKIE['user'])&&isset($_COOKIE['psd'])){//验证身份
$user= $_COOKIE['user'];
$pass= $_COOKIE['psd'];
}
else{
header("location:index.php");
}
$result = '';
if(isset($_POST['content']) && $_POST['content']!=''){
$mblog="http://api.minicloud.com.cn/statuses/update.xml";
$data='content='.urlencode($_POST['content']).'';//URL转义
$s=postMblog($mblog,$user,$pass,$data);//调用函数,得到返回值
if(strpos($s,"true")!==false){//更新成功则会返回“true”字样
$result = "更新成功!";
}
else{
$result = "更新失败!";
}
}
function postMblog($mblog,$user,$pass,$data){
$curl=curl_init();//(
curl_setopt($curl,CURLOPT_URL,$mblog);
curl_setopt($curl,CURLOPT_USERPWD,$user.":".$pass);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl,CURLOPT_POSTFIELDS,$data);//发送数据
$s=curl_exec($curl);
curl_close($curl);
return $s;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>更新状态</title>
</head>
<body>
<form method="post" action="update.php">
<input id="content" name="content" type="text" />
<input type="submit" value="发布"/>
</form>
<?php echo $result?>
</body>
</html>
参考资料
01 《嘀咕接口文档》 http://code.google.com/p/digu-api/wiki/DiguApi
02 拖鞋 《使用PHP同时更新Twitter、饭否、嘀咕和做啥》http://www.cnblogs.com/fdszlzl/archive/2009/06/16/1504231.html