猿问

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

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


我已经使用静态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     

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>';

        }

    }

}

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


慕桂英546537
浏览 185回答 2
2回答

沧海一幻觉

处理响应字符串的方法不是一个好主意,您应该坚持将内容作为XML处理并使用。这使用XPath查找处理数据的起点(我无法使用当前示例进行测试),但是应该可以帮助您完成所需的工作...// Load the original reply$xml = simplexml_load_string($reply);//running through the array and printing all valuesif ($xml !== false) {&nbsp; &nbsp; // Find the <auto> element (use [0] as you want the first one)&nbsp; &nbsp; $auto = $xml->xpath("//auto")[0];&nbsp; &nbsp; // Loop through the cotizacion elements in the auto element&nbsp; &nbsp; foreach ($auto->cotizacion as $cotizacion) {&nbsp; &nbsp; &nbsp; &nbsp; foreach ($cotizacion->cobertura as $cobertura) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo $cobertura->codigo;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '<br>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo $cobertura->descripcion;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '<br>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo $cobertura->premio;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '<br>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo $cobertura->cuotas;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '<br>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo $cobertura->impcuotas;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '<br>';&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

慕哥6287543

SOAP响应仍然是XML文档,因此请与其一起使用而不是与之抗争。将其视为字符串绝对不是很好。据我所知,您正在尝试使用所有<cotizaction>元素。在XML文档中查找元素很简单。在XPath上阅读。$xml = simplexml_load_string(htmlspecialchars($reply));if ($xml) {&nbsp; &nbsp; foreach ($xml->xpath('//cotizacion') as $cotizacion) {&nbsp; &nbsp; &nbsp; &nbsp; // do your thing&nbsp; &nbsp; }}
随时随地看视频慕课网APP
我要回答