我如何在 php 中读取 Folloing Soap 响应

<?php

$xml ='<?xml version="1.0" encoding="UTF-8"?>

<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://www.promostandards.org/WSDL/ProductDataService/1.0.0/SharedObjects/" xmlns:ns2="http://www.promostandards.org/WSDL/ProductDataService/1.0.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <env:Body>

        <ns2:GetProductResponse>

            <ns2:Product>

                <ns1:productId>1322059</ns1:productId>

                <ns2:productName>Smart WiFi Security Camera</ns2:productName>

                <ns2:productBrand>AAkron Line</ns2:productBrand>

                <ns2:export>false</ns2:export>

                <ns2:ProductCategoryArray>

                    <ns2:ProductCategory>

                        <ns2:category>Cameras, Safety, Health Care, Insurance, Finance, Tech Industry, NEW for 2018, New for 2019</ns2:category>

                        <ns2:subCategory>N/A</ns2:subCategory>

                    </ns2:ProductCategory>

                </ns2:ProductCategoryArray>

            </ns2:Product>

        </ns2:GetProductResponse>

    </env:Body>

</env:Envelope>';

//header("Content-type: text/xml");

//echo $xml;

$data_xml = simplexml_load_string($xml);

print_r($data_xml);

?>

我已经尝试了其他参考资料中的所有内容,但没有任何效果。它输出一个空数组。


繁星点点滴滴
浏览 156回答 3
3回答

吃鸡游戏

如果您使用DOMDocument并且DOMXPath可以使用 XPath 查询从 XML 中找到您需要的信息。$xml ='<?xml version="1.0" encoding="UTF-8"?><env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://www.promostandards.org/WSDL/ProductDataService/1.0.0/SharedObjects/" xmlns:ns2="http://www.promostandards.org/WSDL/ProductDataService/1.0.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">&nbsp; &nbsp; <env:Body>&nbsp; &nbsp; &nbsp; &nbsp; <ns2:GetProductResponse>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ns2:Product>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ns1:productId>1322059</ns1:productId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ns2:productName>Smart WiFi Security Camera</ns2:productName>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ns2:productBrand>AAkron Line</ns2:productBrand>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ns2:export>false</ns2:export>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ns2:ProductCategoryArray>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ns2:ProductCategory>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ns2:category>Cameras, Safety, Health Care, Insurance, Finance, Tech Industry, NEW for 2018, New for 2019</ns2:category>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ns2:subCategory>N/A</ns2:subCategory>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </ns2:ProductCategory>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </ns2:ProductCategoryArray>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </ns2:Product>&nbsp; &nbsp; &nbsp; &nbsp; </ns2:GetProductResponse>&nbsp; &nbsp; </env:Body></env:Envelope>';$dom=new DOMDocument;$dom->loadXML( $xml );$xp=new DOMXPath( $dom );$xp->registerNamespace('ns1','http://www.promostandards.org/WSDL/ProductDataService/1.0.0/SharedObjects/');$xp->registerNamespace('ns2','http://www.promostandards.org/WSDL/ProductDataService/1.0.0/');$xp->registerNamespace('xsi','http://www.w3.org/2001/XMLSchema-instance');$xp->registerNamespace('env','http://www.w3.org/2003/05/soap-envelope');$products=$xp->query('//ns2:Product');if( $products->length > 0 ){&nbsp; &nbsp; foreach( $products as $product ){&nbsp; &nbsp; &nbsp; &nbsp; $id=$xp->query( 'ns1:productId', $product )->item(0)->nodeValue;&nbsp; &nbsp; &nbsp; &nbsp; $name=$xp->query( 'ns2:productName', $product )->item(0)->nodeValue;&nbsp; &nbsp; &nbsp; &nbsp; $brand=$xp->query( 'ns2:productBrand', $product )->item(0)->nodeValue;&nbsp; &nbsp; &nbsp; &nbsp; $export=$xp->query( 'ns2:export', $product )->item(0)->nodeValue;&nbsp; &nbsp; &nbsp; &nbsp; $category=$xp->query( 'ns2:ProductCategoryArray/ns2:ProductCategory/ns2:category', $product )->item(0)->nodeValue;&nbsp; &nbsp; &nbsp; &nbsp; $subcategory=$xp->query( 'ns2:ProductCategoryArray/ns2:ProductCategory/ns2:subCategory', $product )->item(0)->nodeValue;&nbsp; &nbsp; &nbsp; &nbsp; echo $id, $name, $brand, $category, $subcategory;&nbsp; &nbsp; }}输出:1322059Smart WiFi Security CameraAAkron LineCameras, Safety, Health Care, Insurance, Finance, Tech Industry, NEW for 2018, New for 2019N/A

慕田峪4524236

您还可以使用 SimpleXML 和Xpath。function getNsValue($xml, $path, $index = 0){&nbsp; return (string)$xml->xpath($path)[$index];}xml测试数据:$xml ='<?xml version="1.0" encoding="UTF-8"?><env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://www.promostandards.org/WSDL/ProductDataService/1.0.0/SharedObjects/" xmlns:ns2="http://www.promostandards.org/WSDL/ProductDataService/1.0.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">&nbsp; &nbsp; <env:Body>&nbsp; &nbsp; &nbsp; &nbsp; <ns2:GetProductResponse>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ns2:Product>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ns1:productId>1322059</ns1:productId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ns2:productName>Smart WiFi Security Camera</ns2:productName>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ns2:productBrand>AAkron Line</ns2:productBrand>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ns2:export>false</ns2:export>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ns2:ProductCategoryArray>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ns2:ProductCategory>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ns2:category>Cameras, Safety, Health Care, Insurance, Finance, Tech Industry, NEW for 2018, New for 2019</ns2:category>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ns2:subCategory>N/A</ns2:subCategory>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </ns2:ProductCategory>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </ns2:ProductCategoryArray>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </ns2:Product>&nbsp; &nbsp; &nbsp; &nbsp; </ns2:GetProductResponse>&nbsp; &nbsp; </env:Body></env:Envelope>';$data_xml = simplexml_load_string($xml);例子:$category = getNsValue($data_xml,'//ns2:category');$productId = getNsValue($data_xml,'//ns1:productId');var_dump($category, $productId);/*string(91) "Cameras, Safety, Health Care, Insurance, Finance, Tech Industry, NEW for 2018, New for 2019"string(7) "1322059"*/&nbsp; &nbsp;&nbsp;

慕盖茨4494581

我已经使用以下代码解决了我的问题-$response = file_get_contents($xml);$res=simplexml_load_string($response);print_r($res);
打开App,查看更多内容
随时随地看视频慕课网APP