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

时间:2015-06-28 19:24:54

标签: php mysql json laravel

我发出一个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);
    }
}

1 个答案:

答案 0 :(得分:1)

就像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点运行&#34;

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

如果您无法访问服务器,则可以使用webcron服务,例如:http://www.mywebcron.com/