douyou2368 2015-06-28 19:24
浏览 56

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

I make a PHP request that queries a database every time a map coordinate is changed. This returns some JSON data. I'd like to make a request every 5 hours, as the data does not change often. If this is not possible do you recommend generating a static JSON document that refreshes itself every 5 hours? How can I do this?

My actual code for querying the database on every request looks like this:

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条回答 默认 最新

  • douba9654 2015-06-28 19:52
    关注

    Like Kepoly said in comments, cron is solution. If you have access to server and you use Debian/Ubuntu do:

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

    And type in:

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

    This task will run "0th minute of 12am and 5am and 10am and 3pm and 8pm every day".

    For other distros there should be similar way

    If you don't have access to server you could use webcron service, like this one: http://www.mywebcron.com/

    评论

报告相同问题?

悬赏问题

  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?