技术头条 - 一个快速在微博传播文章的方式     搜索本站
您现在的位置首页 --> PHP --> 嘀咕接口示范

嘀咕接口示范

浏览:1919次  出处信息

一共有三个文件。分别用于“登录”、“跳转”和“更新嘀咕”。如下:

index.php文件

01 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
02 <html xmlns="http://www.w3.org/1999/xhtml">
03 <head>
04 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
05 <title>请登录</title>
06 </head>
07 <body>
08 <form method="post" name="mblog" action="redirect.php">
09 <p>用名:<input name="username"/></p>
10 <p>密码:<input name="password" type="password"/></p>
11 <p><input type="checkbox" name="m[digu]" value="1" checked="checked"/> 嘀咕</p>
12 <p><input type="submit" value="发布"/></p>
13 </form>
14 </body>
15 </html>

redirect.php文件

01 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
02 <html xmlns="http://www.w3.org/1999/xhtml">
03 <head>
04 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
05 <title>验证中</title>
06 </head>
07 <body>
08 <?
09 if(isset($_POST['username']) && $_POST['username']!=''&& isset($_POST['password']) && $_POST['password']!=''){
10   
11     $user=$_POST['username'];
12     $pass=$_POST['password'];
13   
14     $mblog="http://api.minicloud.com.cn/account/verify.xml?isAllInfo=false";//嘀咕接口地址,以XML格式返回
15   
16     $curl=curl_init();//curl初始化
17   
18     //curl请求
19     curl_setopt($curl,CURLOPT_URL,$mblog);
20     curl_setopt($curl,CURLOPT_USERPWD,$user.":".$pass);
21     curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
22   
23     $s=curl_exec($curl);
24   
25     curl_close($curl);
26   
27     if(strpos($s,"true")!==false){
28         echo "正在跳转……";
29         setcookie("user",$user);//可以用md5()等进行加密保护
30         setcookie("psd",$pass);
31         header("location:update.php");
32     }
33     else{
34         echo "密码错误。";
35     }
36 }
37 else{
38     echo "请填写完整";
39 }
40 ?>
41 </body>
42 </html>

update.php文件

01 <?
02 if(isset($_COOKIE['user'])&&isset($_COOKIE['psd'])){//验证身份
03   
04     $user= $_COOKIE['user'];
05     $pass= $_COOKIE['psd'];
06 }
07 else{
08     header("location:index.php");
09 }
10   
11 $result = '';
12   
13 if(isset($_POST['content']) && $_POST['content']!=''){
14   
16   
17     $data='content='.urlencode($_POST['content']).'';//URL转义
18   
19     $s=postMblog($mblog,$user,$pass,$data);//调用函数,得到返回值
20   
21     if(strpos($s,"true")!==false){//更新成功则会返回“true”字样
22       $result = "更新成功!";
23     }
24     else{
25       $result = "更新失败!";
26     }
27   
28 }
29   
30 function postMblog($mblog,$user,$pass,$data){
31     $curl=curl_init();//(
32     curl_setopt($curl,CURLOPT_URL,$mblog);
33     curl_setopt($curl,CURLOPT_USERPWD,$user.":".$pass);
34     curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
35     curl_setopt($curl,CURLOPT_POSTFIELDS,$data);//发送数据
36     $s=curl_exec($curl);
37     curl_close($curl);
38     return $s;
39 }
40   
41 ?>
42 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
43 <html xmlns="http://www.w3.org/1999/xhtml">
44 <head>
45 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
46 <title>更新状态</title>
47 </head>
48 <body>
49 <form method="post" action="update.php">
50 <input id="content" name="content" type="text" />
51 <input type="submit" value="发布"/>
52 </form>
53 <?php echo $result?>
54   
55 </body>
56 </html>

参考资料

1 嘀咕接口文档 http://code.google.com/p/digu-api/wiki/DiguApi

http://www.cnblogs.com/fdszlzl/archive/2009/06/16/1504231.html

建议继续学习:

  1. 一个典型支付系统的设计与实现    (阅读:7870)
  2. 使用PHP创建一个面向对象的博客    (阅读:3941)
  3. 关于架构的一句话,还有一个实例    (阅读:3477)
  4. 如何保证一个程序在单台服务器上只有唯一实例(linux)    (阅读:3106)
  5. 如何让Perl脚本同时只运行一个实例    (阅读:2498)
  6. 人人都是交互设计师    (阅读:1656)
QQ技术交流群:445447336,欢迎加入!
扫一扫订阅我的微信号:IT技术博客大学习
© 2009 - 2024 by blogread.cn 微博:@IT技术博客大学习

京ICP备15002552号-1