title: Google Weather API - parsing and modifying data
body: <p><strong><em>This question is no longer up-to-date -- Google shut down the unofficial weather API in 2012</em></strong></p>
  
  <hr>
  
  <p>I'd like to put some weather forecast to a friend's web page.
  When I address for</p>
  
  <pre><code>http://www.google.com/ig/api?weather=koprivnica,croatia&amp;hl=hr
  </code></pre>
  
  <p>The browser returns the right content I'd like to parse to PHP with this code:</p>
  
  <pre><code>&lt;?php
  $xml = simplexml_load_file('http://www.google.com/ig/api?weather=koprivnica,croatia&amp;hl=hr');
  $information = $xml-&gt;xpath("/xml_api_reply/weather/forecast_information");
  $current = $xml-&gt;xpath("/xml_api_reply/weather/current_conditions");
  $forecast_list = $xml-&gt;xpath("/xml_api_reply/weather/forecast_conditions");
  ?&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
  &lt;title&gt;VREMENSKA PROGNOZA&lt;/title&gt;
  &lt;/head&gt;
      &lt;body&gt;
          &lt;h1&gt;&lt;?= print $information[0]-&gt;city['data']; ?&gt;&lt;/h1&gt;
          &lt;h2&gt;Danas&lt;/h2&gt;
          &lt;div class="weather"&gt;
              &lt;img src="&lt;?= 'http://www.google.com' . $current[0]-&gt;icon['data']?&gt;" alt="weather"?&gt;
              &lt;span class="condition"&gt;
              &lt;?= $current[0]-&gt;temp_f['data'] ?&gt;&amp;deg; F,
              &lt;?= $current[0]-&gt;condition['data'] ?&gt;
              &lt;/span&gt;
          &lt;/div&gt;
          &lt;h2&gt;Prognoza&lt;/h2&gt;
          &lt;?php foreach ($forecast_list as $forecast) : ?&gt;
          &lt;div class="weather"&gt;
              &lt;img src="&lt;?= 'http://www.google.com' . $forecast-&gt;icon['data']?&gt;" alt="weather"?&gt;
              &lt;div&gt;&lt;?= $forecast-&gt;day_of_week['data']; ?&gt;&lt;/div&gt;
              &lt;span class="condition"&gt;
                  &lt;?= $forecast-&gt;low['data'] ?&gt;&amp;deg; F - &lt;?= $forecast-&gt;high['data'] ?&gt;&amp;deg; F,
                  &lt;?= $forecast-&gt;condition['data'] ?&gt;
              &lt;/span&gt;
          &lt;/div&gt;
          &lt;?php endforeach ?&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>But the code above won't work because I used 'hr' instead of 'en' (hr = Croatian language):</p>
  
  <pre><code>$xml = simplexml_load_file('http://www.google.com/ig/api?weather=koprivnica,croatia&amp;hl=en')
  </code></pre>
  
  <p>is the working syntax but the returned data are in English and the temperature is in Fahrenheit.</p>
  
  <p>I suppose it's matter of a wrong UTF-8 encoding property.</p>
  
  <p>I do not know how to grab the exact Croatian text and convert degrees F into Celsius.</p>
  
  <hr>
  
  <ol>
  <li><p>I found afterwards a link to the <a href="https://stackoverflow.com/questions/3100143/problem-on-google-weather-report-calcuation">F-to-C solution</a> and changed line 19:</p>
  
  <p><code>&lt;?= $current[0]-&gt;temp_f['data'] ?&gt;&amp;deg; F,</code></p>
  
  <p>to</p>
  
  <p><code>&lt;?= $current[0]-&gt;temp_c['data'] ?&gt;&amp;deg; C,</code></p>
  
  <p>(I do not use it in the current version because it seems the API handles Celsius.)</p></li>
  <li><p>To keep the degrees in C while having language set to "en" at the same time you can use <code>en-gb</code>.</p></li>
  </ol>