2013-06-12 81 views
2
// Based on http://stackoverflow.com/questions/3934639/soap-authentication-with-php 
include 'auth.php'; 
$soapURL = "http://traindata.njtransit.com:8090/NJTTrainData.asmx?WSDL" ; 
$soapParameters = array('UserCredentials'=>array('userName' => $username, 'password' => $password)); 

$soapFunction = "getTrainScheduleJSON" ; 
$soapFunctionParameters = Array('station' => "NY") ; 

$soapClient = new SoapClient($soapURL, $soapParameters); 

$soapResult = $soapClient->__soapCall($soapFunction, $soapFunctionParameters); 

這裏是輸出(下面)。它抱怨缺少必需的標題「UserCredentials」,但我有UserCredentials包括在內。或者我錯過了什麼?謝謝!PHP/SOAP:UserCredentials如何傳遞給SOAP請求?

PHP Fatal error: Uncaught SoapFault exception: [soap:MustUnderstand] System.Web.Services.Protocols.SoapHeaderException: Missing required header 'UserCredentials'. 
    at System.Web.Services.Protocols.SoapServerProtocol.ReadParameters() 
    at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest() in /home/www/html/njtransit/wed.php:13 
Stack trace: 
#0 /home/www/html/njtransit/wed.php(13): SoapClient->__soapCall('getTrainSchedul...', Array) 
#1 {main} 
    thrown in /home/www/html/njtransit/wed.php on line 13 

這是另一個在__setSoapHeaders之前添加SoapHeader的嘗試。我還添加了命名空間($ NS):

include 'auth.php'; 
$soapURL = "http://traindata.njtransit.com:8090/NJTTrainData.asmx?WSDL" ; 
$ns = 'http://microsoft.com/webservices/'; //Namespace of the WS. 
$soapParameters = array('UserCredentials'=>array('userName' => $username, 'password' => $password)); 

$soapFunction = "getTrainScheduleJSON" ; 
$soapFunctionParameters = Array('station' => "NY") ; 

// $soapClient = new SoapClient($soapURL, $soapParameters); 
$soapClient = new SoapClient($soapURL); 

$header = new SoapHeader($ns,'UserCredentials',$soapParameters,false); 

$soapClient->__setSoapHeaders($header); 
var_dump($soapClient); 
$soapResult = $soapClient->__soapCall($soapFunction, $soapFunctionParameters); 

這裏是產出和新的錯誤消息,「對象引用不設置到對象的實例。」

object(SoapClient)#1 (3) { 
    ["_soap_version"]=> 
    int(1) 
    ["sdl"]=> 
    resource(5) of type (Unknown) 
    ["__default_headers"]=> 
    array(1) { 
    [0]=> 
    object(SoapHeader)#2 (4) { 
     ["namespace"]=> 
     string(33) "http://microsoft.com/webservices/" 
     ["name"]=> 
     string(15) "UserCredentials" 
     ["data"]=> 
     array(1) { 
     ["UserCredentials"]=> 
     array(2) { 
      ["userName"]=> 
      string(10) "myusername" 
      ["password"]=> 
      string(17) "mypassword" 
     } 
     } 
     ["mustUnderstand"]=> 
     bool(false) 
    } 
    } 
} 
PHP Fatal error: Uncaught SoapFault exception: [soap:Server] System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.NullReferenceException: Object reference not set to an instance of an object. 
    at DepartureVisionServiceXMLExternal.ServiceExternal.Login(String username, String password) in C:\Users\vcaicxz\Desktop\CRYSTAL\PentaDepartureVisionServiceXMLExternaL\NJTTrainData.asmx.cs:line 55 
    at DepartureVisionServiceXMLExternal.ServiceExternal.getTrainScheduleJSON(String station) in C:\Users\vcaicxz\Desktop\CRYSTAL\PentaDepartureVisionServiceXMLExternaL\NJTTrainData.asmx.cs:line 141 
    --- End of inner exception stack trace --- in /home/www/html/njtransit/wed2.php:19 
Stack trace: 
#0 /home/www/html/njtransit/wed2.php(19): SoapClient->__soapCall('getTrainSchedul...', Array) 
#1 {main} 
    thrown in /home/www/html/njtransit/wed2.php on line 19 
+0

「SoapClient」的第二個參數涉及選項,而不是soapheaders。 [看看__setSoapHeaders()](http://www.php.net/manual/en/soapclient.setsoapheaders.php) – Wrikken

+0

@Wrikken所以我需要進行多個調用? – Edward

+0

不,你需要更多的1個功能/方法。這只是一個soapcall .... – Wrikken

回答

3

就快成功了,這一行:

$header = new SoapHeader($ns,'UserCredentials',$soapParameters,false); 

已經創建UserCredentials節點,所以這行:

$soapParameters = array('UserCredentials'=>array('userName' => $username, 'password' => $password)); 

能/應該是:

$soapParameters = array('userName' => $username, 'password' => $password); 

這給你一個反應,因爲你的方法調用函數,則有兩種可能的方式:

//1. add an extra array around params 
$soapClient->__soapCall($soapFunction, array($soapFunctionParameters)); 
//2. or call like this: 
$soapClient->$soapFunction($soapFunctionParameters); 

(這仍然給我一個空的迴應,但我認爲那是因爲我沒有有效憑據,這是一個要注意的事情:我會期望 soaperror/soapfault,但顯然這個服務不會產生壞證書他們)。

+0

我試過了,它工作!這是soapParameters中的附加UserCredentials,這是阻止它允許正確登錄的主要問題。這對我來說似乎很奇怪,因爲我確定我看到很多SOAP登錄的例子都是這樣做的。再次感謝您抽出寶貴時間查看並提供可行的解決方案,並在第二天按照您的承諾解決問題。 – Edward

+0

另外: $ data_ny = $ soapClient-> getTrainScheduleJSON(array('station'=> $ station)); 做到了。但我喜歡你做$ soapClient的方式 - > $ soapFunction($ soapFunctionParameters);並會改變它。 – Edward