用PHP调用SOAP服务(XML)

时间:2018-10-16 09:56:36

标签: php wordpress web-services soap

我有一个可以使用SOAP / XML的供应商网络服务,并且正在尝试从运行PHP的网站向其发送请求。

可以在以下位置找到WSDL:https://www.gateway.uat.asic.gov.au/gateway/ExternalSearchNniNamePortV3?wsdl

调用此WS的请求示例为:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<uri:request xmlns:uri="uri:v3.external.search.nni.name.asic.gov.au" oas:Id="Example-1">
    <uri1:businessDocumentHeader xmlns:uri1="uri:business.document.header.types.asic.gov.au">
        <uri1:messageType>searchNniName</uri1:messageType>
        <uri1:messageReferenceNumber>[messageReferenceNumber]</uri1:messageReferenceNumber>
        <uri1:messageVersion>3</uri1:messageVersion>
        <uri1:senderId>[senderId]</uri1:senderId>
        <uri1:senderType>REGA</uri1:senderType>
    </uri1:businessDocumentHeader>
    <uri:businessDocumentBody>
        <uri4:searchType xmlns:uri4="uri:nni.types.asic.gov.au">S</uri4:searchType>
        <uri4:searchScope xmlns:uri4="uri:nni.types.asic.gov.au">2</uri4:searchScope>
        <uri4:organisation xmlns:uri4="uri:nni.types.asic.gov.au">
            <uri4:name>ARROW RESOURCES MANAGEMENT PTY. LIMITED</uri4:name>
        </uri4:organisation>
        <uri4:maxResult xmlns:uri4="uri:nni.types.asic.gov.au">1</uri4:maxResult>
    </uri:businessDocumentBody>
</uri:request>

到目前为止,我的PHP代码如下:

$wsdl = "https://www.gateway.uat.asic.gov.au/gateway/ExternalSearchNniNamePortV3?wsdl";
        //$client = new SoapClient($wsdl);

        try {

            $soapClientOptions = array(
                'cache_wsdl' => 0,
                'trace' => 1,
                'login' => '***',
                'password' => '***',
                'stream_context' => stream_context_create(array(
                    'ssl' => array(
                        'verify_peer' => false,
                        'verify_peer_name' => false,
                        'allow_self_signed' => true,
                    ),
                )),
            );
            $client = new SoapClient($wsdl, $soapClientOptions);

            $xml = new XMLWriter();
            $xml->openMemory();
            $xml->startElementNS('uri', "request", 'uri:v3.external.search.nni.name.asic.gov.au');
            $xml->writeAttributeNS(
                'oas',
                'Id',
                'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd',
                'Example-1'
              );
                $xml->startElementNS('uri1', "businessDocumentHeader", "uri:business.document.header.types.asic.gov.au");
                    $xml->startElementNS('uri1', 'messageType', null);
                        $xml->Text("searchNniName");
                    $xml->endElement();
                    $xml->startElementNS('uri1', 'messageVersion', null);
                        $xml->Text("3");
                    $xml->endElement();
                    $xml->startElementNS('uri1', 'senderId', null);
                        $xml->Text("0000123");
                    $xml->endElement();
                    $xml->startElementNS('uri1', 'senderType', null);
                        $xml->Text("REGA");
                    $xml->endElement();
                $xml->endElement();
                $xml->startElementNS('uri', "businessDocumentBody", null);
                    $xml->startElementNS('uri4', 'searchType', "uri:nni.types.asic.gov.au");
                        $xml->Text("searchNniName");
                    $xml->endElement();
                    $xml->startElementNS('uri4', 'searchScope', "uri:nni.types.asic.gov.au");
                        $xml->Text("3");
                    $xml->endElement();
                    $xml->startElementNS('uri4', 'organization', "uri:nni.types.asic.gov.au");
                        $xml->startElementNS('uri4', 'name', null);
                            $xml->Text("Caleidro");
                        $xml->endElement();
                    $xml->endElement();
                    $xml->startElementNS('uri4', 'maxResult', "uri:nni.types.asic.gov.au");
                        $xml->Text("1");
                    $xml->endElement();
                $xml->endElement();
            $xml->endElement();

            //Convert it to a valid SoapVar
            $args = new SoapVar($xml->outputMemory(), XSD_ANYXML);

            $request_param = array(
                "request" => array(
                    "businessDocumentHeader" => array(
                        "messageType" => "searchNniName",
                        //"messageReferenceNumber" => "???",
                        "messageVersion" => 3,
                        "senderId" => "0000123",
                        "senderType" => "REGA",
                    ),
                    "businessDocumentBody" => array(
                        "searchType" => "S", //for the Standard search where all matching entries will be returned
                        "searchScope" => "A", //search for all identities
                        "organisation" => array(
                            "name" => "Caleidro",
                        ),
                        "maxResult" => 1,
                    )
                )
            );

            $response_param = $client->externalSearchNniName(array($args));
            //$xml = simplexml_load_string($response_param);
            //$result = $response_param->AddResult;
            exit('My response is ' . print_r($response_param));
        } catch (Exception $e) {

            echo "Exception Error!";
            exit($e->getMessage());

        }

}

很不幸,它抛出异常并显示以下消息:SOAP-ERROR: Encoding: object has no 'businessDocumentHeader' property

我觉得自己创建的XMLWriter错误。但是我找不到问题。

任何帮助将不胜感激。谢谢

编辑

我正在打印错误并提供更多信息

exit('ERROR ' . $e->getMessage() . ' ====== ' . $client->__getLastRequest());

现在的消息是:

Fatal error: SOAP-ERROR: Encoding: object has no 'businessDocumentHeader' property in /home/caleidro/public_html/wp-content/plugins/insert-php/includes/class.plugin.php(184) : eval()'d code on line 90

实际上,我似乎在错误地编写XMLWritter

0 个答案:

没有答案