PHP simplexml_load_file,URL中带有特殊字符 [英] PHP simplexml_load_file with special chars in URL

查看:215
本文介绍了PHP simplexml_load_file,URL中带有特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据用户的IP检索本地天气预报。

I'm attempting to retrieve a local weather forecast, based on the IP of the user.

我正在使用geoplugin.net来获取用户位置和将城市和国家/地区名称输入到Google Weather API。

I'm using geoplugin.net to get the user location and feed the city and country name to the Google Weather API.

//Get user IP
$ip = $_SERVER['REMOTE_ADDR'];

$geolocation = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$ip));
$geo_city = $geolocation['geoplugin_city'];
$geo_country = $geolocation['geoplugin_countryName'];

$file = "http://www.google.com/ig/api?weather=".$geo_city.",".$geo_country;
$xml = simplexml_load_file($file);

//Echo content of retrieved XML for debugging purposes
echo "<pre>";
print_r($xml);
echo "</pre>";

在大多数情况下效果很好,但是当我使用自己的IP尝试时,我会得到Søborg,丹麦(不是100%准确,但足够接近),这使我从气象API中得到的响应几乎是空的。

It works well for most cases, but when I try it on my own IP, I get Søborg, Denmark (which is not 100% accurate, but close enough) and that gives me an almost empty response from the weather API.

此案的主要嫌疑人是

我想要的XML可以在这里看到: http://www.google.com/ig/api?weather=S%C3%B8borg,丹麦

The XML that I want can be seen here: http://www.google.com/ig/api?weather=S%C3%B8borg,Denmark

要获取的XML可以在这里看到: http://www.google.com/ig/api?weather=S

The XML that I'm getting can be seen here: http://www.google.com/ig/api?weather=S

当我在浏览器中键入此URL时,它可以正常工作:

When I type this URL into the browser it works fine:

http://www.google.com/ig/api?weather=Søborg,Denmark

当我使用此版本时(在浏览器中)它也可以正常工作:

When I use this version it works as well (in the browser):

http://www.google.com/ig/api?weather=S%C3%B8borg,Denmark

但是此版本返回对Borg,Syddanmark的预测:

but this version returns the forecast for Borg,Syddanmark:

http://www.google.com/ig/api?weather=S%26oslash%3Bborg,Denmark

在馈送到simplexml_load_file()时,以上都不返回期望的结果。

None of the above returns the desired result, when fed to the simplexml_load_file().

说,我怀疑这是一个字符集问题,但我不知道该怎么办。

As stated, I suspect that it is a character set issue, but I can't figure out what to do about it.

解决该问题的正确方法是什么?

What is the correct way to solve it?

我知道我可以改用纬度和经度作为Google Weather API的参数,但这只是在解决问题,而不是解决问题。

I know that I can use latitude and longtitude as parameters for Google Weather API instead, but that's just circumventing the problem, not solving it.

推荐答案

如果您对 S%26oslash%3Bborg 进行URL解码,则会看到此字符串对应 S& oslash; borg 这样,在我们像这样解码HTML实体后,我们得到了Søborg

If you URL-decode S%26oslash%3Bborg you'll see that this string corresponds to S&oslash;borg which gives us Søborg after we decode HTML entities like so:

$city = 'S%26oslash%3Bborg,Denmark';
echo $city = rawurldecode($city);
//prints S&oslash;borg,Denmark

echo $city = html_entity_decode($city, 0, 'UTF-8');
//prints Søborg,Denmark

echo $city = rawurlencode($city);
//prints S%C3%B8borg%2CDenmark

然后:

$xml = file_get_contents('http://www.google.com/ig/api?weather='.$city);
$xml = mb_convert_encoding($xml, 'UTF-8');
$xml = simplexml_load_string($xml);
echo $xml->weather->forecast_information->city['data'];

预期输出:

Søborg, Capital Region of Denmark

这篇关于PHP simplexml_load_file,URL中带有特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆