我遇到 simplexml_load_string 函数的问题,该函数之前可以工作,但今天停止工作。
这是输入 xml 的示例:-
$response = '<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.beautyfort.com/api/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><SOAP-ENV:Body><ns1:ProductSearchResponse><ns1:TestMode>false</ns1:TestMode><ns1:Page>1</ns1:Page><ns1:ResultsPerPage>1</ns1:ResultsPerPage><ns1:TotalResults>1276</ns1:TotalResults><ns1:Items><ns1:Item><ns1:StockCode>L3720</ns1:StockCode><ns1:Name>David Beckham Instinct Gift Set 30ml EDT + 150ml Shower Gel</ns1:Name><ns1:QuantityAvailable>1</ns1:QuantityAvailable><ns1:UnitPrice Currency="GBP"><ns1:Amount>8.72</ns1:Amount></ns1:UnitPrice><ns1:YourRating xsi:nil="true"/><ns1:YourStockCode></ns1:YourStockCode><ns1:ImageLastUpdated>2016-12-22 18:13:08</ns1:ImageLastUpdated><ns1:ThumbnailImageUrl>https://www.beautyfort.com/pic/Y0NqeTBJbmdvaUx6ZUFOa0MyTlNObmhGckltYnVQQmg%3D</ns1:ThumbnailImageUrl><ns1:HighResImageUrl xsi:nil="true"/></ns1:Item></ns1:Items></ns1:ProductSearchResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>';
$xml = simplexml_load_string(($response));
//This returning following response:-
SimpleXMLElement Object (
)
Run Code Online (Sandbox Code Playgroud)
有人可以看看这个并让我知道这里出了什么问题吗?
为了回答你的问题,我们首先来看看这个问题。
假设您通过以下方式显示变量的内容$xml:
php > print_r($xml);
Run Code Online (Sandbox Code Playgroud)
在这里,您列出当前节点(根节点)的子节点,其默认命名空间(无前缀)未在该节点中声明:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.beautyfort.com/api/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
Run Code Online (Sandbox Code Playgroud)
如果您要列出该节点的名称空间:
php > print_r($xml->getNamespaces());
Array
(
[SOAP-ENV] => http://schemas.xmlsoap.org/soap/envelope/
)
Run Code Online (Sandbox Code Playgroud)
只有一个可见,因为这是该(根)节点中实际使用的唯一一个(不包括其子节点)。
要列出根及其所有子级的所有使用的命名空间,请添加“true”作为第一个参数:
php > print_r($xml->getNamespaces(true));
Array
(
[SOAP-ENV] => http://schemas.xmlsoap.org/soap/envelope/
[ns1] => http://www.beautyfort.com/api/
[xsi] => http://www.w3.org/2001/XMLSchema-instance
)
Run Code Online (Sandbox Code Playgroud)
列出根及其所有子项的所有声明的命名空间:
php > print_r($xml->getDocNamespaces(true));
Array
(
[SOAP-ENV] => http://schemas.xmlsoap.org/soap/envelope/
[ns1] => http://www.beautyfort.com/api/
[xsi] => http://www.w3.org/2001/XMLSchema-instance
)
Run Code Online (Sandbox Code Playgroud)
simplexml_load_string允许设置默认名称空间作为参数,例如SOAP-ENV(使用额外的参数来指示它是前缀):
php > $xml = simplexml_load_string($content, "SimpleXMLElement", 0, "SOAP-ENV", true);
Run Code Online (Sandbox Code Playgroud)
如果您现在打印变量,您将更接近您所需要的:
php > print_r($xml);
SimpleXMLElement Object
(
[Body] => SimpleXMLElement Object
(
)
)
Run Code Online (Sandbox Code Playgroud)
但这仅适用于根节点。
列出子项时,如果子项使用默认(空)名称空间以外的名称空间,您仍然需要指定名称空间,并指示它是前缀还是名称空间:
php > print_r($xml->children("SOAP-ENV", true));
SimpleXMLElement Object
(
[Body] => SimpleXMLElement Object
(
)
)
Run Code Online (Sandbox Code Playgroud)
要查看第一个根子节点的子节点,请执行以下操作:
php > print_r($xml->children("SOAP-ENV", true)->children("ns1", true));
SimpleXMLElement Object
(
[ProductSearchResponse] => SimpleXMLElement Object
(
...
)
)
Run Code Online (Sandbox Code Playgroud)
但这可能不是您正在寻找的,因为它不是通用解决方案。
对所有命名空间使用字符串替换并重新加载 XML 文档(存储在 中$content):
php > $content2 = str_replace(array_map(function($e) { return "$e:"; }, array_keys($xml->getDocNamespaces())), array(), $content);
Run Code Online (Sandbox Code Playgroud)
您的输入 XML 现在如下所示:
php > echo $content2;
<?xml version="1.0" encoding="UTF-8"?><Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.beautyfort.com/api/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><Body><ProductSearchResponse><TestMode>false</TestMode><Page>1</Page><ResultsPerPage>1</ResultsPerPage><TotalResults>1276</TotalResults><Items><Item><StockCode>L3720</StockCode><Name>David Beckham Instinct Gift Set 30ml EDT + 150ml Shower Gel</Name><QuantityAvailable>1</QuantityAvailable><UnitPrice Currency="GBP"><Amount>8.72</Amount></UnitPrice><YourRating nil="true"/><YourStockCode></YourStockCode><ImageLastUpdated>2016-12-22 18:13:08</ImageLastUpdated><ThumbnailImageUrl>https://www.beautyfort.com/pic/Y0NqeTBJbmdvaUx6ZUFOa0MyTlNObmhGckltYnVQQmg%3D</ThumbnailImageUrl><HighResImageUrl nil="true"/></Item></Items></ProductSearchResponse></Body></Envelope>
Run Code Online (Sandbox Code Playgroud)
重新加载并打印:
php > $xml2 = simplexml_load_string($content2);
php > print_r($xml2);
SimpleXMLElement Object
(
[Body] => SimpleXMLElement Object
(
[ProductSearchResponse] => SimpleXMLElement Object
(
...
)
)
)
Run Code Online (Sandbox Code Playgroud)