gpt4 book ai didi

PHP:每 x 小时自动刷新一次的 JSON 文件

转载 作者:行者123 更新时间:2023-11-29 12:08:21 25 4
gpt4 key购买 nike

每次 map 坐标发生更改时,我都会发出一个 PHP 请求来查询数据库。这会返回一些 JSON 数据。我想每 5 小时发出一次请求,因为数据不会经常更改。如果这是不可能的,您是否建议生成一个每 5 小时刷新一次的静态 JSON 文档?我怎样才能做到这一点?

我在每个请求上查询数据库的实际代码如下所示:

public function getAgenciesJson() {
if(Request::ajax()) { # Just validation to show/send data if requested
$ne_lat = $_GET['ne_lat'];
$sw_lat = $_GET['sw_lat'];
$ne_lng = $_GET['ne_lng'];
$sw_lng = $_GET['sw_lng'];

$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$per_page = 2;

if ($page > 1) { # && $page <= $total_pages) {
$skip = ($page - 1) * $per_page;
} else {
// error - show first set of results
$skip = 0;
}

$agencies = DB::table('users')
->select('user_id','type','c_name','c_logo','c_local','state','city','sn','col','lat','lng')
->skip($skip)
->take($per_page) # Results per page
->where('active',1)
->whereNotNull('lat')
->whereNotNull('lng')
->whereRaw('lat < ? AND lat > ? AND lng < ? AND lng > ?',array($ne_lat,$sw_lat,$ne_lng,$sw_lng));

if(isset($_GET['type_l'])==true && isset($_GET['type_a'])==true) {
$agencies
->orWhere('type','l')
->where('type','a');
} elseif (isset($_GET['type_l'])==true) {
$agencies->where('type','l');
} elseif (isset($_GET['type_a'])==true) {
$agencies->where('type','a');
} else {
$agencies
->orWhere('type','l')
->where('type','a');
}

$i=0;
try {
foreach($agencies->get() as $agency) {
# Assign values
$arr[$i]['a_id'] = $agency->user_id;
$arr[$i]['type'] = $agency->type;
$arr[$i]['name'] = $agency->c_name;
$arr[$i]['logo'] = $agency->c_logo;
$arr[$i]['local'] = $agency->c_local;
$arr[$i]['state'] = $agency->state;
$arr[$i]['city'] = $agency->city;
$arr[$i]['address'] = ($agency->col) ? $agency->sn.', '.$agency->col : $agency->sn;
$arr[$i]['latlon'] = array($agency->lng,$agency->lat);#$agency->lat.",".$agency->lng;#
# Reset variables
$i++;
$latlon=NULL;
}
} catch(Exception $e) { $arr[0]['res'] = null; }

$total = $agencies->count();
$meta = array(
"page" => $page,
"per_page" => $per_page,
"count" => $total,
"total_pages"=> ceil($total/$per_page)
);

return Response::json(array('results'=>$arr,'meta'=>$meta));
} else {
App::abort(404);
}
}

最佳答案

就像 Kepoly 在评论中所说,cron 是解决方案。如果您有权访问服务器并且使用 Debian/Ubuntu,请执行以下操作:

# crontab -e -u <your webserver account, usually www-data>

并输入:

0 00,05,10,15,20 * * * php <path to your script>

此任务将在“每天中午 12 点、上午 5 点、上午 10 点、下午 3 点和晚上 8 点的第 0 分钟”运行。

对于其他发行版应该有类似的方式

如果您无权访问服务器,您可以使用 webcron 服务,如下所示:http://www.mywebcron.com/

关于PHP:每 x 小时自动刷新一次的 JSON 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31103844/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com