<?php
header('Content-Type: text/xml;charset=UTF-8');
$url = 'https://typhoon.yahoo.co.jp/weather/jp/earthquake/list/';
$count = 10;
do {
$html = file_get_contents($url);
$count--;
} while (empty($html) && $count>0);
$http_header = get_headers($url, 1);
$base_url = 'https://typhoon.yahoo.co.jp';
// ヘッダから時間を取得
$now = new DateTime($http_header['Date']);
$now->setTimezone(new DateTimeZone('Asia/Tokyo'));
$now_rfc2822 = $now->format(DateTimeInterface::RFC2822);
$now_PermaLink = $now->format('A') . ' Asia/Tokyo+9 ' . $now->format('D M y');
// RSS ヘッダ
$RSS = <<< EOD
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0">
<channel>
<title>地震情報 - Yahoo!天気・災害</title>
<link>${url}</link>
<language>ja</language>
<lastBuildDate>${now_rfc2822}</lastBuildDate>
<pubDate>${now_rfc2822}</pubDate>
<guid isPermaLink="false">${now_PermaLink}</guid>
EOD;
# テーブルの抜き出し
if (preg_match('/<table .*class="yjw_table yjSt boderset">[\s\S]*?<\/table>/m', $html, $matches)) {
$earth_quake_table = $matches[0];
} else {
$earth_quake_table = '';
}
// row の抜き出し
if (preg_match_all('/<tr.*>([\s\S]*?)<\/tr>/m', $earth_quake_table, $matches)) {
$trs = $matches[1];
$trmax = count($trs);
for ($tr = 1; $tr < $trmax; $tr++) {
// カラムの抜き出し
if (preg_match_all('/<td.*?>([\s\S]*?)<\/td>/m', $trs[$tr], $matches)) {
$tds = $matches[1];
if(preg_match('/<a href="([^"]*)">(.*)<\/a>/', $tds[0],$matches)){
$link = $base_url . $matches[1];
$datetime = $matches[2];
}
$center = $tds[1];
$magnitude = $tds[2];
$max_level = $tds[3];
// 震度3以上
if($max_level<3){
continue;
}
sscanf($datetime, '%d年%d月%d日%d時%d分ごろ', $y, $m, $d, $h, $i);
$ocdate = new DateTime("$y-$m-$d $h:$i");
$ocdate->setTimezone(new DateTimeZone('Asia/Tokyo'));
$occurrence_date = $ocdate->format(DateTimeInterface::RFC2822);
$RSS .= <<< EOD
<item>
<title>[震源地] ${center} [最大震度] 震度${max_level} M${magnitude}(${datetime}発生) - Yahoo</title>
<link>${link}</link>
<description/>
<pubDate>${occurrence_date}</pubDate>
</item>
EOD;
}
}
$RSS .= <<< EOD
</channel>
</rss>
EOD;
$sl = strlen($RSS);
header('Content-Length: '.$sl);
print $RSS;
}
?>