PHP simplexml_加载URL中带有特殊字符的文件

PHP simplexml_加载URL中带有特殊字符的文件,php,url,character-encoding,Php,Url,Character Encoding,我正在尝试根据用户的IP检索本地天气预报 我正在使用geoplugin.net获取用户位置,并将城市和国家名称提供给谷歌天气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

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

我正在使用geoplugin.net获取用户位置,并将城市和国家名称提供给谷歌天气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>";
当我使用此版本时,它也可以工作(在浏览器中):

但此版本返回了对博格、西丹马克的预测:

$file = "http://www.google.com/ig/api?weather=" . $geo_city . "," . $geo_country;
$data = file_get_contents($file);
$data = mb_convert_encoding($data, "UTF-8", "ISO-8859-2");

$xml = simplexml_load_string($data);
echo "<pre>"; print_r($xml); echo "</pre>";
当馈送到simplexml_load_file()时,上述任何一项都不会返回所需的结果

如上所述,我怀疑这是一个字符集问题,但我不知道该怎么办

解决这个问题的正确方法是什么


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

听起来确实像是字符集问题。在将结果传递到
simplexml\u load\u file()之前,您是否尝试过将URL转换为另一种编码,例如使用?

尝试以下方法:

$file=”http://www.google.com/ig/api?weather=" . $geo_市。"," . $geo_国家;
$data=文件\获取\内容($file);
$data=mb_转换_编码($data,“UTF-8”,“ISO-8859-2”);
$xml=simplexml\u load\u字符串($data);
回声“;打印(xml);回声“;

它来自这个可能类似的线程:

如果您对
s%26oslash%3Bborg
进行URL解码,您将看到该字符串对应于
sø;borg
在我们解码HTML实体后,它为我们提供了
Søborg
,如下所示:

$city='S%26oslash%3Bborg,丹麦';
echo$city=rawurldecode($city);
//打印Sø;博格,丹麦
echo$city=html_entity_decode($city,0,'UTF-8');
//丹麦索伯格
echo$city=rawurlencode($city);
//打印S%C3%B8borg%2C丹麦
然后:

$xml=file\u get\u contents('http://www.google.com/ig/api?weather="(城市),;
$xml=mb_convert_编码($xml,'UTF-8');
$xml=simplexml\u load\u字符串($xml);
echo$xml->weather->forecast_information->city['data'];
预期产出:

Søborg,丹麦首都区

响应仍然为空,就像使用此url调用服务一样:。因此,如果您只运行此代码
$data=file\u get\u contents(“http://www.google.com/ig/api?weather=Søborg,丹麦)响应也是空的?是的,空的含义与上面相同。我确实得到了一些xml,但其中没有预测数据。我不太确定我转换成什么和从什么。我尝试了iconv(“ISO-8859-1”、“UTF-8”、“$file”)
和其他方法,但都没有成功<代码>utf8\U编码($file)也不会产生任何结果。非常好!你能告诉我正确的方法是什么,把“ø”变成%C3%B8吗?有内置函数吗?确实有,但是我从geoplugin.net xml中获得的字符串(根据
mb\u detect\u encoding()
)似乎是ASCII格式的,所以当我
rawurlencode()
它时,我得到
S%26oslash%3Bborg
返回的字符串。我搜索并了解到ASCII实际上是UTF-8的一个子集。我试过
$string=iconv('ASCII','UTF-8//IGNORE',$geo_city);echo mb_detect_编码($string)如链接中建议的,它仍然返回ASCII。
http://www.google.com/ig/api?weather=S%C3%B8borg,Denmark
http://www.google.com/ig/api?weather=S%26oslash%3Bborg,Denmark
$file = "http://www.google.com/ig/api?weather=" . $geo_city . "," . $geo_country;
$data = file_get_contents($file);
$data = mb_convert_encoding($data, "UTF-8", "ISO-8859-2");

$xml = simplexml_load_string($data);
echo "<pre>"; print_r($xml); echo "</pre>";