昨天在《5月23日》提到准备写三个程序,今天花了点时间把第一个写完了——每天能够自动提示女朋友去睡觉的程序。
简单的思路是每当0点到5点之间,发现她又有新的微博出现的时候,程序自动去留一条言,提示她去睡觉。
程序主要分三大部分,第一步微博应用的认证,第二部设置相关参数,第三部定时检查是否有新微薄出现。
第一步,略。
第二部,主要保存用户自己的id,Ta的id,Ta最后条微博的id、时间,检查的开始时间、结束时间,你要留给Ta的话,以及微博账号认证的oauth_token、oauth_token_secret。

weibolist.php
<?php
header("Content-type: text/HTML; charset=utf-8");
session_start();
include_once( 'config.php' );
include_once( 'weibooauth.php' );
$url=$_SERVER['HTTP_HOST'].$_SERVER["PHP_SELF"];
if (!isset($_POST['u_id']) && !isset($_POST['comments']) && !isset($_POST['begin_time']) && !isset($_POST['end_time']))
{
echo <<< EOT
<form action="http://{$url}" method="post">
Ta微博的昵称
<input type="text" name="u_id" size="15" maxlength="20" title="Ta微博的昵称" /><br>
想要给TA留的言
<input type="text" name="comments" size="15" maxlength="200" title="想要给TA留的言" /><br>
起始时间(24小时制)
<input type="text" name="begin_time" size="15" maxlength="6" title="起始时间" /><br>
终止时间(24小时制)
<input type="text" name="end_time" size="15" maxlength="6" title="终止时间" /><br>
<input type="submit" value="提交" name="name">
</form>
EOT;
}
else
{
include_once( 'conn.php' );//连接数据库
$c = new WeiboClient( WB_AKEY ,
WB_SKEY ,
$_SESSION['last_key']['oauth_token'] ,
$_SESSION['last_key']['oauth_token_secret'] );
$u_id=$_POST['u_id'];
$comments=$_POST['comments'];
$begin_time=$_POST['begin_time'];
$end_time=$_POST['end_time'];
$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;
}
$last_weibo_id=$msg['status']['id'];
$oauth_token=$_SESSION['last_key']['oauth_token'];
$oauth_token_secret=$_SESSION['last_key']['oauth_token_secret'];
$me = $c->verify_credentials();
$u_my_id=$me['id'];
$mysql = "SELECT * FROM goodnight WHERE u_my_id = '".$u_my_id."' ";
$result = mysql_query($mysql) or die(mysql_error());
$num = mysql_num_rows($result);
if($num==0)
{
mysql_query("INSERT INTO goodnight (u_my_id,oauth_token,oauth_token_secret,u_id,last_weibo_id,begin_time,end_time,comments) VALUES ('".$u_my_id."','".$oauth_token."','".$oauth_token_secret."','".$u_id."','".$last_weibo_id."','".$begin_time."','".$end_time."','".$comments."')")or die(mysql_error());
echo "保存成功<br>";
}
else
{
$mysql = "UPDATE goodnight SET u_id = '".$u_id."',last_weibo_id = '".$last_weibo_id."',begin_time = '".$begin_time."',end_time = '".$end_time."',comments = '".$comments."' WHERE u_my_id = '".$u_my_id."'";
$result = mysql_query($mysql) or die(mysql_error());
echo "修改成功<br>";
}
echo <<< EOT
{$u_id}每天在{$begin_time}和{$end_time}之间还在发微博的话,系统将自动留言:{$comments}到微博{$last_weibo_id}
EOT;
}
?>
<?php
header("Content-type: text/HTML; charset=utf-8");
session_start();
include_once( 'config.php' );
include_once( 'weibooauth.php' );
$url=$_SERVER['HTTP_HOST'].$_SERVER["PHP_SELF"];
if (!isset($_POST['u_id']) && !isset($_POST['comments']) && !isset($_POST['begin_time']) && !isset($_POST['end_time']))
{
echo <<< EOT
<form action="http://{$url}" method="post">
Ta微博的昵称
<input type="text" name="u_id" size="15" maxlength="20" title="Ta微博的昵称" /><br>
想要给TA留的言
<input type="text" name="comments" size="15" maxlength="200" title="想要给TA留的言" /><br>
起始时间(24小时制)
<input type="text" name="begin_time" size="15" maxlength="6" title="起始时间" /><br>
终止时间(24小时制)
<input type="text" name="end_time" size="15" maxlength="6" title="终止时间" /><br>
<input type="submit" value="提交" name="name">
</form>
EOT;
}
else
{
include_once( 'conn.php' );//连接数据库
$c = new WeiboClient( WB_AKEY ,
WB_SKEY ,
$_SESSION['last_key']['oauth_token'] ,
$_SESSION['last_key']['oauth_token_secret'] );
$u_id=$_POST['u_id'];
$comments=$_POST['comments'];
$begin_time=$_POST['begin_time'];
$end_time=$_POST['end_time'];
$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;
}
$last_weibo_id=$msg['status']['id'];
$oauth_token=$_SESSION['last_key']['oauth_token'];
$oauth_token_secret=$_SESSION['last_key']['oauth_token_secret'];
$me = $c->verify_credentials();
$u_my_id=$me['id'];
$mysql = "SELECT * FROM goodnight WHERE u_my_id = '".$u_my_id."' ";
$result = mysql_query($mysql) or die(mysql_error());
$num = mysql_num_rows($result);
if($num==0)
{
mysql_query("INSERT INTO goodnight (u_my_id,oauth_token,oauth_token_secret,u_id,last_weibo_id,begin_time,end_time,comments) VALUES ('".$u_my_id."','".$oauth_token."','".$oauth_token_secret."','".$u_id."','".$last_weibo_id."','".$begin_time."','".$end_time."','".$comments."')")or die(mysql_error());
echo "保存成功<br>";
}
else
{
$mysql = "UPDATE goodnight SET u_id = '".$u_id."',last_weibo_id = '".$last_weibo_id."',begin_time = '".$begin_time."',end_time = '".$end_time."',comments = '".$comments."' WHERE u_my_id = '".$u_my_id."'";
$result = mysql_query($mysql) or die(mysql_error());
echo "修改成功<br>";
}
echo <<< EOT
{$u_id}每天在{$begin_time}和{$end_time}之间还在发微博的话,系统将自动留言:{$comments}到微博{$last_weibo_id}
EOT;
}
?>
第三部,读取数据库存的最后条微博的ID和目前最后条微博ID是否相同,如果不同,并且在时间内,则触法评论功能。其中每次评论需要不同,否则会出现BUG。
goodnight.php
<?php
header("Content-type: text/HTML; charset=utf-8");
echo <<< EOT
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>goodnight</title>
<meta http-equiv=refresh content="60">
</head>
EOT;
ignore_user_abort(1);
set_time_limit(0);
date_default_timezone_set('Asia/Shanghai');
include_once( 'conn.php' );//连接数据库
include_once( 'config.php' );
include_once( 'weibooauth.php' );
$result = mysql_query("SELECT * FROM goodnight ");
while($row = mysql_fetch_array($result))
{
$u_my_id=$row['u_my_id'];
$oauth_token=$row['oauth_token'];
$oauth_token_secret=$row['oauth_token_secret'];
$u_id=$row['u_id'];
$last_weibo_id=$row['last_weibo_id'];
$begin_time=$row['begin_time'];
$end_time=$row['end_time'];
$comments=$row['comments'];
$c = new WeiboClient( WB_AKEY ,
WB_SKEY ,
$oauth_token,
$oauth_token_secret );
$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;
}
$sid=$msg['status']['id'];//获取最近评论ID
if($sid!=$last_weibo_id)
{
$weibo_time=$msg['status']['created_at'];//获取最近一条微博时间
$weibo_time=date('H:i:s', strtotime($weibo_time));
$comments="都".$weibo_time."了,".$comments;//避免每次回复内容相同,否则会报错
if(($weibo_time<$end_time&&$weibo_time>$begin_time&&$begin_time<$end_time)||($begin_time>$end_time&&$weibo_time>$begin_time)||($begin_time>$end_time&&$weibo_time<$end_time))
{
$msg = $c->send_comment($sid,$comments,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 "发送成功";
$mysql = "UPDATE goodnight SET last_weibo_id = '".$sid."' WHERE u_my_id = '".$u_my_id."'";
$result = mysql_query($mysql) or die(mysql_error());
}
else
{
echo "未在时间范围内不修改";
}
}
else
{echo "这条微博是以前的";}
}
$now=date('Y-m-d H:i:s');
echo "<br>".$now;
?>