繁体   English   中英

在php curl请求中从xml获取正文内容

[英]Get body content from xml in php curl request

我正在使用以下代码进行 SOAP 请求。

$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => "https://example.com?WSDL",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS =>"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns1=\"http://Xyz.Abc\" xmlns:ns2=\"http://tempuri.org/\"><SOAP-ENV:Body><ns2:GetStatus><ns1:ReferenceNo>12345</ns1:ReferenceNo></ns2:GetStatus></SOAP-ENV:Body></SOAP-ENV:Envelope>",
      CURLOPT_HTTPHEADER => array(
        "Content-Type: text/xml"
      ),
    ));

$response = curl_exec($curl);
curl_close($curl);
echo $response;

响应:当我设置header('Content-type: application/xml');时得到以下响应header('Content-type: application/xml'); 在正确的 php 脚本中。

<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsa="http://www.w3.org/2005/08/addressing">
    <env:Header>...</env:Header>
    <env:Body>
        <GetStatusResponse xmlns:s1="http://Xyz.Abc" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://tempuri.org/" xmlns="http://tempuri.org/">
            <s1:StatusResponse>
                <s1:Test ResponseCode="INPROGRESS" ResponseMessage="Reference no. 12345"/>
            </s1:StatusResponse>
        </GetStatusResponse>
    </env:Body>
</env:Envelope>

如何从响应中提取 Body 值而不只是在浏览器上打印?

这是我尝试过的

通过simplexml_load_string方法删除 xml 标头并解析响应字符串。

$xml = simplexml_load_string($response);
echo $xml;
$json = json_encode($response);
$arr = json_decode($json,true);
print_r($arr);

我也尝试过SimpleXMLElement ,但是,它正在打印空响应:

$test = new SimpleXMLElement($response);
echo $test;

但是,它正在打印空结果。

SimpleXML 是一个对象。 它不是您想象的完整 XML 字符串。 尝试这个。

//Cast to string to force simpleXml to convert into a string
$xmlString = $xml->asXML();
echo $xmlString . "\n";

我很确定你不能这样做,因为$response是 XML。 尝试 json_encode 它没有意义。

$json = json_encode($response);

我这样做了:

<?php
$x = '<?xml version="1.0" ?>
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsa="http://www.w3.org/2005/08/addressing">
<env:Header>...</env:Header>
<env:Body>
<GetStatusResponse xmlns:s1="http://Xyz.Abc" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://tempuri.org/" xmlns="http://tempuri.org/">
    <s1:StatusResponse>
        <s1:Test ResponseCode="INPROGRESS" ResponseMessage="Reference no. 12345"/>
    </s1:StatusResponse>
</GetStatusResponse>
</env:Body>
</env:Envelope>';
$xml = simplexml_load_string($x);
echo $xml->asXml();
echo "\n";

我得到这个输出:

<?xml version="1.0"?>
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsa="http://www.w3.org/2005/08/addressing">
<env:Header>...</env:Header>
<env:Body>
<GetStatusResponse xmlns:s1="http://Xyz.Abc" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://tempuri.org/" xmlns="http://tempuri.org/">
    <s1:StatusResponse>
        <s1:Test ResponseCode="INPROGRESS" ResponseMessage="Reference no. 12345"/>
    </s1:StatusResponse>
</GetStatusResponse>
</env:Body>
</env:Envelope>

这解释了如何指定命名空间。 https://www.php.net/manual/en/simplexmlelement.attributes.php

$xml->registerXPathNamespace('e', 'http://schemas.xmlsoap.org/soap/envelope/');
$xml->registerXPathNamespace('s', 'http://Xyz.Abc');
$result = $xml->xpath('//s:Test');
$response = $result[0]->attributes();
echo "Response Code = " . $response['ResponseCode'] . "\n";
echo "Response Message = " . $response['ResponseMessage'] . "\n";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM