【问题标题】:Need to remove headers from cURL XML response in PHP需要从 PHP 中的 cURL XML 响应中删除标头
【发布时间】:2019-05-04 05:58:54
【问题描述】:

有一些关于此的主题,但我无法在其中找到解决此问题的方法。我希望它不会违反重复的规则。

我用静态 XML 测试了以下代码,效果很好,但是说 XML 不包含任何标题。

我正在尝试在发出 POST 请求后通过代码删除标头,以便我可以继续处理生成的 XML,但我没有任何运气。

这是 XML:

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><AUTOS_Cotizar_PHPResponse xmlns="http://tempuri.org/"><AUTOS_Cotizar_PHPResult><auto xmlns=""><operacion>1555843</operacion><statusSuccess>TRUE</statusSuccess><statusText></statusText><cotizacion><cobertura><codigo>A0</codigo><descripcion>RESPONSABILIDAD CIVIL SOLAMENTE</descripcion><premio>928,45</premio><cuotas>01</cuotas><impcuotas>928,45</impcuotas></cobertura></cotizacion><datos_cotiz><suma>477250</suma><uso>901</uso></datos_cotiz></auto></AUTOS_Cotizar_PHPResult></AUTOS_Cotizar_PHPResponse></soap:Body></soap:Envelope>

这是代码:

//converting raw cURL response to XML
$temp1 = htmlspecialchars ($reply); 
//replacing top headers
$temp2 = str_replace('<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><AUTOS_Cotizar_PHPResponse xmlns="http://tempuri.org/"><AUTOS_Cotizar_PHPResult>', "<<<'EOD'", $temp1);
//replacing closing header tags     
$temp3 = str_replace('</AUTOS_Cotizar_PHPResult></AUTOS_Cotizar_PHPResponse></soap:Body></soap:Envelope>', "EOD;", $temp2);
//this returns the original $temp1 without having anything replaced
echo $temp3;

//simplexml conversion
$xml = simplexml_load_string($temp3);

//running through the array and printing all values
if ($xml !== false) {
    foreach ($xml->cotizacion as $cotizacion) {
        foreach ($cotizacion->cobertura as $cobertura) {
            echo $cobertura->codigo;
            echo '<br>';
            echo $cobertura->descripcion;
            echo '<br>';
            echo $cobertura->premio;
            echo '<br>';
            echo $cobertura->cuotas;
            echo '<br>';
            echo $cobertura->impcuotas;
            echo '<br>';
        }
    }
}

可能有更有效的方法可以做到这一点,或者我没有正确地做到这一点。我现在只是在学习,所以如果你愿意,请随时以任何方式纠正我,我将不胜感激!

【问题讨论】:

  • 您是否有任何包含您之后的元素的 XML 示例 - 该示例不包含类似 codigo 的任何内容。
  • 为什么用 cURL 标记?
  • 抱歉,我更新了 XML 代码,我错误地粘贴了不完整的响应。另外,用 cURL 标记它,因为这是我用来与 Web 服务通信的。

标签: php xml curl


【解决方案1】:

您处理响应字符串的方式是一个坏主意,您应该坚持将内容处理为 XML 并使用它。这使用 XPath 来找到处理数据的起点(我无法使用当前示例进行测试),但应该可以帮助您完成需要做的事情......

// Load the original reply
$xml = simplexml_load_string($reply);

//running through the array and printing all values
if ($xml !== false) {
    // Find the <auto> element (use [0] as you want the first one)
    $auto = $xml->xpath("//auto")[0];

    // Loop through the cotizacion elements in the auto element
    foreach ($auto->cotizacion as $cotizacion) {
        foreach ($cotizacion->cobertura as $cobertura) {
            echo $cobertura->codigo;
            echo '<br>';
            echo $cobertura->descripcion;
            echo '<br>';
            echo $cobertura->premio;
            echo '<br>';
            echo $cobertura->cuotas;
            echo '<br>';
            echo $cobertura->impcuotas;
            echo '<br>';
        }
    }
}

【讨论】:

    【解决方案2】:

    SOAP 响应仍然是一个 XML 文档,因此请使用它而不是对抗它。把它当作一个字符串来处理肯定不好。

    据我所知,您正在尝试使用所有 &lt;cotizaction&gt; 元素。在 XML 文档中查找元素很简单。阅读XPath

    $xml = simplexml_load_string(htmlspecialchars($reply));
    if ($xml) {
        foreach ($xml->xpath('//cotizacion') as $cotizacion) {
            // do your thing
        }
    }
    

    【讨论】:

    • 谢谢! xpath 可以完美运行,并且在未来也将非常有用。
    猜你喜欢
    • 2011-07-05
    • 1970-01-01
    • 2012-06-24
    • 2021-06-27
    • 2019-11-12
    • 1970-01-01
    • 2018-04-24
    • 2017-09-23
    • 2017-06-18
    相关资源
    最近更新 更多