【问题标题】:How to make PHP SOAP call with header security?如何使用标头安全性进行 PHP SOAP 调用?
【发布时间】:2016-12-15 20:31:15
【问题描述】:

我正在尝试使用nusoap 客户端和普通SoapClient 调用GetHotels 函数,调用该函数时遇到问题,它返回以下错误:

Uncaught SoapFault exception: [a: InternalServiceFault] Object reference not set to an instance of an object.

我将此代码与nusoap_client一起使用

   $client = new nusoap_client("http://amandaws.absolutent.it/Booking.svc" ,    'wsdl');
   $bodyxml =('<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
     xmlns:oas="http://docs.oasisopen.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
     xmlns:tem="http://tempuri.org/">
     <soapenv:Header>
     <oas:Security>
     <oas:UsernameToken>
     <oas:Username>XXX</oas:Username>
     <oas:Password>XXX</oas:Password>
     </oas:UsernameToken>
     </oas:Security>
     </soapenv:Header>
     <soapenv:Body>
     <tem:BGH_Request Language="IT">
     <tem:Criteria HotelCode="FID001" IDRegione="" IDProvincia="" IDComune="" IDLocalita="" IDLineaProdotto="" IDZona="" MaxResults="200" />
     </tem:BGH_Request>
     </soapenv:Body>
    </soapenv:Envelope>');
    $client->soap_defencoding = 'utf-8';
    $client->operation = "GetHotels";
    $result = $client->send($client->serializeEnvelope($bodyxml), "http://tempuri.org/IBooking/GetHotels");
    print_r($result);

Function that i need to call

当我打印 $result 时,我得到这个数组消息

   Array
   (
  [faultcode] => a:InternalServiceFault
[faultstring] => Array
    (
        [!xml:lang] => en-GB
        [!] => Object reference not set to an instance of an object.
    )

[detail] => Array
    (
        [ExceptionDetail] => Array
            (
                [HelpLink] => 
                [InnerException] => 
                [Message] => Object reference not set to an instance of an object.
                [StackTrace] =>    at    Absolute.Web.Common.UoW.TransactionService.InTrasaction(Action actionbeBeforeCommit)
    at Castle.DynamicProxy.AbstractInvocation.Proceed()
    at Castle.Proxies.IBookingProxy.GetHotels(BookingGetHotelsMessageRequest request)
    at SyncInvokeGetHotels(Object , Object[] , Object[] )
    at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs)
    at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc)
  at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)
                [Type] => System.NullReferenceException
            )

    )

)

如果有人帮助我,我会很好

【问题讨论】:

    标签: php web-services soap wsdl nusoap


    【解决方案1】:

    使用body发送整个请求xml是错误的,您需要创建以header开头的请求xml,您可以在documentation阅读更多内容。

    // Set your security header namespace
    $headerNS = 'http://docs.oasisopen.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd';
    
    //Create vars for username and password
    $usernameNode = new SoapVar('your username', XSD_STRING, null, null, 'Username', $headerNS);
    $passwordNode = new SoapVar('your password', XSD_STRING, null, null, 'Password', $headerNS);
    
    // Create Username token node and add vars
    $UsernameTokenNode = new SoapVar([$usernameNode, $passwordNode], SOAP_ENC_OBJECT, null, null, 'UsernameToken', $headerNS);
    
    // Create security node
    $securityNode = new SoapVar([$UsernameTokenNode], SOAP_ENC_OBJECT, null, null, 'Security', $headerNS);
    
    // Now create a header with all above data
    $header = [new SoapHeader($headerNS, 'Security', $securityNode, false)];
    
    // Soap client options you choose
    $options = [];
    
    // Create your SoapClient and add header to client
    $client = new SoapClient('Service Wsdl url', $options);
    $client->__setSoapHeaders($header);
    

    现在您可以创建您的肥皂电话正文并提出您的请求。您可以从documentation 阅读更多关于使用属性创建节点的信息。

    使用$client-&gt;__getLastRequest(); 检查您的最后一个验证请求。

    【讨论】:

    • 谢谢。我实际上已经解决了这个问题,发送带有标头的所有 xml 请求没有问题,我的请求语法错误。来自萨尔瓦多的问候
    • 小心,命名空间的域名中缺少一个破折号(-)。正确的命名空间是http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多