駅すぱあとAPIフリープランをローカルで実行するとエラー発生したので解決していく
はじめに
APIを試してみたく、駅すぱあとAPIフリープランに申し込んだ。


申し込みから2日後に環境ができたとの連絡が入った。

名古屋駅から高山駅の所要時間と運賃を取得するPythonソースコード作成
以下のPythonコードのAPIキーを自分の環境のものに書き換えた。
import requests
import urllib.parse
# ▼あなたのAPIキーをここに記入してください
API_KEY = 'ここにあなたのAPIキーを入力'
# ▼出発駅と到着駅
from_station = '名古屋駅'
to_station = '高山駅'
# ▼URLエンコード(例:名古屋 → %E5%90%8D%E5%8F%A4%E5%B1%8B)
from_encoded = urllib.parse.quote(from_station)
to_encoded = urllib.parse.quote(to_station)
# ▼APIのエンドポイント
url = f'https://api.ekispert.jp/v1/json/search/course/light?key={API_KEY}&from={from_encoded}&to={to_encoded}'
# ▼APIリクエスト
response = requests.get(url)
# ▼結果の処理
if response.status_code == 200:
data = response.json()
course = data['ResultSet']['Course'][0]
# 所要時間の取得
time = course['Route']['timeOnBoard']
print(f"{from_station} → {to_station} の所要時間は {time} 分です。")
# 運賃の取得
price = course['Price'][0]['Oneway']
print(f"片道の運賃は {price} 円です。")
else:
print("APIリクエストに失敗しました。ステータスコード:", response.status_code)
Pythonコード実行で登録ドメインでAPIリクエストを送っていないことによるエラー発生
このPythonコードを実行するとエラーが発生した。
(.venv) Mac:eki-api shibatahiroshitaka$ /Users/shibatahiroshitaka/Downloads/python/eki-api/.venv/bin/python /Users/shibatahiroshitaka/Downloads/python/eki-api/eki-api.py
/Users/shibatahiroshitaka/Downloads/python/eki-api/.venv/lib/python3.8/site-packages/urllib3/__init__.py:35: NotOpenSSLWarning: urllib3 v2 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'LibreSSL 2.8.3'. See: https://github.com/urllib3/urllib3/issues/3020
warnings.warn(
APIリクエストに失敗しました。ステータスコード: 400
登録ドメインがki-hi-ro.comだったのに、ローカルで開発を行なっていたのが原因。


ki-hi-ro.comが稼働しているエックスサーバー上にAPIプロキシ(中継スクリプト)を設置し、それ経由で駅すぱあとAPIにアクセスさせればOK。
【解決策】中継PHPファイルを作成

<?php
header("Content-Type: application/json");
// APIキー
$apiKey = '';
// パラメータの取得
$from = urlencode($_GET['from'] ?? '');
$to = urlencode($_GET['to'] ?? '');
// 駅すぱあとAPIのURL構築
$url = "https://api.ekispert.jp/v1/json/search/course/light?key={$apiKey}&from={$from}&to={$to}";
// 駅すぱあとAPIへリクエスト
$response = file_get_contents($url);
// 結果をそのまま返却
echo $response;
作成した中継PHPファイルを登録ドメインのサーバにアップ
エックスサーバーとのやり取りは、Cyberduckを利用している。
public_htmlにapiフォルダを作成した。

ローカルで作成したekispert_proxy.phpをアップした。

php.iniではなくcURLを使用する書き方に修正
このURLにアクセスするとエラーが発生した。

以下の解決策を試してみる。

php.iniの場所

phpinfo.phpを作成してアップした。

https://ki-hi-ro.com/phpinfo.phpにアクセスして、Loaded Configuration FIleを探した。

/etc/php-fpm/php80-php.ini
etcフォルダが見当たらなかった。

共有レンタルサーバーであるXserverを使用しているため、直接アクセスできなかった。
cURLを利用する方法がいいらしい。

先ほどアップしたekispert_proxy.phpをcURLを使用する方法に書き換えた。

file_get_contents()がhttpsを扱えない環境をcURLを使って解決した。
ブラウザでhttps://ki-hi-ro.com/api/ekispert_proxy.php?from=名古屋駅&to=高山駅にアクセスすると、JSONが返ってくるらしい。

正しい駅名を入力しないとエラーが返ってくる
エラーが返ってきた。

{
"ResultSet": {
"apiVersion": "1.27.0.0",
"engineVersion": "202507_02a",
"Error": {
"code": "E102",
"Message": "駅名が見つかりません。(名古屋駅)"
}
}
}
エンコードしないといけないようなので、以下のURLに変更してみる。
https://ki-hi-ro.com/api/ekispert_proxy.php?from=%E5%90%8D%E5%8F%A4%E5%B1%8B%E9%A7%85&to=%E9%AB%98%E5%B1%B1%E9%A7%85
結果は変わらなかった。アドレスバーに入力すると勝手にデコードされているという現象もあった。
名古屋駅という文字列がEkispert APIの辞書になかったのが原因。
正しい駅名を入力したら、正しい結果が返ってきた
名古屋と高山に修正したら、エラーが出なくなった。

{
"ResultSet": {
"apiVersion": "1.27.0.0",
"engineVersion": "202507_02a",
"ResourceURI": "https://roote.ekispert.net/result?arr=%E9%AB%98%E5%B1%B1&arr_code=24492&connect=true&dep=%E5%90%8D%E5%8F%A4%E5%B1%8B&dep_code=25077&express=true&highway=true&hour&liner=true&local=true&minute&plane=true&shinkansen=true&ship=true&sleep=false&sort=time&surcharge=3&type=dep&via1=&via1_code=&via2=&via2_code="
}
}
ResourceURIにアクセスすることができた。

https://roote.ekispert.net/result?arr=%E9%AB%98%E5%B1%B1&arr_code=24492&connect=true&dep=%E5%90%8D%E5%8F%A4%E5%B1%8B&dep_code=25077&express=true&highway=true&hour&liner=true&local=true&minute&plane=true&shinkansen=true&ship=true&sleep=false&sort=time&surcharge=3&type=dep&via1=&via1_code=&via2=&via2_code=
【成功】ローカルのPythonスクリプトからAPIを送信する
ローカルのPythonスクリプトでPHPプロキシスクリプトを呼び出す。

import requests
params = {
"from": "名古屋",
"to": "高山"
}
res = requests.get("https://ki-hi-ro.com/api/ekispert_proxy.php", params=params)
data = res.json()
print(data)
実行すると以下が取得できた。
{'ResultSet': {'apiVersion': '1.27.0.0', 'engineVersion': '202507_02a', 'ResourceURI': 'https://roote.ekispert.net/result?arr=%E9%AB%98%E5%B1%B1&arr_code=24492&connect=true&dep=%E5%90%8D%E5%8F%A4%E5%B1%8B&dep_code=25077&express=true&highway=true&hour&liner=true&local=true&minute&plane=true&shinkansen=true&ship=true&sleep=false&sort=time&surcharge=3&type=dep&via1=&via1_code=&via2=&via2_code='}}
正しい駅名のリストを取得したい
正しい駅名を入力しないとAPIが正しく取得されない。
そのため、既存の駅名データCSVを使用していく。


Mac:Applications shibatahiroshitaka$ curl -O https://nlftp.mlit.go.jp/ksj/gml/data/N02/N02-2023/N02-23_GML.zip
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 19885 0 19885 0 0 8603 0 --:--:-- 0:00:02 --:--:-- 8641
アプリケーションフォルダにインストールしてしまったzipファイルをダウンロードフォルダに移動させた。

アーカイブユーティリティで開けなかった。

The Unarchiverで上手く行くらしい。


The Unarchiverでも展開することができなかった。

7zipでやってみる。

ターミナルでコマンドを叩いた。

Mac:Downloads shibatahiroshitaka$ brew install p7zip
==> Auto-updating Homebrew...
Adjust how often this is run with HOMEBREW_AUTO_UPDATE_SECS or disable with
HOMEBREW_NO_AUTO_UPDATE. Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
==> Downloading https://ghcr.io/v2/homebrew/portable-ruby/portable-ruby/blobs/sha256:45cea656cc5b5f5b53a9d4fc9e6c88d3a29b3aac862d1a55f1c70df534df5636
##############################################################
インストールできた。

以下のコマンドで、7zipを使って解凍しようとしたが、エラーが出た。
Mac:Downloads shibatahiroshitaka$ 7z x ~/Downloads/N02-23_GML.zip -o~/Downloads/station_data
7-Zip [64] 17.05 : Copyright (c) 1999-2021 Igor Pavlov : 2017-08-28
p7zip Version 17.05 (locale=utf8,Utf16=on,HugeFiles=on,64 bits,8 CPUs x64)
Scanning the drive for archives:
1 file, 19885 bytes (20 KiB)
Extracting archive: /Users/shibatahiroshitaka/Downloads/N02-23_GML.zip
ERROR: /Users/shibatahiroshitaka/Downloads/N02-23_GML.zip
/Users/shibatahiroshitaka/Downloads/N02-23_GML.zip
Open ERROR: Can not open the file as [zip] archive
ERRORS:
Is not archive
Can't open as archive: 1
Files: 0
Size: 0
Compressed: 0
ダウンロードされたZIPファイルが破損しているなどが原因らしい。

現在のzipファイルを削除して、再ダウンロードした。
Mac:Downloads shibatahiroshitaka$ rm ~/Downloads/N02-23_GML.zip
Mac:Downloads shibatahiroshitaka$ curl -o ~/Downloads/N02-23_GML.zip https://nlftp.mlit.go.jp/ksj/gml/data/N02/N02-2023/N02-23_GML.zip
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 20062 0 20062 0 0 17463 0 --:--:-- 0:00:01 --:--:-- 17613
それを7zipで解凍しようとしたが、再びエラーが出た。
Mac:Downloads shibatahiroshitaka$ 7z x ~/Downloads/N02-23_GML.zip -o~/Downloads/station_data
7-Zip [64] 17.05 : Copyright (c) 1999-2021 Igor Pavlov : 2017-08-28
p7zip Version 17.05 (locale=utf8,Utf16=on,HugeFiles=on,64 bits,8 CPUs x64)
Scanning the drive for archives:
1 file, 20062 bytes (20 KiB)
Extracting archive: /Users/shibatahiroshitaka/Downloads/N02-23_GML.zip
ERROR: /Users/shibatahiroshitaka/Downloads/N02-23_GML.zip
/Users/shibatahiroshitaka/Downloads/N02-23_GML.zip
Open ERROR: Can not open the file as [zip] archive
ERRORS:
Is not archive
Can't open as archive: 1
Files: 0
Size: 0
Compressed: 0
原因はソースの国交省のサイトが404エラーになっていたこと。


正しいURLを教えてもらった。

これもダメだった。
古い情報のようだ。
GPTは2024年6月までの情報しかインプットしていない。

2024年6月時点だと、国交省の鉄道データは、2023年版までしか存在しない。

最新の2024年度版をホームページから直接ダウンロードしよう。


これを7zipで展開する。
しっかりとzipファイルだった。
Mac:Downloads shibatahiroshitaka$ file ~/Downloads/N02-24_GML.zip
/Users/shibatahiroshitaka/Downloads/N02-24_GML.zip: Zip archive data, at least v2.0 to extract
解凍完了。
Mac:Downloads shibatahiroshitaka$ 7z x ~/Downloads/N02-24_GML.zip -o~/Downloads/station_data
7-Zip [64] 17.05 : Copyright (c) 1999-2021 Igor Pavlov : 2017-08-28
p7zip Version 17.05 (locale=utf8,Utf16=on,HugeFiles=on,64 bits,8 CPUs x64)
Scanning the drive for archives:
1 file, 12717734 bytes (13 MiB)
Extracting archive: /Users/shibatahiroshitaka/Downloads/N02-24_GML.zip
--
Path = /Users/shibatahiroshitaka/Downloads/N02-24_GML.zip
Type = zip
Physical Size = 12717734
Everything is Ok
Folders: 1
Files: 20
Size: 115645995
Compressed: 12717734

【中断】駅名と路線名を抽出

駅データを抽出するサンプルを試す。

.shpから駅名を抽出できるらしい。

geopandasをインストール
pipコマンドが効かなかった。
(.venv) Mac:eki-api shibatahiroshitaka$ pip list
bash: pip: command not found

pipのパスを明示的に指定して確認

(.venv) Mac:eki-api shibatahiroshitaka$ python -m pip list
/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: No module named pip
仮想環境を起動し直してみる。
deactivateができなかった。
(.venv) Mac:eki-api shibatahiroshitaka$ deactivate
bash: deactivate: command not found

仮想環境が正しくアクティブか確認

.venvが二つになった。
(.venv) Mac:eki-api shibatahiroshitaka$ source .venv/bin/activate
(.venv) (.venv) Mac:eki-api shibatahiroshitaka$

pip listに成功
(.venv) (.venv) Mac:eki-api shibatahiroshitaka$ pip list
Package Version
------------------ ---------
certifi 2025.7.14
charset-normalizer 3.4.2
idna 3.10
pip 20.2.3
requests 2.32.4
setuptools 49.2.1
urllib3 2.2.3
WARNING: You are using pip version 20.2.3; however, version 25.0.1 is available.
You should consider upgrading via the '/Users/shibatahiroshitaka/Downloads/python/eki-api/.venv/bin/python3 -m pip install --upgrade pip' command.
再び、geopandasをインストール
(.venv) (.venv) Mac:eki-api shibatahiroshitaka$ pip install geopandas
Collecting geopandas
Downloading geopandas-0.13.2-py3-none-any.whl (1.1 MB)
|████████████████████████████████| 1.1 MB 6.5 MB/s
Collecting fiona>=1.8.19
Downloading fiona-1.10.1.tar.gz (444 kB)
|████████████████████████████████| 444 kB 6.1 MB/s
Installing build dependencies ... done
Getting requirements to build wheel ... error
ERROR: Command errored out with exit status 1:
command: /Users/shibatahiroshitaka/Downloads/python/eki-api/.venv/bin/python3 /Users/shibatahiroshitaka/Downloads/python/eki-api/.venv/lib/python3.8/site-packages/pip/_vendor/pep517/_in_process.py get_requires_for_build_wheel /var/folders/7k/yv7x7vb964g70gd8jrn4l4nw0000gn/T/tmp0ribv538
cwd: /private/var/folders/7k/yv7x7vb964g70gd8jrn4l4nw0000gn/T/pip-install-agkvjx7n/fiona
Complete output (2 lines):
WARNING:root:Failed to get options via gdal-config: [Errno 2] No such file or directory: 'gdal-config'
CRITICAL:root:A GDAL API version must be specified. Provide a path to gdal-config using a GDAL_CONFIG environment variable or use a GDAL_VERSION environment variable.
----------------------------------------
ERROR: Command errored out with exit status 1: /Users/shibatahiroshitaka/Downloads/python/eki-api/.venv/bin/python3 /Users/shibatahiroshitaka/Downloads/python/eki-api/.venv/lib/python3.8/site-packages/pip/_vendor/pep517/_in_process.py get_requires_for_build_wheel /var/folders/7k/yv7x7vb964g70gd8jrn4l4nw0000gn/T/tmp0ribv538 Check the logs for full command output.
WARNING: You are using pip version 20.2.3; however, version 25.0.1 is available.
You should consider upgrading via the '/Users/shibatahiroshitaka/Downloads/python/eki-api/.venv/bin/python3 -m pip install --upgrade pip' command.
エラーが発生している。

GDAL / GEOSという依存ライブラリが足りないことによるエラー

GDALというものをインストールする。

依存ライブラリが多すぎて驚愕した。

先人の努力をコマンドひとつで利用できる時代になったということだろう。
この恩恵に感謝したい。
容量を圧迫している。(ディスクがいっぱいとの通知が入った)
今使っているMacBook Proのバッテリーと容量の課題は、長期的に取り組みたい。
なかなか重い腰が上がらない。
時間がかかりすぎるため、control + Cで中断した。

コメントを残す