File: /home/awaldron/_buildinmotion/bim_ci/app/Controllers/SiteMap.php
<?php namespace App\Controllers;
class SiteMap extends BaseController
{
public function index()
{
$url = 'https://hub.bimcatalyst.dev/api/v2/objects/data/Metros?size=10000&i=0&q=&t=&sort=asc';
$authToken = 'Jy23Ns2zSEiOjuBipq+9Mw==';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url); // Set the request URL
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return the response as a string
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: Bearer $authToken", // Set the Authorization header
"Content-Type: application/json" // Optional: Specify content type if sending/receiving JSON
]);
$response = curl_exec($ch);
$metros = json_decode( $response, true );
$metros = $metros['r'];
usort($metros, function ($a, $b) {
// Compare by StateAbbr first
$stateComparison = strcmp($a['StateAbbr'], $b['StateAbbr']);
if ($stateComparison === 0) {
// If StateAbbr is the same, compare by City
return strcmp($a['City'], $b['City']);
}
return $stateComparison;
});
//echo '<pre>', print_r($metros), '</pre>';
$curr_state = '';
$out = '';
foreach ($metros as $m) {
if ($curr_state != $m['StateAbbr']) {
$out .= '<url>'."\n";
$out .= ' <loc>https://buildinmotion.com/local/state/'.strtolower($m['StateAbbr']).'</loc>'."\n";
$out .= ' <lastmod>'.date('Y-m-d').'</lastmod>'."\n";
$out .= '</url>'."\n";
$curr_state = $m['StateAbbr'];
}
$city = strtolower(str_ireplace(' ', '-', $m['City']));
$city = preg_replace("/[^A-Za-z0-9\-]/", '', $city);
$out .= '<url>'."\n";
$out .= ' <loc>https://buildinmotion.com/local/'.strtolower($m['StateAbbr']).'/'.$city.'</loc>'."\n";
$out .= ' <lastmod>'.date('Y-m-d').'</lastmod>'."\n";
$out .= '</url>'."\n";
}
echo $out;
}
//--------------------------------------------------------------------
}