PHP SOAP国家铁路咨询

时间:2013-08-30 11:52:10

标签: php xml web-services soap

修改

我收到了未经授权的错误。事实证明这是因为我提供了一个不正确的令牌。我现在有一个工作令牌,所以不再未经授权。但是我仍然收到错误。现在它说' SOAP-ERROR:编码:对象没有' numRows'属性'

如何创建GetDepartureBoardRequest的numRows和crs子节点?

由于


我试图访问国家铁路公司的实时离职...

https://staging.livedepartureboards.co.uk/ldbws/

我有一个访问令牌。

我在生成客户端xml和进行身份验证方面遇到了问题。

根据(非常有限的)文档,我的肥皂信封应该是这样的......

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:com="http://thalesgroup.com/RTTI/2010-11-01/ldb/commontypes" xmlns:typ="http://thalesgroup.com/RTTI/[put your version here]/ldb/types">
   <soapenv:Header>
      <com:AccessToken>
         <com:TokenValue>XXXXXXXXXXXXXXXXXXXXXXXXXX</com:TokenValue>
      </com:AccessToken>
   </soapenv:Header>
   <soapenv:Body>
      <typ:GetDepartureBoardRequest>
         <typ:numRows>10</typ:numRows>
         <typ:crs>MAN</typ:crs>
      </typ:GetDepartureBoardRequest>
   </soapenv:Body>
</soapenv:Envelope>
我现在看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://thalesgroup.com/RTTI/2012-01-13/ldb/types" xmlns:ns2="http://thalesgroup.com/RTTI/2010-11-01/ldb/commontypes">
    <SOAP-ENV:Header>
        <ns2:AccessToken>
            <ns2:TokenValue>XXXXXXXXXXXXXXXXX</ns2:TokenValue>
        </ns2:AccessToken>
    </SOAP-ENV:Header>
    <SOAP-ENV:Body>
        <ns1:GetDepartureBoardRequest/>
        <ns1:crs>MAN</ns1:crs>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

这是我用来生成它的PHP:

<?php
$token = 'XXXXXXXXXXXXXXXXXXXXXXXXX';

$client = new SoapClient("https://staging.livedepartureboards.co.uk/ldbws/wsdl.aspx",
                   array('trace' => TRUE));

$headerParams = array('ns2:TokenValue'    => $token);

$soapStruct = new SoapVar($headerParams, SOAP_ENC_OBJECT);
$header = new SoapHeader('http://thalesgroup.com/RTTI/2010-11-01/ldb/commontypes', 'AccessToken', $soapStruct, false);
$client->__setSoapHeaders($header);

try {

    $args =  array(
               new SoapParam('10','ns1:numRows'),
               new SoapParam('WIL','ns1:crs')
        );


  $response = $client->__call('GetDepartureBoard',$args);

}
  catch(Exception $e){
      print_r($e);
      }
      echo "REQUEST:\n" . $client->__getLastRequest() . "\n";
?>

最后,这是我收到的SOAP Fault Object:

SoapFault Object
(
    [message:protected] => SOAP-ERROR: Encoding: object hasn't 'numRows' property
    [string:Exception:private] => 
    [code:protected] => 0
    [file:protected] => /var/www/vhosts/MYSITE.co.uk/httpdocs/live-departures/index.php
    [line:protected] => 22
    [trace:Exception:private] => Array
        (
            [0] => Array
                (
                    [function] => __call
                    [class] => SoapClient
                    [type] => ->
                    [args] => Array
                        (
                            [0] => GetDepartureBoard
                            [1] => Array
                                (
                                    [0] => Array
                                        (
                                            [0] => SoapParam Object
                                                (
                                                    [param_name] => ns1:numRows
                                                    [param_data] => 10
                                                )

                                            [1] => SoapParam Object
                                                (
                                                    [param_name] => ns1:crs
                                                    [param_data] => MAN
                                                )

                                        )

                                )

                        )

                )

            [1] => Array
                (
                    [file] => /var/www/vhosts/MYSITE.co.uk/httpdocs/live-departures/index.php
                    [line] => 22
                    [function] => GetDepartureBoard
                    [class] => SoapClient
                    [type] => ->
                    [args] => Array
                        (
                            [0] => Array
                                (
                                    [0] => SoapParam Object
                                        (
                                            [param_name] => ns1:numRows
                                            [param_data] => 10
                                        )

                                    [1] => SoapParam Object
                                        (
                                            [param_name] => ns1:crs
                                            [param_data] => MAN
                                        )

                                )

                        )

                )

        )

    [previous:Exception:private] => 
    [faultstring] => SOAP-ERROR: Encoding: object hasn't 'numRows' property
    [faultcode] => Client
    [faultcodens] => http://schemas.xmlsoap.org/soap/envelope/
)

这让我发疯,我似乎无法理解正在发生的事情。我已经阅读了所有PHP SOAP文档,并将其搜索到了突破点。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

是的!感谢hakre指出不正确的令牌。

原来另一个问题是由于我如何设置args并调用GetDepartureBoard。

而不是这样做:

    $args =  array(
               new SoapParam('10','ns1:numRows'),
               new SoapParam('WIL','ns1:crs')
        );


  $response = $client->__call('GetDepartureBoard',$args);

我应该这样做:

    $args =  array(
              'numRows'=>'10',
               'crs'=>'MAN'
        );

  $response = $client->GetDepartureBoard($args);

立即行动:)