PHP代码查找通过输入框输入的城市时间


PHP code to find time of a city inputted through a input box

我有这个代码,

 <?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"。

这将完成文本框部分。

我试着融入其中,但得到了一些奇怪的时间。

<?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>