6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

chatworkで天気予報【chatwork API】

Last updated at Posted at 2015-10-25

#明日晴れるかな
毎晩明日の天気を通知してくれると便利だと思って…

#livedoor天気APIの仕様
http://weather.livedoor.com/weather_hacks/webservice

(例)「福岡県・久留米の天気」を取得する場合
下記URLにアクセスしてJSONデータを取得します。
基本URL + 久留米のID(400040)
http://weather.livedoor.com/forecast/webservice/json/v1?city=400040

#phpでchatworkへ投稿する

chatwork
<?php

  $cityid = 取得したい地区のID;
  $url = 'http://weather.livedoor.com/forecast/webservice/json/v1?city='.$cityid.'';
  $json = file_get_contents($url, true);
  $json = json_decode($json, true);
 
  // Public
  $title = $json['title'];
  $description = $json['description']['text'];
  $publicTime = $json['publicTime'];
  $copyright = $json['copyright']['title'];
  $copyrightLink = 'http://weather.livedoor.com/area/forecast/';
  //$json['copyright']['link']
 
  // Location
  $city = $json['location']['city'];                
  $area = $json['location']['area'];               
  $prefecture = $json['location']['prefecture'];    
  $link = $json['link'];
  $pinpoint = $json['pinpointLocations'];

  
  //forecasts
  foreach ($json['forecasts'] as $entry) {
    $dateLabel = $entry['dateLabel'];                               
    $telop = $entry['telop'];                                       
    $date = $entry['date'];                                         
    $min = $entry['temperature']['min'];                            
    $max = $entry['temperature']['max'];                            
    $mincelsius = $entry['temperature']['min']["celsius"];          
    $minfahrenheit = $entry['temperature']['min']["fahrenheit"];    
    $maxcelsius = $entry['temperature']['max']["celsius"];          
    $maxfahrenheit = $entry['temperature']['max']["fahrenheit"];    
    $image = $entry['image']["url"];                               
 
    // NULL
    if (!isset($min)) { $mincelsius = "--"; }
    if (!isset($max)) { $maxcelsius = "--"; }
    if (!isset($celsius)) { $min = "--"; }
    if (!isset($fahrenheit)) { $min = "--"; }
    
    }
    
    $forecasts = $json['forecasts'];
    
    $body  = "";
    $body .= "[info][title]明日".$forecasts[1]['date']."のお天気[/title]".
             "明日は『".$forecasts[1]['telop']."』\n".
             "最高気温".$forecasts[1]['temperature']['max']["celsius"]."℃\n".
             "最低気温".$forecasts[1]['temperature']['min']["celsius"]."℃\n".
             "©".$copyrightLink;
             "[/info]"."\n".
    $option = array('body' => $body);
    $ch = curl_init();
    /*表示させたいチャットのルームID*/
    $room_id = 000000;
    curl_setopt($ch, CURLOPT_URL, 'https://api.chatwork.com/v1/rooms/'.$room_id.'/messages');
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-ChatWorkToken:チャットワークのAPIキー'));
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($option, '', '&'));
    $response = curl_exec($ch);
    curl_close($ch);

#cronで毎晩定刻にphpを起動
レンタルサーバだったらコンパネから簡単に設定できる(はず)
さくらサーバではできました。

#イメージ
tenki.jpg

#参考サイト
お天気情報 API Weather Hacks を PHP で使ってみた

webrawl様、Livedoor天気部分パクら参考にさせて頂きました。

6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?