doushi7761 2015-04-09 19:33
浏览 134

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

I am trying to create a SOAP request for an API like below in some case I will need to add other tags under <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>

What is the correct way in PHP to write this code not using old and simple fashion string concatenation technique.

I thought about using SimpleXMLElement

so here is what I have done

$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());

But the output show up like this

<?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>

How can I correct the output?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 r语言蛋白组学相关问题
    • ¥15 Python时间序列如何拟合疏系数模型
    • ¥15 求学软件的前人们指明方向🥺
    • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
    • ¥20 双层网络上信息-疾病传播
    • ¥50 paddlepaddle pinn
    • ¥20 idea运行测试代码报错问题
    • ¥15 网络监控:网络故障告警通知
    • ¥15 django项目运行报编码错误
    • ¥15 STM32驱动继电器