open weather map api

まず、city listをダウンロードします。
http://bulk.openweathermap.org/sample/

コマンドラインで、解凍します。
$ gzip -d city.list.json.gz

すると、city.list.jsonができます。

[vagrant@localhost weather]$ ls
city.list.json  index.php

jpanの国コードはjp
データを見てみましょう。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
  "id": 1850144,
  "name": "Tōkyō-to",
  "country": "JP",
  "coord": {
    "lon": 139.691711,
    "lat": 35.689499
  }
},
{
  "id": 1848354,
  "name": "Yokohama-shi",
  "country": "JP",
  "coord": {
    "lon": 139.642502,
    "lat": 35.447781
  }
},

では、今回は丸の内(“Marunouchi”)でデータを取得してみます。
API Keyはサイトで30秒くらいで取得できます。
https://home.openweathermap.org/api_keys

1
2
3
4
5
6
7
8
9
10
11
12
<?php
 
$city_code = "Marunouchi";
$API_KEY = "hoge";
 
$forecast = json_decode(file_get_contents($BASE_URL), true);
 
echo "<pre>";
var_dump($forecast);
echo "</pre>";
?>

weather rain など返ってきてますね。