如何使用PHP动态创建SOAP请求

时间:2015-04-09 19:33:35

标签: php xml soap simplexml

我正在尝试为下面的API创建SOAP请求 在某些情况下,我需要在<ticketId>int</ticketId>

下添加其他标签
<?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
      <soap:Body>
        <GetServiceTicket xmlns="http://connectwise.com">
          <credentials>
            <CompanyId>string</CompanyId>
            <IntegratorLoginId>string</IntegratorLoginId>
            <IntegratorPassword>string</IntegratorPassword>
          </credentials>
          <ticketId>int</ticketId>
        </GetServiceTicket>
      </soap:Body>
    </soap:Envelope>

PHP使用旧的简单时尚字符串连接技术编写此代码的正确方法是什么。

我考虑过使用SimpleXMLElement

所以这就是我所做的

$xml = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><xml/>');

$envelop = $xml->addChild('soap:Envelope');
$envelop->addAttribute('xmlns:xsi','http://www.w3.org/2001/XMLSchema-instance');
$envelop->addAttribute('xmlns:xsd','http://www.w3.org/2001/XMLSchema');
$envelop->addAttribute('xmlns:soap','http://schemas.xmlsoap.org/soap/envelope/');
$body = $envelop->addChild('soap:Body');
$action = $body->addChild('GetServiceTicket');
$action->addAttribute('xmlns','http://connectwise.com');
$credentials = $action->addChild('credentials');

$CompacnyId = $credentials->addChild('CompacnyId','string');
$IntegratorLoginId = $credentials->addChild('IntegratorLoginId','string');
$IntegratorPassword = $credentials->addChild('IntegratorPassword','string');

$ticketId = $action->addChild('ticketId', 'int');

Header('Content-type: text/xml');
print($xml->asXML());

但输出显示如下

<?xml version="1.0" encoding="utf-8"?>
<xml>
    <Envelope xsi="http://www.w3.org/2001/XMLSchema-instance" xsd="http://www.w3.org/2001/XMLSchema" soap="http://schemas.xmlsoap.org/soap/envelope/">
        <Body>
            <GetServiceTicket xmlns="http://connectwise.com">
                <credentials>
                    <CompacnyId>string</CompacnyId>
                    <IntegratorLoginId>string</IntegratorLoginId>
                    <IntegratorPassword>string</IntegratorPassword>
                </credentials>
                <ticketId>int</ticketId>
            </GetServiceTicket>
        </Body>
    </Envelope>
</xml>

如何更正输出?

0 个答案:

没有答案