如何在php

时间:2016-11-06 04:27:31

标签: php xml soap

我有SoapUI使用这个xml,但我需要使用php 5.3在我的服务器上使用该数据。我想我需要将$ string转换为数组。 $ xml =(array)simplexml_load_string($ string);不会抛出任何错误,但调用的响应为NULL。

$string =   '
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tran="http://www.cornerstoneondemand.com/Webservices/TranscriptAndTask">
    <soapenv:Header>
        <wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
            <wsse:UsernameToken wsu:Id="UsernameToken-115E54B97689076253912">
                <wsse:Username>me</wsse:Username>
                <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">word</wsse:Password>
                <wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">dvhXLFiL4Aoi2KQ==</wsse:Nonce>
                <wsu:Created>2016-10-19T15:26:02.539Z</wsu:Created>
            </wsse:UsernameToken>
        </wsse:Security>
    </soapenv:Header>
    <soapenv:Body>
      <tran:GetTranscriptAndTasks>
         <tran:request>
            <Request corpName="learning">
                <User id="me">
                    <RequestTypes>
                        <Inbox/>
                        <Transcript inprogressOnly="false" pageNumber="1"/>
                        <Session pageNumber="1" upcomingOnly="true"/>
                        <Assigned assignedOnly="true"/>
                        <Approval approvalDateRequested="1967-08-13"/>
                        <Task pendingTasksOnly="true"/>
                        <SuggestedTraining pageNumber="1"/>
                    </RequestTypes>
                </User>
            </Request>
         </tran:request>
      </tran:GetTranscriptAndTasks>
    </soapenv:Body>
</soapenv:Envelope>
';


$xml = (array)simplexml_load_string($string);

$soapClient = new SoapClient($wsdl, array('trace' => 1));

$response = $soapClient->GetTranscriptAndTasks($xml);
var_dump($response);

非常感谢任何帮助!

编辑:我发现https://github.com/sapankumarmohanty/lamp/blob/master/Crate-XML-2-Array会将xml变成一个漂亮的数组。但我的结果仍然是NULL ... 如果它有帮助,我在这里复制了WSDL http://www.markforsyth.com/TranscriptAndTaskService.wsdl

1 个答案:

答案 0 :(得分:1)

您实际上可以直接使用SoapUI中的xml代码。

以下是我的代码中的一些代码片段:

我的类的构造函数,它作为Web服务的接口提供服务。 wsdl被定义为类中的常量:

public function __construct($username, $password) {
    $this->client = new SoapClient(self::wsdl);
    $this->client->__setSoapHeaders(self::securityHeader($username, $password));
}

我的类中返回构造函数中使用的安全标头的函数:

private static function securityHeader($username, $password) {
    $nsWSSE = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";
    $nsWSU = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd";
    $nonce = 'xxxxx';

    $xml = '<nsWSSE:Security xmlns:nsWSSE="' . $nsWSSE . '" xmlns:nsWSU="' . $nsWSU . '">'
        . '<nsWSSE:UsernameToken>'
        . '<nsWSSE:Username>' . $username . '</nsWSSE:Username>'
        . '<nsWSSE:Password>' . $password . '</nsWSSE:Password>'
        . '<nsWSSE:Nonce>' . $nonce . '</nsWSSE:Nonce>'
        . '<nsWSU:Created>' . gmdate('Y-m-d\TH:i:s\Z') . '</nsWSU:Created>'
        . '</nsWSSE:UsernameToken>'
        . '</nsWSSE:Security>';

    $securityToken = new SoapVar($xml, XSD_ANYXML);

    return new SoapHeader($nsWSSE, 'Security', $securityToken);
}

我班上的一个函数,它向WS函数发出请求&#34; abc&#34;:

public function abc() {
    $xml = ... paste your xml code from SoapUI here ...
    $param = new SoapVar($xml, XSD_ANYXML);
    return $this->client->abc($param);
}