前回ZABBIXで見よう見まねでグラフを作ってみましたがPHPファイルの中身をあまり理解してなかったので勉強しました。OpenWeatherMapから気温をとってくるPHPファイル。
[root@wiki-web-2 externalscripts]# cat test_weather_001.php #!/usr/bin/php <?php // API Key $apiKey='c0fb77bc40a18b9a24019dcb41ee57d3'; // OpenWeatherMap API のURL $apiUrl = 'http://api.openweathermap.org/data/2.5/weather?units=metric&APPID=' . $apiKey . '&q=' ; // 第1引数で天気を取得する都市を指定 // 都市はZabbixのアイテムキーで指定します $city = $argv[1]; // 天気データの取得(Json形式) $weatherDatas_json = @file_get_contents($apiUrl . $city); // Json形式のデータを配列変数に変換 $weatherDatas = json_decode($weatherDatas_json, true); // ここは実際仕込む時に消す。テスト時だけ表示 var_dump( $weatherDatas ); echo "------------------------------------\n\n"; // 温度を取得 $weatherTemp = $weatherDatas['main']['temp']; echo $weatherTemp; [root@wiki-web-2 externalscripts]#
★で解説を書いてみる。
#!/usr/bin/php <?php // API Key $apiKey='c0fb77bc40a18b9a24019dcb41ee57d3'; // OpenWeatherMap API のURLを組み立てる $apiUrl = 'http://api.openweathermap.org/data/2.5/weather?units=metric&APPID=' . $apiKey . '&q=' ; ★' ' シングルクォートは文字列扱い。 ★'. ドットは変数内で文字をつなげる。スペースは見た目だけ。 ★「 http://api.openweathermap.org/data/2.5/weather?units=metric&APPID=c0fb77bc40a18b9a24019dcb41ee57d3&q= 」 というURLが生成される // 第1引数で天気を取得する都市を指定 // 都市はZabbixのアイテムキーで指定します $city = $argv[1]; ★ $argv[1] はphp実行時の引数 ★ php getWeather_temp.php tokyo とかやると$cityにtokyoが入る ★ ちなみに php getWeather_temp.php [tokyo] でも同じ。 // 天気データの取得(Json形式) $weatherDatas_json = @file_get_contents($apiUrl . $city); ★「 http://api.openweathermap.org/data/2.5/weather?units=metric&APPID=c0fb77bc40a18b9a24019dcb41ee57d3&q=tokyo 」 というURLが生成される ★ @file_get_contents はwget みたいなコマンド ★ $weatherDatas_json へ入れる ★この時点で取ってきたデータはJSON形式でこんな感じ。 {"coord":{"lon":139.69,"lat":35.69},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}],"base":"stations","main":{"temp":21,"pressure":1019,"humidity":52,"temp_min":18.89,"temp_max":22.22},"visibility":10000,"wind":{"speed":8.2,"deg":60},"clouds":{"all":0},"dt":1463649156,"sys":{"type":1,"id":7619,"message":0.0194,"country":"JP","sunrise":1463599984,"sunset":1463650971},"id":1850147,"name":"Tokyo","cod":200} // Json形式のデータを配列変数に変換 $weatherDatas = json_decode($weatherDatas_json, true); ★ JSONで取ってきたデータを配列変数に並び替え。 ★ $weatherDatasにまるっと入る ★この時点で取ってきたデータはこんな感じ。 array(12) { ["coord"]=> array(2) { ["lon"]=> float(139.69) ["lat"]=> float(35.69) } ["weather"]=> array(1) { [0]=> array(4) { ["id"]=> int(800) ["main"]=> string(5) "Clear" ["description"]=> string(9) "clear sky" ["icon"]=> string(3) "01d" } } ["base"]=> string(8) "stations" ["main"]=> array(5) { ["temp"]=> int(21) ["pressure"]=> int(1019) ["humidity"]=> int(52) ["temp_min"]=> float(18.89) ["temp_max"]=> float(22.22) } ["visibility"]=> int(10000) ["wind"]=> array(2) { ["speed"]=> float(8.2) ["deg"]=> int(60) } ["clouds"]=> array(1) { ["all"]=> int(0) } ["dt"]=> int(1463649156) ["sys"]=> array(6) { ["type"]=> int(1) ["id"]=> int(7619) ["message"]=> float(0.0194) ["country"]=> string(2) "JP" ["sunrise"]=> int(1463599984) ["sunset"]=> int(1463650971) } ["id"]=> int(1850147) ["name"]=> string(5) "Tokyo" ["cod"]=> int(200) } var_dump( $weatherDatas ); echo '------------------------------------'; 配列関数はecho だと array だよ、って表示されるだけなので、どんな中身かはvar_dumpで分かる。 // 気温を取得 $weatherTemp = $weatherDatas['main']['temp']; ★配列変数のどの値をとってくるかの指定は簡単だ // 気温を表示 echo $weatherTemp; ★ZABBIXで取得する値は最後にechoで返する
なるほどなるほど。
2 thoughts on “PHPでも勉強するか! その3 お天気スクリプトを読み解く”