2013-01-07 113 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数据。