SQLの窓 イラストAC フリー素材

2015年05月24日

Livedoor の お天気Webサービス API は、JSON のテストするのに重宝しています。

制限も特に無く、GET メソッドで呼び出せる今時珍しい公開 API です。

お天気Webサービス仕様 - Weather Hacks - livedoor 天気情報

ただ、JSON を返すまっとうな API なので、結果の表示はとてもテストには不向きです。


 

そこで、簡単な PHPコードで JSON を整形して日本語を表示する自分用 API を作成します
<?php
header( "Content-Type: application/json; Charset=utf-8" );
header( "pragma: no-cache" );
header( "Expires: Wed, 31 May 2000 14:59:58 GMT" );
header( "Cache-control: no-cache" );
header( "Access-Control-Allow-Origin: *" );

if ( $_GET['city'] == '' ) {
	$_GET['city'] = "270000";
}

$work = file_get_contents( "http://weather.livedoor.com/forecast/webservice/json/v1?city={$_GET['city']}" );
$obj = json_decode( $work );
$work = json_encode( $obj, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT );

print $work;
?>

『Access-Control-Allow-Origin: *』があるので、JavaScript からも呼べます。

テストしたい放題です。
{
	"pinpointLocations": [
		{
			"link": "http:\/\/weather.livedoor.com\/area\/forecast\/2710000",
			"name": "大阪市"
		},
		{
			"link": "http:\/\/weather.livedoor.com\/area\/forecast\/2714000",
			"name": "堺市"
		},
		{
			"link": "http:\/\/weather.livedoor.com\/area\/forecast\/2720200",
			"name": "岸和田市"
		},
		{
			"link": "http:\/\/weather.livedoor.com\/area\/forecast\/2720300",
			"name": "豊中市"
		},
		{
			"link": "http:\/\/weather.livedoor.com\/area\/forecast\/2720400",
			"name": "池田市"
		},
		{
			"link": "http:\/\/weather.livedoor.com\/area\/forecast\/2720500",
			"name": "吹田市"
		},
		{
			"link": "http:\/\/weather.livedoor.com\/area\/forecast\/2720600",
			"name": "泉大津市"
		},
		{
			"link": "http:\/\/weather.livedoor.com\/area\/forecast\/2720700",
			"name": "高槻市"
		},
		{
			"link": "http:\/\/weather.livedoor.com\/area\/forecast\/2720800",
			"name": "貝塚市"
		},
		{
			"link": "http:\/\/weather.livedoor.com\/area\/forecast\/2720900",
			"name": "守口市"
		},
		{
			"link": "http:\/\/weather.livedoor.com\/area\/forecast\/2721000",
			"name": "枚方市"
		},
		{
			"link": "http:\/\/weather.livedoor.com\/area\/forecast\/2721100",
			"name": "茨木市"
		},
		{
			"link": "http:\/\/weather.livedoor.com\/area\/forecast\/2721200",
			"name": "八尾市"
		},
		{
			"link": "http:\/\/weather.livedoor.com\/area\/forecast\/2721300",
			"name": "泉佐野市"
		},
		{
			"link": "http:\/\/weather.livedoor.com\/area\/forecast\/2721400",
			"name": "富田林市"
		},
		{
			"link": "http:\/\/weather.livedoor.com\/area\/forecast\/2721500",
			"name": "寝屋川市"
		},
		{
			"link": "http:\/\/weather.livedoor.com\/area\/forecast\/2721600",
			"name": "河内長野市"
		},
		{
			"link": "http:\/\/weather.livedoor.com\/area\/forecast\/2721700",
			"name": "松原市"
		},
		{
			"link": "http:\/\/weather.livedoor.com\/area\/forecast\/2721800",
			"name": "大東市"
		},
		{
			"link": "http:\/\/weather.livedoor.com\/area\/forecast\/2721900",
			"name": "和泉市"
		},
		{
			"link": "http:\/\/weather.livedoor.com\/area\/forecast\/2722000",
			"name": "箕面市"
		},
		{
			"link": "http:\/\/weather.livedoor.com\/area\/forecast\/2722100",
			"name": "柏原市"
		},
		{
			"link": "http:\/\/weather.livedoor.com\/area\/forecast\/2722200",
			"name": "羽曳野市"
		},
		{
			"link": "http:\/\/weather.livedoor.com\/area\/forecast\/2722300",
			"name": "門真市"
		},
		{
			"link": "http:\/\/weather.livedoor.com\/area\/forecast\/2722400",
			"name": "摂津市"
		},
		{
			"link": "http:\/\/weather.livedoor.com\/area\/forecast\/2722500",
			"name": "高石市"
		},
		{
			"link": "http:\/\/weather.livedoor.com\/area\/forecast\/2722600",
			"name": "藤井寺市"
		},
		{
			"link": "http:\/\/weather.livedoor.com\/area\/forecast\/2722700",
			"name": "東大阪市"
		},
		{
			"link": "http:\/\/weather.livedoor.com\/area\/forecast\/2722800",
			"name": "泉南市"
		},
		{
			"link": "http:\/\/weather.livedoor.com\/area\/forecast\/2722900",
			"name": "四條畷市"
		},
		{
			"link": "http:\/\/weather.livedoor.com\/area\/forecast\/2723000",
			"name": "交野市"
		},
		{
			"link": "http:\/\/weather.livedoor.com\/area\/forecast\/2723100",
			"name": "大阪狭山市"
		},
		{
			"link": "http:\/\/weather.livedoor.com\/area\/forecast\/2723200",
			"name": "阪南市"
		},
		{
			"link": "http:\/\/weather.livedoor.com\/area\/forecast\/2730100",
			"name": "島本町"
		},
		{
			"link": "http:\/\/weather.livedoor.com\/area\/forecast\/2732100",
			"name": "豊能町"
		},
		{
			"link": "http:\/\/weather.livedoor.com\/area\/forecast\/2732200",
			"name": "能勢町"
		},
		{
			"link": "http:\/\/weather.livedoor.com\/area\/forecast\/2734100",
			"name": "忠岡町"
		},
		{
			"link": "http:\/\/weather.livedoor.com\/area\/forecast\/2736100",
			"name": "熊取町"
		},
		{
			"link": "http:\/\/weather.livedoor.com\/area\/forecast\/2736200",
			"name": "田尻町"
		},
		{
			"link": "http:\/\/weather.livedoor.com\/area\/forecast\/2736600",
			"name": "岬町"
		},
		{
			"link": "http:\/\/weather.livedoor.com\/area\/forecast\/2738100",
			"name": "太子町"
		},
		{
			"link": "http:\/\/weather.livedoor.com\/area\/forecast\/2738200",
			"name": "河南町"
		},
		{
			"link": "http:\/\/weather.livedoor.com\/area\/forecast\/2738300",
			"name": "千早赤阪村"
		}
	],
	"link": "http:\/\/weather.livedoor.com\/area\/forecast\/270000",
	"forecasts": [
		{
			"dateLabel": "今日",
			"telop": "晴れ",
			"date": "2015-05-24",
			"temperature": {
				"min": null,
				"max": null
			},
			"image": {
				"width": 50,
				"url": "http:\/\/weather.livedoor.com\/img\/icon\/1.gif",
				"title": "晴れ",
				"height": 31
			}
		},
		{
			"dateLabel": "明日",
			"telop": "晴のち曇",
			"date": "2015-05-25",
			"temperature": {
				"min": {
					"celsius": "17",
					"fahrenheit": "62.6"
				},
				"max": {
					"celsius": "30",
					"fahrenheit": "86.0"
				}
			},
			"image": {
				"width": 50,
				"url": "http:\/\/weather.livedoor.com\/img\/icon\/5.gif",
				"title": "晴のち曇",
				"height": 31
			}
		},
		{
			"dateLabel": "明後日",
			"telop": "晴時々曇",
			"date": "2015-05-26",
			"temperature": {
				"min": null,
				"max": null
			},
			"image": {
				"width": 50,
				"url": "http:\/\/weather.livedoor.com\/img\/icon\/2.gif",
				"title": "晴時々曇",
				"height": 31
			}
		}
	],
	"location": {
		"city": "大阪",
		"area": "近畿",
		"prefecture": "大阪府"
	},
	"publicTime": "2015-05-24T17:00:00+0900",
	"copyright": {
		"provider": [
			{
				"link": "http:\/\/tenki.jp\/",
				"name": "日本気象協会"
			}
		],
		"link": "http:\/\/weather.livedoor.com\/",
		"title": "(C) LINE Corporation",
		"image": {
			"width": 118,
			"link": "http:\/\/weather.livedoor.com\/",
			"url": "http:\/\/weather.livedoor.com\/img\/cmn\/livedoor.gif",
			"title": "livedoor 天気情報",
			"height": 26
		}
	},
	"title": "大阪府 大阪 の天気",
	"description": {
		"text": " 近畿地方は、東シナ海に中心を持つ高気圧に覆われて北部や中部では概ね\n晴れていますが、南部では南海上に停滞する前線の影響で雲の広がっている\nところがあります。\n\n 今夜の近畿地方は、高気圧に覆われて概ね晴れるでしょう。\n\n 明日の近畿地方は、高気圧に覆われて昼頃までは晴れますが、その後は気\n圧の谷や上空の寒気の影響で雲が広がりやすく、雨の降るところがある見込\nみです。",
		"publicTime": "2015-05-24T16:33:00+0900"
	}
}



posted by at 2015-05-24 17:13 | PHP+通信 | このブログの読者になる | 更新情報をチェックする