且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP simplexml_load_file,URL中带有特殊字符

更新时间:2022-04-28 04:54:42

如果您对 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øborg which gives us Søborg after we decode HTML entities like so:

$city = 'S%26oslash%3Bborg,Denmark';
echo $city = rawurldecode($city);
//prints Sø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