简体   繁体   中英

PHP: JSON file that refreshes every x hours automatically

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);
    }
}

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM