-2

我有这个代码,

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

?> 

上面的代码会给我迪拜市的当地时间和日期。

我需要从文本输入框中获取城市值并用它替换“迪拜”。

这将完成文本框部分。

我试图合并,但得到了一些奇怪的时间。

4

1 回答 1

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>
于 2013-08-24T17:51:33.337 回答