您的位置:首页 > 运维架构 > 网站架构

SAE 实现网站自动更新,定期发布新图片

2017-09-05 08:24 393 查看
最近捣鼓的一个美女写真图片网站运行在SAE(新浪云) , 每天都是手动更新网站烦得很。 于是想着如何实现自动更新(半自动更新)。

经过一番思索终于有办法了,新建一个 tupian_post 的数据表,用于保存图片标题、图片地址等信息,闲时将图集信息保存到这张数据表中。

图片数据有了,下一步实现自动更新,新建一个 autopost.php 文件,代码大概如下:

<?php

require( dirname( dirname(__FILE__) ) . '/system/config.php' );

# 获取上次发布图片的时间
require(SYS_PATH . '/lib/cache.class.php');

$key = 'task_is_post';
$time = intval( cache::get($key) );

if ( (time() - $time) < 3600*10 )
{
# 防止频繁执行
exit(' today is posted at ' . date('Y-m-d H:i:s', $time) );
}

/*
* 查询图片数据库
* http://luolitu.com * 获取库里的美女图片
*/
require(SYS_PATH . '/lib/mysql.class.php');
mysql::init();

$post_num = 2;
$data = mysql::finds("SELECT * FROM `tupian_temp` ORDER BY `id` ASC LIMIT {$post_num};");

if ( !$data )
{
# 库里没有图片
exit(' lib is empty ');
}

# 实例化后台相关类库  '/sb250/tupian.class.php'
require( dirname( dirname(__FILE__) ) . '/sb250/tupian.class.php' );
$obj = new tupian();

$post_i = 0; # 统计成功发布的数量

foreach ($data as $row)
{
$msg = $obj->is_post($row['id']);

if ( $msg == '恭喜你,图片发布成功!' )
{
$post_i++;
# 更新最新图片缓存
cache::delete('index_new_list_1');
cache::delete('index_new_list_2');
cache::delete('index_new_list_3');
}
}

# 任务完成 记录执行时间
if ( $post_i )
{
cache::set($key, time() );
exit(' task is complete');
}

exit('program is end');

接下来,在 SAE 新建一个“定时任务“,每天凌晨0点2分运行 autopost.php 就 ok 了。

经过测试网站已经实现自动(半自动)更新,每天凌晨0点都是自动发布新的美女图片。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  美女 网站 SAE