在PHP SoapClient请求中更改名称空间?

时间:2016-10-17 03:19:55

标签: php xml soap laravel-4

我正在寻找一种更改命名空间的方法:" ns1"进入" ret",使用SoapUI测试下面的XML,命名空间设置为" ret"并且请求成功了。我已经用Google搜索了#34;并在此搜索了其他相关问题的答案,但没有运气。所以,我有点想要找到答案......

以下是生成的要发送给请求的XML:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://retailexpress.com.au/">
    <SOAP-ENV:Header>
        <ns1:ClientHeader>
            <ns1:ClientID>Random-hash-clientID</ns1:ClientID>
            <ns1:UserName>Username</ns1:UserName>
            <ns1:Password>Password</ns1:Password>
        </ns1:ClientHeader>
    </SOAP-ENV:Header>
    <SOAP-ENV:Body>
        <ns1:CustomerGetBulkDetails>
            <ns1:LastUpdated>2000-01-01T00:00:00.000Z</ns1:LastUpdated>
            <ns1:OnlyCustomersWithEmails>1</ns1:OnlyCustomersWithEmails>
        </ns1:CustomerGetBulkDetails>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

尽管如此,我们必须要求使用相同的命名空间(ret)似乎有点奇怪,但那是怎么回事......

以下是用于生成上述内容的PHP代码:

    $rexHost = '<domain of retail express>';
    $rexApi = '<URI of retail express API / wsdl path>';
    $rexUser = 'Username';
    $rexPassword = 'Password';
    $rexApiClient = 'Random-hash-clientID';
    $rexApiHost = 'http://retailexpress.com.au/';

    $client = new SoapClient($rexHost.$rexApi, array('trace' => true));

    $auth = new stdClass();
    $auth->ClientID = $rexApiClient;
    $auth->UserName = $rexUser;
    $auth->Password = $rexPassword;

    $header = new SoapHeader($rexApiHost, 'ClientHeader', $auth, false);
    $client->__setSoapHeaders($header);

    $lastUpdate = '2000-01-01T00:00:00.000Z'; //hardcoded for test

    $params = array();
    $params[] = new SoapVar($lastUpdate, XSD_DATETIME, null, null, 'LastUpdated', $rexApiHost);
    $params[] = new SoapVar(1, XSD_INTEGER, null, null, 'OnlyCustomersWithEmails', $rexApiHost);

    try {
        $users = null;
        return $users = $client->CustomerGetBulkDetails( new SoapVar($params, SOAP_ENC_OBJECT));
    } catch (Exception $e) {
        Log::info($e->getMessage());
        Log::info($client->__getLastRequest()); //laravel logger, where I got the generated SOAP XML request
        return false;
    }

1 个答案:

答案 0 :(得分:2)

$ docker run -it --rm -v /c/projects:/home/projects php:7.0-cli ls -l /home/projects 不是命名空间,而是它的别名。 ns1是命名空间。命名空间定义http://retailexpress.com.au/定义当前元素及其后代的别名。命名空间必须是唯一且稳定的。使用定义和别名允许用作命名空间的复杂URI和序列化的简短可读别名。

以下三个示例都由XML解析器解析为名称空间xmlns:ns1="http://retailexpress.com.au/"中的元素CustomerGetBulkDetails

  • http://retailexpress.com.au/
  • <ns1:CustomerGetBulkDetails xmlns:ns1="http://retailexpress.com.au/"/>
  • <ret:CustomerGetBulkDetails xmlns:ret="http://retailexpress.com.au/"/>

换句话说,如果XML / SOAP实现正常工作,那么命名空间使用哪个别名 - <CustomerGetBulkDetails xmlns="http://retailexpress.com.au/"/>ns1无关紧要。