<?php
  $ans = "";
  if (isset($_GET['CityName'])) {
    //exec("curl " . "http://www.webservicex.net/globalweather.asmx/GetWeather?" . $_SERVER['QUERY_STRING'] . " > " . dirname(__FILE__) . "/weather.xml");
    //$ans = file_get_contents(dirname(__FILE__) . "/weather.xml");
    //echo $ans;
    $ans = file_get_contents("http://www.webservicex.net/globalweather.asmx/GetWeather?" . $_SERVER['QUERY_STRING']);
    $ans2 = str_replace('<' . '/string>','',str_replace('<string xmlns="http://www.webserviceX.NET"><' . '?xml version="1.0" encoding="utf-16"?>','',str_replace("&lt;","<",str_replace("&gt;",">",$ans))));
    $ans = str_replace('<' . '?xml version="1.0" encoding="utf-8"?>','',str_replace('<' . 'Status>Success</Status>','',$ans2));
    $ans2 = str_replace('<' . 'CurrentWeather>','',str_replace('</' . 'CurrentWeather>','',$ans));
    $ans = str_replace('<' . 'CurrentWeather>','',str_replace('</' . 'CurrentWeather>','',$ans2));
    $ans2 = str_replace('</Time>','',str_replace('</Wind>','',str_replace('</SkyConditions>','',str_replace('</Visibility>','',str_replace('</DewPoint>','',str_replace('</RelativeHumidity>','',str_replace('</Temperature>','',str_replace('</Pressure>','',str_replace('</Location>','',$ans)))))))));
        for ($jj=0; $jj<15; $jj++) {
          $ans = str_replace(">",": ",str_replace("<","",$ans2));
          $ans2 = $ans;
        }
    echo $ans;
    exit(0);
  }
?>
<html>
<head>
<?php
echo '<title>Get the Weather via Global Weather ... http://www.webservicex.net/ ... thanks</title>';
?>

<script type="text/javascript">

window.onload = initAll;
var myxhr = false, breal=0;
var cityName, countryName, urlis;


function createXMLData(incity, incountry) {
 var outxml = '<' + '?' + 'xml version="1.0" encoding="utf-8"' + '?' + '>';
 outxml = outxml + '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">';
 outxml = outxml + '  <soap:Body>';
 outxml = outxml + '    <GetWeather xmlns="http://www.webserviceX.NET">';
 outxml = outxml + '      <CityName>' + incity + '</CityName>';
 outxml = outxml + '      <CountryName>' + incountry + '</CountryName>';
 outxml = outxml + '    </GetWeather>';
 outxml = outxml + '  </soap:Body>';
 outxml = outxml + '</soap:Envelope>';
 return outxml;
}

function initAll() {
    
  if (window.XMLHttpRequest) {
        myxhr = new window.XMLHttpRequest;
    }
    else {
  try {
    myxhr = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (othermicrosoft) {
    try {
      myxhr = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (failed) {
      myxhr = false;
    }
  }
    }

  if (myxhr) {
        //console.log("prehere");
    cityName = document.getElementById("CityName").value;
    countryName = document.getElementById("CountryName").value;
    var xml = createXMLData(cityName, countryName);
    
    myxhr.onreadystatechange = readGlobalWeatherXml;
    //myxhr.open("GET", "http://www.webservicex.net/globalweather.asmx/GetWeather?CityName=" + cityName + "&CountryName=" + countryName, true);
    myxhr.open("GET", "./get_weather.php?CityName=" + cityName + "&CountryName=" + countryName, true);
   //myxhr.open("POST","http://www.webservicex.net/globalweather.asmx/GetWeather",true);
if (1 == 2) {
	// Set request headers for message to send. Depends on the web service
	myxhr.setRequestHeader('Man', 'POST /globalweather.asmx HTTP/1.1');
	myxhr.setRequestHeader('Content-Type', 'text/xml');
	myxhr.setRequestHeader('Content-Length', xml.length);
	myxhr.setRequestHeader('SOAPAction', 'http://www.webserviceX.NET/GetCitiesByCountry');
    alert(xml);
    
    myxhr.send(xml);
}

    myxhr.send(null);
  
  } else {
    alert("Not able to create XMLHttpRequest ... sadly.");
  }
}

function readGlobalWeatherXml() {
  var wstuff;
  if (myxhr.readyState == 4 && breal == 1) {
    console.log("myxhr.status=" + myxhr.status);
    if (myxhr.status == 200) {
      console.log("here");
      if (myxhr.responseXML) {
        console.log("here1");
        var myAllLinks = myxhr.responseXML.getElementsByTagName("Location");
        
        for (var myi=0; myi<myAllLinks.length; myi++) {
          document.getElementById("Wind").innerHTML = "Wind: " +  myAllLinks[myi].getElementsByTagName("Wind")[0].firstChild;
          document.getElementById("Time").innerHTML = "Time: " +  myAllLinks[myi].getElementsByTagName("Time")[0].firstChild;
          document.getElementById("Visibility").innerHTML = "Visibility: " +  myAllLinks[myi].getElementsByTagName("Visibility")[0].firstChild;
          document.getElementById("SkyConditions").innerHTML = "Sky Conditions: " +  myAllLinks[myi].getElementsByTagName("SkyConditions")[0].firstChild;
          document.getElementById("Temperature").innerHTML = "Temperature: " +  myAllLinks[myi].getElementsByTagName("Temperature")[0].firstChild;
          document.getElementById("DewPoint").innerHTML = "Dew Point: " +  myAllLinks[myi].getElementsByTagName("DewPoint")[0].firstChild;
          document.getElementById("RelativeHumidity").innerHTML = "Relative Humidity: " +  myAllLinks[myi].getElementsByTagName("RelativeHumidity")[0].firstChild;
          document.getElementById("Pressure").innerHTML = "Pressure: " +  myAllLinks[myi].getElementsByTagName("Pressure")[0].firstChild;
  
        }
        
      } else if (myxhr.responseText) {
        console.log("here5=" + myxhr.responseText);
        var ws2, wstuff = myxhr.responseText.replace('</Time>','').replace('</Wind>','').replace('</SkyConditions>','').replace('</Visibility>','').replace('</DewPoint>','').replace('</RelativeHumidity>','').replace('</Temperature>','').replace('</Pressure>','').replace('</Location>','');
        document.getElementById("weather").style.display = 'block';
        for (var jj=0; jj<15; jj++) {
          ws2 = wstuff.replace(">",": ").replace("<","");
          wstuff = ws2;
        }
        document.getElementById("weather").innerHTML = wstuff;
      }      
    } else {
      console.log("Problem: " + myxhr.status);
      //alert("Problem: " + myxhr.status);
    }
  }
}



</script>

</head>
<body>
<h2>Hover (or Click) for weather from GlobalWeather</h2>
                      <form target="_blank" action='http://www.webservicex.net/globalweather.asmx/GetWeather' method="POST">                      
                        
                          <table cellspacing="0" cellpadding="4" frame="box" bordercolor="#dcdcdc" rules="none" style="border-collapse: collapse;">
                          <tr>
	<td class="frmHeader" background="#dcdcdc" style="border-right: 2px solid white;">Parameter</td>
	<td class="frmHeader" background="#dcdcdc">Value</td>
</tr>

                        
                          <tr>
                            <td class="frmText" style="color: #000000; font-weight: normal;">CityName:</td>
                            <td><input class="frmInput" type="text" size="50" id="CityName" name="CityName"></td>
                          </tr>
                        
                          <tr>
                            <td class="frmText" style="color: #000000; font-weight: normal;">CountryName:</td>
                            <td><input class="frmInput" type="text" size="50" id="CountryName" name="CountryName"></td>
                          </tr>
                        
                        <tr>
                          <td></td>
                          <td align="right"> <input onclick=' cityName = document.getElementById("CityName").value; countryName = document.getElementById("CountryName").value; urlis = "http://www.webservicex.net/globalweather.asmx/GetWeather?CityName=" + cityName + "&CountryName=" + countryName; window.open(urlis,"_blank"); ' onmouseover=" breal=1; initAll();" type="submit" value="Hover or Click (for new window)" id="submit" class="button"></td>
                        </tr>
                        </table>
                        
                        <p id="Time"></p>
                        <p id="Wind"></p>
                        <p id="Visibility"></p>
                        <p id="SkyConditions"></p>
                        <p id="Temperature"></p>
                        <p id="DewPoint"></p>
                        <p id="Pressure"></p>
                        

                    </form>
<?php
  echo '<textarea id="weather" style="display: none;"  rows="14" cols="80"> ' . $ans . '</textarea>';
?>
</body>
</html>
