Skip to content

Instantly share code, notes, and snippets.

@Epicpkmn11
Last active April 21, 2022 21:28
Show Gist options
  • Save Epicpkmn11/6e60adbf3475277b753b259ab02a53cd to your computer and use it in GitHub Desktop.
Save Epicpkmn11/6e60adbf3475277b753b259ab02a53cd to your computer and use it in GitHub Desktop.
nextrip.php - PHP webpage for getting NexTrip stop info https://nextrip.xn--rck9c.xn--tckwe/
<?php /*
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
*/ ?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>停留所のNexTrip情報</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<style>
main {
max-width: 768px;
}
h2 {
border-bottom: 1px solid lightgray;
}
</style>
</head>
<body>
<?php
// このページのURL
$self = "https://nextrip.xn--rck9c.xn--tckwe/";
// formの変数
$stop = $_GET['stop'];
$favSet = $_POST['fav-set'];
$favRemove = $_POST['fav-remove'];
?>
<header>
<nav class="navbar navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="<?php echo $self; ?>">停留所のNexTrip情報</a>
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="https://gist.github.com/Epicpkmn11/6e60adbf3475277b753b259ab02a53cd" target="_blank">ソース</a>
</li>
</ul>
</div>
</nav>
</header>
<main class="mt-3 container-fluid">
<?php
date_default_timezone_set('America/Chicago');
if(isset($stop)) { // 停留所情報を表示
$directions = [
"NB" => "北行",
"EB" => "東行",
"SB" => "南行",
"WB" => "西行"
];
// APIデータを取得
$content = file_get_contents("https://svc.metrotransit.org/NexTrip/$stop?format=json");
$json = json_decode($content);
if(isset($json)) {
// 情報をプリント
echo "<h2>停留所{$stop}のNexTrip情報</h2>";
echo '<div class="row row-cols-2 row-cols-sm-4 row-cols-md-6">';
foreach($json as $bus) {
if(strchr($bus->DepartureText, ':')) {
preg_match("/(\d+)000/", $bus->DepartureTime, $matches);
$time = date('H:i', $matches[1]);
} else {
$time = preg_replace("/ Min/", "分", $bus->DepartureText);
$time = preg_replace("/Due/", "今すぐ", $time);
}
echo '<div class="col">';
echo "<p><strong>$bus->Route{$bus->Terminal}({$directions[$bus->RouteDirection]})</strong><br>$time</p>";
echo '</div>';
}
echo '</div>';
// お気に入りフォームをプリント
$favDesc = json_decode($_COOKIE['favorites'], true)[$stop];
echo "<form method=\"post\" action=\"$self\">";
echo "<input type=\"hidden\" name=\"fav-set\" value=\"$stop\">";
echo '<div class="input-group">';
echo '<label for="desc" class="input-group-text">お気に入り</label>';
echo "<input type=\"text\" id=\"desc\" name=\"desc\" class=\"form-control\" placeholder=\"説明\" value=\"$favDesc\" required>";
echo '<input type="submit" class="btn btn-outline-secondary" value="' . (isset($favDesc) ? '更新' : '追加') . '">';
echo '</div>';
echo '</form>';
} else {
echo "<div class=\"alert alert-danger\">{$stop}は無効な停留所です。</div>";
}
} else { // HPを表示
// 停留所フォームをプリント
echo "<form method=\"get\" action=\"$self\">";
echo '<div class="input-group mb-3">';
echo '<label for="stop" class="input-group-text">停留所</label>';
echo '<input type="number" min="0" id="stop" name="stop" class="form-control" placeholder="12345" required>';
echo '<input type="submit" class="btn btn-outline-secondary" value="検索">';
echo '</div>';
echo '</form>';
// お気に入り一覧
$favorites = isset($_COOKIE['favorites']) ? json_decode($_COOKIE['favorites'], true) : Array();
if(isset($favSet)) { // お気に入りに追加
$desc = $_POST['desc'];
$favorites[$favSet] = $desc;
ksort($favorites);
setcookie("favorites", json_encode($favorites), 2147483647);
echo "<div class=\"alert alert-info\">{$favSet}({$desc})は設定しました。</div>";
} else if(isset($favRemove)) { // お気に入りから削除
if(array_key_exists($favRemove, $favorites)) {
$desc = $favorites[$favRemove];
unset($favorites[$favRemove]);
setcookie("favorites", json_encode($favorites), 2147483647);
echo "<div class=\"alert alert-info\">{$favRemove}({$desc})は削除しました。</div>";
} else {
echo "<div class=\"alert alert-warning\">{$favRemove}はお気に入りにありません。</div>";
}
}
// お気に入りがある場合、プリント
if(count($favorites) > 0) {
echo '<h2>お気に入り</h2>';
echo "<form action=\"$self\">";
echo '<ul class="list-unstyled">';
foreach($favorites as $favStop => $favDesc) {
echo '<li class="mb-1 d-flex">';
echo "<button type=\"submit\" formmethod=\"get\" name=\"stop\" value=\"$favStop\" class=\"btn btn-sm btn-secondary flex-fill me-1 text-start\">$favStop ($favDesc)</button>";
echo "<button type=\"submit\" formmethod=\"post\" name=\"fav-remove\" value=\"$favStop\" aria-label=\"{$favStop}を削除\" class=\"btn btn-sm btn-danger\">×</button>";
echo '</li>';
}
echo '</ul>';
echo '</form>';
}
}
?>
</main>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment