0

我对服务器端的事情非常陌生。我需要设置一个简单的 server.php 来接收 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>

显然,我可以在里面看到一个用户名/密码。
验证用户/通行证是很容易的部分,但我该如何解析呢?

4

3 回答 3

0

试试 SimpleXML,这里有几个例子:

SimpleXML 示例

于 2013-01-07T08:06:42.050 回答
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;
于 2013-01-07T08:12:09.810 回答
0

在解析 SOAP 请求时,您可能应该使用现有的 API。这不仅会自动消除您的解析问题,还会生成正确的 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数据。

于 2013-01-07T09:05:49.397 回答