gpt4 book ai didi

PHP/SOAP : How can UserCredentials be passed to SOAP request?

转载 作者:行者123 更新时间:2023-12-04 04:53:39 28 4
gpt4 key购买 nike

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

最佳答案

你快到了,这一行:

$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,但显然该服务不会在错误凭据上生成它们) .

关于PHP/SOAP : How can UserCredentials be passed to SOAP request?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17065464/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com