Google Weather API - parsing and modifying data

前端 未结 2 959
终归单人心
终归单人心 2021-01-23 02:05

This question is no longer up-to-date -- Google shut down the unofficial weather API in 2012


I\'d like to put some weather forecast to a

2条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-23 02:36

    Encoding problem:

    For some reason Google returns the XML content without proper encoding declaration. One would expect something like:

    
    

    But they skip the encoding attribute in the header. This makes the simplexml_load_file function assume the default encoding of UTF-8. I would consider this a bug in their API implementation, since the XML spec defines UTF-8 as the fallback default encoding.

    To compesate for this, try something like:

    Which seems to work. The ISO-8859-2 value was a pure guess.

    Fahrenheit/Celsius:

    I don't see an easy way to request the temperature data to be provided in Celsius instead of Fahrenheit in this API (I couldn't find the official doc, am I blind?). However conversion from F to C shouldn't be hard at all.

    Try this formula:

    (°F  -  32)  x  5/9 = °C
    

    which you can find in thousand of places. I took it from http://www.manuelsweb.com/temp.htm

提交回复
热议问题