SOAP Client unauthorized (PHP)

1,420 views
Skip to first unread message

Robert Ntim

unread,
Jun 7, 2016, 9:09:35 PM6/7/16
to A gathering place for the Open Rail Data community
Hi Guys,

I have an hackathon project in mind and I was planning to use the national rail way API.

I have wrote a PHP script to connect the SOAP server, but I keep getting back SoapFault Object errors saying that my request is unauthorized.

I did numerous researches and I double checked my private token but still could not manage to get it working, so I have no clues of what is wrong.

Can somebody please look at the code and tell me the mistake? Any help would be greatly appreciated.

<?php

class LDBWS {

       
private $soapClient;
       
private static $URI = "https://lite.realtime.nationalrail.co.uk/OpenLDBWS/wsdl.aspx?ver=2016-02-16";
       
private static $NS = "http://thalesgroup.com/RTTI/2016-02-16/ldb/";
       
private static $TOKEN = "my-token-value";
       
private $header;

       
public function __construct(){

                $params
= array("trace" => true );
                $this
->soapClient = new SoapClient(self::$URI, $params);        
                $headerParams
= array("tns:TokenValue" => self::$TOKEN);
                $soapStruct
= new SoapVar($headerParams, SOAP_ENC_OBJECT);
                $this
->header = new SoapHeader(self::$NS, "AccessToken", $soapStruct, false);
                $this
->soapClient->__setSoapHeaders($this->header);

       
}


       
public function getDepartureBoard($argsArray = array()){
               
return $this->soapClient->__call("GetDepartureBoard", $argsArray);
       
}


}
try{

        $ldbws
= new LDBWS();

        $ldbws
->getDepartureBoard(array("numRows" => "5", "crs" => "WAE"));
}catch(SoapFault $e){
        echo
"<pre>";
        print_r
($e);
        echo
"</pre>";
}

SoapFault Object
(
   
[message:protected] => Unauthorized
   
[string:Exception:private] =>
   
[code:protected] => 0
   
[file:protected] => D:\localServer\apache24\htdocs\www\soap.php
   
[line:protected] => 24
   
[trace:Exception:private] => Array
       
(
           
[0] => Array
               
(
                   
[function] => __doRequest
                   
[class] => SoapClient
                   
[type] => ->
                   
[args] => Array
                       
(
                           
[0] => my-token-value
                           
[1] => https://lite.realtime.nationalrail.co.uk/OpenLDBWS/ldb9.asmx
                           
[2] => http://thalesgroup.com/RTTI/2012-01-13/ldb/GetDepartureBoard
                           
[3] => 1
                           
[4] => 0
                       
)

               
)

           
[1] => Array
               
(
                   
[file] => D:\localServer\apache24\htdocs\www\soap.php
                   
[line] => 24
                   
[function] => __call
                   
[class] => SoapClient
                   
[type] => ->
                   
[args] => Array
                       
(
                           
[0] => GetDepartureBoard
                           
[1] => Array
                               
(
                                   
[numRows] => 5
                                   
[crs] => WAE
                               
)

                       
)

               
)

           
[2] => Array
               
(
                   
[file] => D:\localServer\apache24\htdocs\www\soap.php
                   
[line] => 33
                   
[function] => getDepartureBoard
                   
[class] => LDBWS
                   
[type] => ->
                   
[args] => Array
                       
(
                           
[0] => Array
                               
(
                                   
[numRows] => 5
                                   
[crs] => WAE
                               
)

                       
)

               
)

       
)

   
[previous:Exception:private] =>
   
[faultstring] => Unauthorized
   
[faultcode] => HTTP
)


Matt A

unread,
Jun 8, 2016, 3:23:13 AM6/8/16
to A gathering place for the Open Rail Data community
Hi Robert,

I don't speak php, but at a guess the I suspect the fault lies in incorrectly specifying the namespace for the AccessToken section. This has its own namespace - http://thalesgroup.com/RTTI/2013-11-28/Token/types - seperate to the namespace for the schema version.

So I think you need to get a new namespace set up for the token type then reference it when using it in the header. If you can add the XML produced by your code before sending that'll make diagnosis a lot easier. :)

Cheers,

Matt

Robert Ntim

unread,
Jun 8, 2016, 8:55:39 AM6/8/16
to A gathering place for the Open Rail Data community
Hi Matt,

Thanks for the help. 
I am not really sure how to create and validate my own name space, what I did was to types "http://thalesgroup.com/RTTI/2016-02-16/ldb/myCustomNameSpace" as my namespace.
I was able to produce the XML used for the request and this is what it shows me
<SOAP-ENV:Header>
<ns2:AccessToken>
<ns2:TokenValue>my-token-value</ns2:TokenValue>
</ns2:AccessToken>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns1:GetDepartureBoardRequest/>
<param1>WAE</param1>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
For some reason it does not seem to detect the "numRows" parameter I fed to the function call, but just the "crs" parameter.

Rail Ale Fan

unread,
Jun 8, 2016, 10:45:16 AM6/8/16
to Robert Ntim, A gathering place for the Open Rail Data community
Hi Robert,

Looks like a couple of separate issues - I was able to get your code working by changing as follows;

For $NS, by using:

  private static $NS = "http://thalesgroup.com/RTTI/2010-11-01/ldb/commontypes";

And then using ns2: prefix instead of tns: at line 15:

  $headerParams = array("ns2:TokenValue" => self::$TOKEN);

That solves the authentication issue, and then to make the GetDepartureBoard call, instead of:


  return $this->soapClient->__call("GetDepartureBoard", $argsArray);

...use:

  return $this->soapClient->getDepartureBoard($argsArray);

( __call() is deprecated, php.net recommends using the actual method names of the object where possible )

Hope this helps!
--
http://live-departures.info/rail/





--
You received this message because you are subscribed to the Google Groups "A gathering place for the Open Rail Data community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openraildata-t...@googlegroups.com.
To post to this group, send email to openrail...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Robert Ntim

unread,
Jun 8, 2016, 11:36:45 AM6/8/16
to A gathering place for the Open Rail Data community, robyn...@yahoo.it
Hi Rail Ale Fan,

I do not know how to thank you enough.
Is magically working now, after hours of tries you came up with the solution. I did not realize the __call was deprecated, usually I get warning saying that a given method call is deprecated but in this case I have not got any. I have used the the __soapCall method too, but now I am going with your solution.   

I really appreciated all the help I have got from you all
Reply all
Reply to author
Forward
0 new messages