前に書いた(jQueryの$.ajaxでPHPプログラムを経由してJSONデータを表示するプログラム)をもとにlivedoor天気APIからデータを取得して地域名をアラートで表示するプログラムをついでに作成。
IdWeather.php
天気情報を取得して出力するPHPプログラム。
$city = 130010; $base_url = "http://weather.livedoor.com/forecast/webservice/json/v1?city=$city"; $json = file_get_contents($base_url); $json = mb_convert_encoding($json, 'UTF-8'); print $json;
ajax.html
IdWeather.phpを読み込んでJSONデータを読み込みます。正常に通信ができると地域名をあらーどで表示します。
$(function() {
$.ajax({
url:'IdWeather.php',
dataType: 'json',
})
.done(function(data) {
alert(data.title);
})
.fail(function(data) {
alert('error');
});
});
スポンサーリンク

コメント