【问题标题】:PHP SOAP request to consume WCF with header and XML bodyPHP SOAP 请求使用带有标头和 XML 正文的 WCF
【发布时间】:2014-06-16 05:49:49
【问题描述】:

请求 XML

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://schemas.navitaire.com/WebServices/ISessionManager/Logon</Action>
<h:ContractVersion xmlns:h="http://schemas.navitaire.com/WebServices">330</h:ContractVersion>
</s:Header>

<s:Body>
<LogonRequest xmlns="http://schemas.navitaire.com/WebServices/ServiceContracts/SessionService">
  <logonRequestData xmlns:d4p1="http://schemas.navitaire.com/WebServices/DataContracts/Session" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <d4p1:DomainCode>WWW</d4p1:DomainCode>
    <d4p1:AgentName>API****</d4p1:AgentName>
    <d4p1:Password>********</d4p1:Password>
    <d4p1:LocationCode i:nil="true" />
    <d4p1:RoleCode>APIB</d4p1:RoleCode>
    <d4p1:TerminalInfo i:nil="true" />
  </logonRequestData>
</LogonRequest>
</s:Body>
</s:Envelope>

WSDL 和其他模式只能从特定 IP 访问,所以我将内容发布在 http://pastie.org/9263788

LogonRequest 元素可在此处找到 paste.org/9290170

以下是我尝试过的代码

$wsdl = 'https://trtestr3xapi.navitaire.com/sessionmanager.svc?wsdl';

$client     = new SoapClient($wsdl, array("trace" => 1, "exception" => 0)); 

$header = new SoapHeader('http://schemas.navitaire.com/WebServices','ContractVersion','330', '1');
$client->__setSoapHeaders($header);

$ns_d4p1 = 'http://schemas.navitaire.com/WebServices/DataContracts/Session';
$ns_i = 'http://www.w3.org/2001/XMLSchema-instance';

$logonResquestData = new SimpleXMLElement("<logonRequestData xmlns:i='$ns_i' xmlns:d4p1='$ns_d4p1' />");

$logonResquestData->addChild('d4p1:DomainCode', 'WWW', $ns_d4p1);
$logonResquestData->addChild('d4p1:AgentName', 'AgentName', $ns_d4p1);
$logonResquestData->addChild('d4p1:Password', 'Pass****', $ns_d4p1);
$logonResquestData->addChild('d4p1:LocationCode', null, $ns_d4p1)->addAttribute('i:nil', 'true', $ns_i);
$logonResquestData->addChild('d4p1:RoleCode', 'APIB', $ns_d4p1);
$logonResquestData->addChild('d4p1:TerminalInfo', null, $ns_d4p1)->addAttribute('i:nil', 'true', $ns_i);


$logon_request = $client->Logon($logonResquestData);

return $logon_request;

以上代码抛出如下错误

Fatal error:  Uncaught SoapFault exception: [a:InternalServiceFault]   LogonRequest.AgentName:LengthStringAttribute: The value of LogonRequest.AgentName:LengthStringAttribute is empty, but is not optional. in C:\Inetpub\vhosts\domain.com\tiger\x.php:27
Stack trace:
#0 C:\Inetpub\vhosts\domain.com\tiger\x.php(27): SoapClient-&gt;__call('Logon', Array)
#1 C:\Inetpub\vhosts\domain.com\tiger\x.php(27): SoapClient-&gt;Logon(Object(SimpleXMLElement))
#2 {main}

27 行的 C:\Inetpub\vhosts\domain.com\tiger\x.php 中抛出

我是否以正确的方法和正确的架构发送请求?我哪里错了?

【问题讨论】:

    标签: php xml web-services wcf soap


    【解决方案1】:

    您不需要创建XML,使用SoapClient时,您的登录请求数据应该是这样的:

    $logonResquestData = array(
        'DomainCode' => 'WWW',
        'AgentName' => '****',
        'Password' => '*****',
        'RoleCode' => 'abcabc',
    
    )
    

    【讨论】:

    • 如果我按照您的建议更改登录请求数据,我会收到错误 Fatal error: Uncaught SoapFault exception: [a:InternalServiceFault] Object reference not set to an instance of an object.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-24
    相关资源
    最近更新 更多