Google Weather API 错误的 XML 响应中的奇怪字符
我刚刚启动了一个我一直在开发的小应用程序。没什么大不了的,但我想让它正常工作。它位于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
找出 google 返回的 XML 的编码。然后将其从该编码重新编码为 UTF-8,然后您可以使用
SimpleXML
加载 XML。Google Weather API XML 具有基于请求时指定的语言的编码(也可以指定您想要的编码,我很快就会谈到)。
例如,它可以是
ISO-8859-2
作为相关问题 PHP XML — Google Weather API - 解析并修改数据(语言、UTF-8 和 F 到摄氏度) 显示。您可以通过查看 HTTP 响应标头内容类型来找出是哪一个:
您使用了
utf8_encode
< super>Docs 来更改编码,它会转换ISO-8859-1
(也称为Latin-1
) 将字符串编码为UTF-8
。看起来对秘密谷歌天气 API 的标准查询默认返回此值。您可以通过向查询添加
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:
You used
utf8_encode
Docs to change the encoding, it converts aISO-8859-1
(also referred to asLatin-1
) encoded string toUTF-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 asUTF-8
:Doing this will ensure you always get a specific encoding instead that you need to guess or to parse response headers.