2016-01-28 144 views
-3

這是我的請求發送PHP解析XML內容

$header = "POST 213.207.000.000/services/AmountCharging/v3 HTTP/1.1 \r\n"; $header .= "Content-type: text/xml;charset=UTF-8 \r\n"; 
$header .= "Content-length: ".strlen($request_xml)." \r\n"; 
$header .= "Content-transfer-encoding: text \r\n"; 
$header .= "Connection: Keep-Alive \r\n\r\n"; 
$header .= $request_xml; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($ch, CURLOPT_URL,$url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_TIMEOUT, 120); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header); 
$strxml = curl_exec($ch); 

下面是我的XML響應,並使用PHP我要搶的<soapenv:Body></soapenv:Body>

<?xml version="1.0" encoding="utf-8" ?> 
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
<soapenv:Body> 
<ns1:chargeAmountResponse xmlns:ns1="http://www.csapi.org/schema/parlayx/payment/amount_charging/v3_1/local"></ns1:chargeAmountResponse> 
</soapenv:Body> 
</soapenv:Envelope> 

任何想法,只是內容? 謝謝..

+0

沒有真正的內容 - 一個命名空間的空標籤是所有 – RamRaider

+0

看到的http://stackoverflow.com/questions/16412047/parse-xml-namespaces-with-php-simplexml – michi

+0

可能的複製[你如何解析和處理PHP中的HTML/XML?](http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) –

回答

0

鑑於在soapenv:Body內確實沒有什麼值得一看的,下面並沒有真正顯示任何有趣的內容,但應該有所幫助。

$strxml='<?xml version="1.0" encoding="utf-8" ?> 
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <soapenv:Body> 
     <ns1:chargeAmountResponse xmlns:ns1="http://www.csapi.org/schema/parlayx/payment/amount_charging/v3_1/local"></ns1:chargeAmountResponse> 
    </soapenv:Body> 
</soapenv:Envelope>'; 

$dom=new DOMDocument; 
$dom->loadXML($strxml); 
$body=$dom->getElementsByTagNameNS('http://schemas.xmlsoap.org/soap/envelope/','Body')->item(0); 
echo 'Body: '.$body->tagName.', Child: '.$body->childNodes->item(1)->tagName.', Attribute: '.$body->childNodes->item(1)->getAttribute('xmlns:ns1'); 


Output 
------ 
Body: soapenv:Body, Child: ns1:chargeAmountResponse, Attribute: http://www.csapi.org/schema/parlayx/payment/amount_charging/v3_1/local 


update: 
------- 

Given that it does appear supplying a single line string causes errors 
you could aproach it slightly differently. 

$col=$dom->getElementsByTagName('*'); 
foreach($col as $node) { 
    echo 'tag:'.$node->tagName.' value:'.$node->nodeValue.BR; 
} 
+0

對於行回聲我得到以下錯誤信息: - >注意:試圖獲取非對象的屬性 致命錯誤:調用成員函數getAttribute() – andreas777

+0

RamRaider,當我把$ strxml作爲上面的例子你說它的工作原理,但當我從$ strxml = curl_exec($ ch)將它合併到我的代碼中時;它不,這是什麼? – andreas777

+0

,這取決於你的代碼 - 沒有看到代碼很難說。 – RamRaider