如何将XML SOAP响应转换为php字符串

时间:2015-12-10 23:47:15

标签: php soap

我的一个客户在SOAP上创建了一个Web服务:

    POST /TiendaOnLine.asmx HTTP/1.1
Host: tiendawow.webvillanet.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tiendawow.webvillanet.com/ObtenerExistencia"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <ObtenerExistencia xmlns="http://tiendawow.webvillanet.com/">
      <articulo>string</articulo>
      <usuario>string</usuario>
      <password>string</password>
    </ObtenerExistencia>
  </soap:Body>
</soap:Envelope>

然后我创建了一个php文件来使用Web服务:

    <?php
            $url="http://tiendawow.webvillanet.com/TiendaOnLine.asmx?WSDL";
            $parameters=array();
            $parameters['articulo']="code";
            $parameters['usuario']="user";
$parameters['password']="pass";        
            $client = new SoapClient($url,$parameters);
            $result = $client->ObtenerExistencia($parameters);
            var_dump($result);
    ?>

我得到这样的输出:

object(stdClass)#2 (1) { ["ObtenerExistenciaResult"]=> string(5) "11.00" } 

我需要的是能够仅将11.00(或任何值取决于articulo)保存到变量以供以后使用,例如将其保存在数据库中。 ¿我该怎么做?

1 个答案:

答案 0 :(得分:0)

您需要访问ObtenerExistenciaResult对象中的属性$result

if(property_exists($result, 'ObtenerExistenciaResult')) {
    $value = $result->ObtenerExistenciaResult;
}
如果属性存在,

$value现在将包含字符串11.00

你应该有一些逻辑来处理错误并确保回来的数据符合你的期望。但是,假设它存在,这将获得该属性的值。

PHP Objects

property_exists

相关问题