【问题标题】:php simplexml_load_file with htmlspecialchars带有 htmlspecialchars 的 php simplexml_load_file
【发布时间】:2013-03-20 04:52:36
【问题描述】:

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

我不会工作。 有任何想法吗? 提前谢谢你。

【问题讨论】:

    标签: php xml xml-parsing simplexml htmlspecialchars


    【解决方案1】:

    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 已经经过 urldecode。因此,在您的脚本中,您可以直接使用 $_GET 条目中的值。

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

    文档:urlencode

    【讨论】:

    • 对不起。但是,我不明白我需要 urlencode 做什么。当我使用第 4 个“代码容器”中的代码时,它会返回正常链接。问题是 $xml = simplexml_load_file($url);不想阅读该链接
    • 1) 您必须进行 urlencode,因为您的内容中有一些字符应该在 URL 中受到保护(空格、...)。如果我对您的内容进行 urlencode,我将获得:%3CPickUpCityListRQ%3E%3CCredentials%20username%3D'USERNAME'%20password%3D'PASSWORD'%20remoteIp%3D'IP'%2F%3E%3CCountry%3EUK%3C%2FCountry%3E%3C%2FPickUpCityListRQ%3E。 2) 由于您的 URL 参数是内容,而不是文件,因此您必须使用 simple_xml_load_string 而不是 simple_xml_load_file
    • 不,你只需要urlencode你的参数,然后再把它放在URL上。在由 URL 表示的 PHP 脚本上,您只需 simple_xml_load_string($_GET['xml']) 即可加载 XML 内容。不要(不要忘记标记您的最佳答案;)
    • 在这种情况下 in 什么也不返回。即使我尝试从 X.xml 获取内容。 $required = urlencode($required); $url = 'X.xml'; $xml = simplexml_load_string($_GET['$url']);; echo $xml->CountryList->Country[1];什么都不返回
    • 嗯..您的评论似乎令人困惑。如果要直接从文件加载 XML 内容,只需使用 $xml = simple_xml_load_file("your_file.xml")。但是,如果要将 XML 内容发送到将处理此 XML 的特定脚本(到特定 URL),则必须对 XML 内容进行 urlencode(因为它在 URL 上传输),将其作为 URL 的参数。在完成这项工作的脚本上,只需使用 simple_xml_load_string($_GET['your_param_name']); 从您的参数(应该在 $_GET 中)加载 XML
    【解决方案2】:

    答案从PHP simplexml_load_file with special chars in 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];
    

    【讨论】:

      猜你喜欢
      • 2012-02-15
      • 1970-01-01
      • 2018-03-14
      • 1970-01-01
      • 2012-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多