2016-12-15 58 views
0

我試圖使用nusoap客戶端,並與正常SoapClient調用GetHotels功能和調用函數時,我有這個問題,它返回以下錯誤:如何使PHP頭部安全性調用PHP SOAP?

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 
      ) 

    ) 

) 

如果有人可以幫助我,我會非常好看

回答

0

發送整個請求XML與身體是錯誤的,你需要創建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創建具有attiributes的節點。

使用$client->__getLastRequest();檢查您的上次請求進行驗證。

+0

謝謝。我已經解決了這個問題,沒有問題發送所有的XML請求與頭,我在請求中的語法不好。來自薩爾瓦多的問候 –