我有这段代码,
<?php
// XML File
$feed_url = "http://api.worldweatheronline.com/free/v1/weather.ashx?q=dubai&format=xml&extra=localObsTime&num_of_days=5&key=42esgfa4pfqp6zwgr4egjbph";
// INITIATE CURL.
$curl = curl_init();
// CURL SETTINGS.
curl_setopt($curl, CURLOPT_URL,"$feed_url");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 0);
// GRAB THE XML FILE.
$xxml = curl_exec($curl);
curl_close($curl);
$xml = new SimpleXMLElement($xxml);
$flag=0;
// Loop through ... only pick the first one
foreach ($xml->current_condition as $item) {
if($flag==0){
echo"
Dubai Time And Date: {$item->localObsDateTime}
<br />
";
}
$flag=1;
}
?>
上述代码将为我提供迪拜市的当地时间和日期。
我需要从文本输入框中取出城市值,并将“dubai”替换为该值。
这将执行文本框部分。
我试图融入,但得到一些奇怪的时间。
答案 0 :(得分:0)
<?php
if(isSet($_POST['city']) && $_POST['city'])
{
// XML File
$feed_url = "http://api.worldweatheronline.com/free/v1/weather.ashx?q=" . urlencode(trim($_POST['city'])) . "&format=xml&extra=localObsTime&num_of_days=5&key=xxxxxxxxx";
// INITIATE CURL.
$curl = curl_init();
// CURL SETTINGS.
curl_setopt($curl, CURLOPT_URL,"$feed_url");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 0);
// GRAB THE XML FILE.
$xxml = curl_exec($curl);
curl_close($curl);
$xml = new SimpleXMLElement($xxml);
$flag=0;
// Loop through ... only pick the first one
foreach ($xml->current_condition as $item) {
if($flag==0){
echo
ucfirst($_POST['city']) . " Time And Date: {$item->localObsDateTime}
<br />
";
}
$flag=1;
}
}
?>
<form method="post" action="">
<input type="text" name="city" value="<?php echo (isSet($_POST['city']) && $_POST['city'] ? $_POST['city'] : ''); ?>">
<input type="submit" value="Search City">
</form>