亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

需要從PHP中的cURL XML響應中刪除標頭

需要從PHP中的cURL XML響應中刪除標頭

PHP
慕桂英546537 2021-05-13 14:11:07
關于此有一些線程,但是我在其中找不到解決此問題的解決方案。我希望它不會違反重復的規則。我已經使用靜態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 valuesif ($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 回答

?
滄海一幻覺

TA貢獻1824條經驗 獲得超5個贊

處理響應字符串的方法不是一個好主意,您應該堅持將內容作為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>';

        }

    }

}


查看完整回答
反對 回復 2021-05-28
?
慕哥6287543

TA貢獻1831條經驗 獲得超10個贊

SOAP響應仍然是XML文檔,因此請與其一起使用而不是與之抗爭。將其視為字符串絕對不是很好。


據我所知,您正在嘗試使用所有<cotizaction>元素。在XML文檔中查找元素很簡單。在XPath上閱讀。


$xml = simplexml_load_string(htmlspecialchars($reply));

if ($xml) {

    foreach ($xml->xpath('//cotizacion') as $cotizacion) {

        // do your thing

    }

}


查看完整回答
反對 回復 2021-05-28
  • 2 回答
  • 0 關注
  • 207 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號