私は自分のウェブサイトの天気フィードに取り組んでいます。
現在、今後 2 日間の予報しか取得できません。今後 5 日間の予報が必要です。
これが私のコードです:
$ipaddress = $_SERVER['REMOTE_ADDR'];
$locationstr = "http://api.locatorhq.com/?user=MYAPIUSER&key=MYAPIKEY&ip=".$ipaddress."&format=xml";
$xml = simplexml_load_file($locationstr);
$city = $xml->city;
switch ($city)
{
case "Pretoria":
$loccode = "SFXX0044";
$weatherfeed = file_get_contents("http://weather.yahooapis.com/forecastrss?p=".$loccode."&u=c");
if (!$weatherfeed) die("weather check failed, check feed URL");
$weather = simplexml_load_string($weatherfeed);
readWeather($loccode);
break;
}
function readWeather($loccode)
{
$doc = new DOMDocument();
$doc->load("http://weather.yahooapis.com/forecastrss?p=".$loccode."&u=c");
$channel = $doc->getElementsByTagName("channel");
$arr;
foreach($channel as $ch)
{
$item = $ch->getElementsByTagName("item");
foreach($item as $rcvd)
{
$desc = $rcvd->getElementsByTagName("description");
$_SESSION["weather"] = $desc->item(0)->nodeValue;
}
}
}
天気を照会する次の行に注目してください。
$doc = new DOMDocument();
$doc->load("http://weather.yahooapis.com/forecastrss?p=".$loccode."&u=c");
// url resolves to http://weather.yahooapis.com/forecastrss?p=SFXX0044&u=c in this case
Googleで検索すると、代わりにこのURLを使用することを提案するこのリンクが見つかりました:
$doc->load("http://xml.weather.yahoo.com/forecastrss/SFXX0044_c.xml");
これも機能し、XML ファイルに 5 日間の予測が表示されますが、サイトにはまだ 2 日間の予測しか表示されません。
channel
これは、RSS フィードにある子要素を利用しているのに対し、XML フィードにはそのような子要素がないためだと思います。
誰かがここで洞察を提供できれば、本当に感謝しています。