2011-12-09 9 views
3

で入力を取ることはこれがAPIである:=>リンク

http://www.google.com/ig/api?weather=[city name]

私は手動でその後、それがために、ユーザの入力を取得する方法をperfectly.Isを作品だhttp://www.google.com/ig/api?weather=dhakaよう[都市名]を置くとき市の名前。

<? 
$xml = simplexml_load_file('http://www.google.com/ig/api?weather=dhaka'); //Manually,I put 'dhaka' here 
$information = $xml->xpath("/xml_api_reply/weather/forecast_information"); 
$current = $xml->xpath("/xml_api_reply/weather/current_conditions"); 
$forecast_list = $xml->xpath("/xml_api_reply/weather/forecast_conditions"); 
?> 
<html> 
    <head> 
     <title>Google Weather API</title> 
    </head> 
    <body> 
     <h1><?= print $information[0]->city['data']; ?></h1> 
     <h2>Today's weather</h2> 
     <div class="weather">  
      <img src="<?= 'http://www.google.com' . $current[0]->icon['data']?>" alt="weather"?> 
      <span class="condition"> 
      <?= $current[0]->temp_f['data'] ?>&deg; F, 
      <?= $current[0]->condition['data'] ?> 
      </span> 
     </div> 
     <h2>Forecast</h2> 
     <? foreach ($forecast_list as $forecast) : ?> 
     <div class="weather"> 
      <img src="<?= 'http://www.google.com' . $forecast->icon['data']?>" alt="weather"?> 
      <div><?= $forecast->day_of_week['data']; ?></div> 
      <span class="condition"> 
       <?= $forecast->low['data'] ?>&deg; F - <?= $forecast->high['data'] ?>&deg; F, 
       <?= $forecast->condition['data'] ?> 
      </span> 
     </div> 
     <? endforeach ?> 
    </body> 
</html> 

ありがとうございます。

答えて

2

フォーム

<form method="post"> 
City <input type="text" name="cityName"> 
<br> 
<input type="submit"> 
</form> 

を作成し、$xml = simplexml_load_file('http://www.google.com/ig/api?weather=dhaka');$xml = simplexml_load_file('http://www.google.com/ig/api?weather='.$_POST['cityName']);

+0

Thanks.Itの作業:-)を変更することができます – Adnan

2

htmlフォームを使用し、$ _GET/$ _POSTから入力を取得し、それをapi urlにrawurlencode()します。