/** * lock around 20 - next 20 * * @return void * @author **/ public function generateXML() { \Log::info('Running Sitemap XML Generator @' . date('Y-m-d H:i:s')); ////////////// // GET AT // ////////////// $credentials['grant_type'] = 'client_credential'; $credentials['key'] = env('CAPCUS_API_KEY', 'asdfghjkl'); $credentials['secret'] = env('CAPCUS_API_SECRET', 'asdfghjkl'); $api_url = env('API_URL', 'http://capcusapi'); $api = new \GuzzleHttp\Client(['base_url' => $api_url]); $api_result = json_decode($api->post($api_url . '/authorized/client', ['form_params' => $credentials])->getBody()); if (str_is('success', $api_result->status)) { \Session::put('access_token', $api_result->data->access_token); \Session::put('refresh_token', $api_result->data->refresh_token); \Session::put('expired_at', $api_result->data->expired_at); } else { \Log::error('Crontab error access token'); } ////////////// // API DATA // ////////////// $limit = 50000; $query_data['skip'] = 0; $query_data['take'] = 50; $query_data['scheduled_since'] = date("Y-m-d"); $query_data['with_travel_agent'] = true; $query_data['with_count'] = 1; $tours = API::GetLatestTours($query_data); $total_tours = $tours['count']; $connection = ceil($total_tours / $query_data['take']); $total_xml = floor($total_tours / $limit); //How many xml made foreach (range(0, $total_xml) as $key) { $current_tours = []; //in every xml made need to make many connection foreach (range(0, min($limit / $query_data['take'], $connection)) as $key2 => $value2) { $query_data['skip'] = (int) ((ceil($total_tours / $query_data['take']) - $connection) * $query_data['take']); $next_tours = API::GetLatestTours($query_data); $current_tours = array_merge($current_tours, $next_tours['data']); $connection = $connection - 1; } //get file xml $my_file = env('SITEMAP_GENERATOR_TARGET') . '/sitemap' . ($key + 1) . '.xml'; $handle = fopen($my_file, 'w') or die('Cannot open file: ' . $my_file); //implicitly creates file $header = '<?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"> <!-- created with Free Online Sitemap Generator www.xml-sitemaps.com -->'; fwrite($handle, $header); //re write foreach ($current_tours as $key3 => $value3) { $data = "\n" . '<url> <loc>http://capcus.id/tours/d' . $value3->travel_agent->slug . '/' . \Carbon\Carbon::parse($value3->published_at)->year . '/' . $value3->slug . '</loc> <changefreq>always</changefreq> </url>'; fwrite($handle, $data); } $footer = "\n" . '</urlset>'; fwrite($handle, $footer); } return true; }
function sitemap() { //////////////////// // Calculate Page // //////////////////// $page = max(1, request()->get('page')); $per_page = 50000; $skip = ($page - 1) * $per_page; $filters = []; ////////////// // API DATA // ////////////// $query_data['skip'] = $skip; $query_data['take'] = $per_page; $query_data['scheduled_since'] = date("Y-m-d"); $query_data['with_travel_agent'] = true; $query_data['with_count'] = 1; $tours = API::GetLatestTours($query_data); $this->layout->page = view($this->version . '.sitemap'); $this->layout->page->filters = $filters; $this->layout->page->subtitle = $subtitle; $this->layout->page->tour_count = $tours['count']; $this->layout->page->tours = $tours['data']; $this->layout->page->per_page = $per_page; $this->layout->page->current_page = $page; $this->layout->page->total_page = ceil($tours['count'] / $per_page); $this->layout->title = 'Paket Tour ' . $subtitle . ' - Capcus.id'; return $this->layout; }