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 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
4

1 に答える 1

3

あなたはもうすぐそこにいます、この行:

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

すでにUserCredentialsノードを作成しているため、次の行は次のとおりです。

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

できる/すべき:

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

関数を呼び出す方法により、空の応答が得られます。次の2 つの方法があります。

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

(これでも空の応答が返されますが、これは有効な資格情報がないためだと思います。これには注意が必要です。soaperror/soapfaultが発生すると予想されますが、明らかにこのサービスは不正な資格情報でそれらを生成しません) .

于 2013-06-13T18:19:51.857 に答える