Google Weather API 错误的 XML 响应中的奇怪字符

发布于 2024-12-16 19:47:14 字数 419 浏览 0 评论 0原文

我刚刚启动了一个我一直在开发的小应用程序。没什么大不了的,但我想让它正常工作。它位于 www.wedrapp.com

大多数时候它工作得很好。输入一个城市,XML 将被返回、解析,并将返回的数据显示给用户。

但不幸的是,当搜索某些城市(例如马赛)时,会返回错误。如果你搜索马赛你就会明白我的意思。我有一种感觉,这与特殊字符有关,因为 Marseille 搜索实际上在 XML 中返回了 Marseilles, Provence-Alpes-Côte d'Azur。同样,Paris 会给出错误,因为它实际上返回 Paris, Île-de-France。

任何人都可以阐明如何去除这些奇怪的字符,或者至少阻止它们在击中屏幕之前提供错误吗?它是用 PHP 解析的 XML。

I've just launched a small application i've been working on. Nothing major, but something I would like to get properly working. It's at www.wedrapp.com.

Most of the time it works perfectly fine. Enter a city, XML is returned, parsed and the data returned is shown to the user.

Unfortunately however, an error is returned when certain cities are searched such as Marseille. If you search Marseille you will see what I mean. I have a feeling it is to do with special characters, as Marseille searched actually returns Marseilles, Provence-Alpes-Côte d'Azur in the XML. Similarly Paris gives an error as it actually returns Paris, Île-de-France.

Can anyone shed some light on how to strip these strange characters out, or at least stop them providing an error before hitting the screen? It is XML parsed with PHP.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

单身情人 2024-12-23 19:47:14

找出 google 返回的 XML 的编码。然后将其从该编码重新编码为 UTF-8,然后您可以使用 SimpleXML 加载 XML。

Google Weather API XML 具有基于请求时指定的语言的编码(也可以指定您想要的编码,我很快就会谈到)。

例如,它可以是 ISO-8859-2 作为相关问题 PHP XML — Google Weather API - 解析并修改数据(语言、UTF-8 和 F 到摄氏度) 显示。

您可以通过查看 HTTP 响应标头内容类型来找出是哪一个:

Content-Type: text/xml; charset=ISO-8859-1

您使用了 utf8_encode< super>Docs 来更改编码,它会转换 ISO-8859-1 (也称为Latin-1) 将字符串编码为 UTF-8。看起来对秘密谷歌天气 API 的标准查询默认返回此值。

您可以通过向查询添加 oe 参数来指定您想要的编码。例如,直接将其获取为 UTF-8

http://www.google.com/ig/api?weather=Mountain+View&oe=utf-8
                                                   ^

这样做将确保您始终获得特定的编码,而不需要猜测或解析响应标头。

Find out in which encoding the XML returned by google is. Then re-encode it from that encoding to UTF-8, then you can load the XML with SimpleXML.

The Google Weather API XML has an encoding based on the language that is specified when it's requested (It is possible to specify the encoding you want to have as well, I come to that soon).

For example, it can be ISO-8859-2 as a related question PHP XML — Google Weather API - parsing and modifying data (Language, UTF-8, and F to Celsius) shows.

You can find out which one by looking into the HTTP Response Header Content-Type:

Content-Type: text/xml; charset=ISO-8859-1

You used utf8_encodeDocs to change the encoding, it converts a ISO-8859-1 (also referred to as Latin-1) encoded string to UTF-8. It looks like that standard queries to the secret google weather API return this by default.

You can specify the encoding you'd like to have by adding a oe parameter to the query. For example to get it directly as UTF-8:

http://www.google.com/ig/api?weather=Mountain+View&oe=utf-8
                                                   ^

Doing this will ensure you always get a specific encoding instead that you need to guess or to parse response headers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文