0

我正在使用以下代码获取 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];

我不会工作。有任何想法吗?先感谢您。

4

2 回答 2

2

htmlspecialchars用于保护 HTML 内容页面中的特殊字符(尤其是在用户输入时,以避免某种 XSS 或其他攻击..)。

当您操作 URL 时,您应该使用urlencode将您的内容作为 URL 的参数发送。

所以你的网址将是:

http://someserver/somethink/services/ServiceRequest.do?xml=%3CPickUpCityListRQ%3E%3CCredentials%20username%3D'USERNAME'%20password%3D'PASS‌​WORD'%20remoteIp%3D'IP'%2F%3E%3CCountry%3EUK%3C%2FCountry%3E%3C%2FPickUpCityListR‌​Q%3E

正如文档所说,urldecode不需要,因为超全局变量 $_GET 和 $_REQUEST 已经被 urldecoded。因此,在执行该工作的脚本中,您可以直接使用 $_GET 条目中的值。

$xml = simplexml_load_string($_GET['xml']);

文档:urlencode

于 2013-03-20T05:00:13.500 回答
0

从PHP simplexml_load_file窃取的答案在 URL 中有特殊字符

用这个

$username = "USERNAME";
$password = "PASSWORD";
$accessurl = "Credentials username='$username' password='$password' remoteIp='123.123.123.123'/";
$required = "<PickUpCityListRQ><$accessurl><Country>UK</Country></PickUpCityListRQ>";

$url= rawurlencode("somelink/service/ServiceRequest.do?xml={$required}");

$xml = simplexml_load_file($url);
echo $xml->CountryList->Country[1];
于 2013-03-20T05:03:03.960 回答