2013-01-07 53 views
0

我對服務器端的東西非常陌生。我需要設置一個簡單的server.php來接收XML請求。解析XML請求

我甚至不知道如何從這開始。我習慣於從表單中正常的Post/Get變量。

這裏是什麼,我聽的,並需要以迴應的例子:

<?xml version="1.0" encoding="UTF-8"?> 
<SOAP-ENV:Envelope 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" 
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" 
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> 
    <SOAP-ENV:Body> 
     <authenticate xmlns="http://someplace.someplace.com/"> 
      <strUserName xsi:type="xsd:string">username</strUserName> 
      <strPassword xsi:type="xsd:string">password</strPassword> 
     </authenticate> 
    </SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 

顯然有一個用戶名/密碼,我可以看到裏面。
驗證用戶/密碼很容易,但我該如何解析?

+2

http://php.net/manual/en/class.soapserver.php –

回答

0

您還可以使用DOM元素來訪問所需的值:

$xml = '<?xml version="1.0" encoding="UTF-8"?> 
<SOAP-ENV:Envelope 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" 
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" 
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> 
    <SOAP-ENV:Body> 
     <authenticate xmlns="http://someplace.someplace.com/"> 
      <strUserName xsi:type="xsd:string">username</strUserName> 
      <strPassword xsi:type="xsd:string">password</strPassword> 
     </authenticate> 
    </SOAP-ENV:Body> 
</SOAP-ENV:Envelope>'; 

$dom = new DOMDocument(); 
$dom->loadXML($xml); 

$username = $dom->getElementsByTagName('strUserName')->item(0)->nodeValue; 
$password = $dom->getElementsByTagName('strPassword')->item(0)->nodeValue; 
+0

問題,你把實際的xml放在$ xml var中。 如果server.php正在監聽,它是一個$ _POST?如何將請求放入$ xml中? – coffeemonitor

+0

取決於請求,但是,它將是'post'或'get'值。只要找出,女巫價值包含的要求和使用。 – Peon

0

你或許應該使用現有的API在解析SOAP請求時。這不僅會自動解決您的解析問題,還會生成正確的SOAP輸出。

例子:

$request = '<?xml version="1.0" encoding="UTF-8"?> 
<SOAP-ENV:Envelope 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" 
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" 
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> 
    <SOAP-ENV:Body> 
     <authenticate xmlns="http://someplace.someplace.com/"> 
      <strUserName xsi:type="xsd:string">foo</strUserName> 
      <strPassword xsi:type="xsd:string">bar</strPassword> 
     </authenticate> 
    </SOAP-ENV:Body> 
</SOAP-ENV:Envelope>'; 

$s = new SoapServer(NULL, array('uri' => 'http://someplace.someplace.com/')); 
$s->setClass("Auth"); 
$s->handle($request); 

class Auth 
{ 
    public function authenticate($strUserName, $strPassword) 
    { 
     return "U: $strUserName; P: $strPassword"; 
    } 
} 

注意:如果你沒有一個參數傳遞給handle()它將使用POST數據。