我正在使用以下代码获取 XML 的内容:
$xml = simplexml_load_file("X.xml");
echo $xml->CountryList->Country[1];
这是 X.xml:
<PickUpCityListRQ>
<CountryList>
<Country>Albania</Country>
<Country>Andorra</Country>
</CountryList>
</PickUpCityListRQ>
一切正常,它为我返回了 Andorra,但是,当我尝试使用带有特殊字符的 url 时,比如这个:
http://somelink/ServiceRequest.do?xml=<PickUpCityListRQ><Credentials username='USERNAME' password='PASSWORD' remoteIp='IP'/><Country>UK</Country></PickUpCityListRQ>
此链接对您不起作用,因为它只是一个示例,但请相信,真正的链接返回与 X.xml 相同的内容。我知道原因是链接中的特殊字符,但我无法让它工作。我试过这样的事情:
$username = "USERNAME";
$password = "PASSWORD";
$accessurl = htmlspecialchars("Credentials username='$username' password='$password' remoteIp='123.123.123.123'/");
$required = htmlspecialchars("<PickUpCityListRQ><$accessurl><Country>UK</Country></PickUpCityListRQ>");
$url = 'somelink/service/ServiceRequest.do?xml='.$required;
echo $url;
它返回(带有回显)所需的链接,以防我手动(在浏览器中)使用它,我将获得所需的内容。但是,如果我尝试使用此代码获取 XML 内容:
$xml = simplexml_load_file($url);
echo $xml->CountryList->Country[1];
我不会工作。有任何想法吗?先感谢您。