需要从PHP中的cURL XML响应中删除标头

时间:2019-05-04 05:58:55

标签: php xml curl

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

我已经用静态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>';
        }
    }
}

可能有更有效的方法来执行此操作,或者我可能没有正确执行此操作。我现在正要学习,所以随时可以以任何方式纠正我,我将不胜感激!

2 个答案:

答案 0 :(得分: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>';
        }
    }
}

答案 1 :(得分:1)

SOAP响应仍然是XML文档,因此请与其一起使用而不是与之抗争。将其视为字符串绝对不好。

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

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