PHP Examples: Difference between revisions

From Open Rail Data Wiki
m Added categories
EvelynSnow (talk | contribs)
datafeeds.networkrail.co.uk→publicdatafeeds.networkrail.co.uk
 
(8 intermediate revisions by 2 users not shown)
Line 1: Line 1:
There are two ways to access the Network Rail data feeds from PHP: the [http://pecl.php.net/package/stomp PECL Stomp package] or the [http://stomp.fusesource.org/index.html Stomp library]. 
There are two PHP libraries which can be used to access the data feeds:


==PECL Stomp Package ==
* [http://pecl.php.net/package/stomp PECL Stomp package]
* [http://stomp.fusesource.org/index.html Stomp library]
 
== PECL Stomp ==


The PECL Stomp Package can be installed through the PECL command line tool; this is part of the PHP PEAR extensions package.  Instructions for installing PEAR and PECL on different systems can be found by searching the web.  Once PECL is installed, type <code>pecl install stomp</code> at the command line.
The PECL Stomp Package can be installed through the PECL command line tool; this is part of the PHP PEAR extensions package.  Instructions for installing PEAR and PECL on different systems can be found by searching the web.  Once PECL is installed, type <code>pecl install stomp</code> at the command line.
Line 7: Line 10:
Documentation for the PECL Stomp package can be found on the [http://php.net/manual/en/book.stomp.php PHP:Stomp Manual page].
Documentation for the PECL Stomp package can be found on the [http://php.net/manual/en/book.stomp.php PHP:Stomp Manual page].


===Basic Example===
=== Basic Network Rail Example ===
This example connects to the Network Rail data feed via STOMP, subscribes to the <code>TRAIN_MVT_ALL_TOC</code> topic, and sets up a loop listening for new frames.
This example connects to the Network Rail data feed via STOMP, subscribes to the <code>TRAIN_MVT_ALL_TOC</code> topic, and sets up a loop listening for new frames. Unless the connection is lost, it would need to be killed from the command line. The example for National Rail data includes a brief method of limiting the frames.


<code>
<code>
  <?php
  <?php
  // Network Rail Stomp Handler example by ian13
  // Network Rail Stomp Handler example by ian13
  $server = "tcp://datafeeds.networkrail.co.uk:61618";
  $server = "tcp://publicdatafeeds.networkrail.co.uk:61618";
  $user = "username";
  $user = "username";
  $password = "password";
  $password = "password";
Line 39: Line 42:
</code>
</code>


===Durable Subscriptions===
=== Basic National Rail Push Port Example ===
This example connects to the National Rail data feed via STOMP, subscribes to the <code>GIVEN_QUEUE</code> topic, and sets up a loop listening for new frames. It also is limited on the loop for a specific number of frames, which is particularly useful when testing. This can be changed using the number variable. If you want to look at the output, in the 'do stuff area', you can simply add in "print_r($event);". The function gzdecode is also only available from PHP 5.4.
 
<code>
<?php
// National Rail Push Port Stomp Handler example by Joni Ellenby
$server = "tcp://publicdatafeeds.nationalrail.co.uk:61613";
$user = "GIVEN_USER";
$password = "GIVEN_PASS";
$queue = "GIVEN_QUEUE";
&nbsp;
$number = '20' ;
$i = '0' ;
&nbsp;
$con = new Stomp($server, $user, $password);
if (!$con) {
    die('Connection failed: ' . stomp_connect_error());
}
&nbsp;
$con->subscribe("/queue/" . $queue);
&nbsp;
while($con && $i < $number){
    if ($con->hasFrame()){
        $msg = $con->readFrame();
        $event = gzdecode($msg->body)
        $i++ ;
          // do stuff with $event here
        $con->ack($msg);
    }
}
&nbsp;
die('Connection lost: ' . time());
?>
</code>
 
=== Durable Subscriptions ===


The example above will only receive messages while it is connected to the STOMP server.  By specifying a <code>client-id</code> header in the connection, and an <code>activemq.subscriptionName</code> header in the subscription, messages that were sent while the client was offline will be received upon connection.
The example above will only receive messages while it is connected to the STOMP server.  By specifying a <code>client-id</code> header in the connection, and an <code>activemq.subscriptionName</code> header in the subscription, messages that were sent while the client was offline will be received upon connection.


<code>
<code> // Specify a client-id header in the connection
// Specify a client-id header in the connection
  $con = new Stomp($server, $user, $password, array('client-id' => 'somename'));
  $con = new Stomp($server, $user, $password, array('client-id' => 'somename'));
  &hellip;
  &hellip;
  // Specify an activemq.subscriptionName header in the subscription
  // Specify an activemq.subscriptionName header in the subscription
  $con->subscribe("/topic/" . $channel, array('activemq.subscriptionName' => 'somename'));
  $con->subscribe("/topic/" . $channel, array('activemq.subscriptionName' => 'somename'));
  &hellip;
  &hellip;</code>
</code>


 
== Stomp library ==
==Stomp library==


An alternative Stomp client is available from [http://stomp.fusesource.org/download.html Fuse Source]. It has some minor issues, such as injecting spaces in the username/password, and sleeping on message retrieval.
An alternative Stomp client is available from [http://stomp.fusesource.org/download.html Fuse Source]. It has some minor issues, such as injecting spaces in the username/password, and sleeping on message retrieval.
Line 59: Line 94:
A fixed version can be obtained from [http://mail-archives.apache.org/mod_mbox/activemq-users/201103.mbox/%3CAANLkTinetbR6w-CFzJ+q-Xk3p1xkkr3BzYBcxDAU8VPO@mail.gmail.com%3E here] or [http://mail-archives.apache.org/mod_mbox/activemq-users/201103.mbox/raw/%3CAANLkTinetbR6w-CFzJ+q-Xk3p1xkkr3BzYBcxDAU8VPO@mail.gmail.com%3E here].
A fixed version can be obtained from [http://mail-archives.apache.org/mod_mbox/activemq-users/201103.mbox/%3CAANLkTinetbR6w-CFzJ+q-Xk3p1xkkr3BzYBcxDAU8VPO@mail.gmail.com%3E here] or [http://mail-archives.apache.org/mod_mbox/activemq-users/201103.mbox/raw/%3CAANLkTinetbR6w-CFzJ+q-Xk3p1xkkr3BzYBcxDAU8VPO@mail.gmail.com%3E here].


<code>
<code>require_once('Stomp.php');
  require_once('Stomp.php');
    
    
  $con = new Stomp('tcp://datafeeds.networkrail.co.uk:61618/');
$con = new Stomp('tcp://publicdatafeeds.networkrail.co.uk:61618/');
  $con->clientId = 'someclient';
$con->clientId = 'someclient';
  $con->connect($username, $password);
$con->connect($username, $password);
    
    
  $con->subscribe("/topic/TRAIN_MVT_ALL_TOC");
$con->subscribe("/topic/TRAIN_MVT_ALL_TOC");
  $msg = $con->readFrame();
$msg = $con->readFrame();
    
    
  if ($msg != null) {
if ($msg != null) {
    foreach (json_decode($msg->body) as $event) {
  foreach (json_decode($msg->body) as $event) {
      // do stuff with $event here
    // do stuff with $event here
    }
    $con->ack(msg);
   }
   }
  $con->ack(msg);
}
    
    
  $con->disconnect();
$con->disconnect();</code>
</code>


{{Navtable-DataFeeds}}
{{Navtable-DataFeeds}}

Latest revision as of 11:56, 25 February 2023

There are two PHP libraries which can be used to access the data feeds:

PECL Stomp

The PECL Stomp Package can be installed through the PECL command line tool; this is part of the PHP PEAR extensions package. Instructions for installing PEAR and PECL on different systems can be found by searching the web. Once PECL is installed, type pecl install stomp at the command line.

Documentation for the PECL Stomp package can be found on the PHP:Stomp Manual page.

Basic Network Rail Example

This example connects to the Network Rail data feed via STOMP, subscribes to the TRAIN_MVT_ALL_TOC topic, and sets up a loop listening for new frames. Unless the connection is lost, it would need to be killed from the command line. The example for National Rail data includes a brief method of limiting the frames.

<?php
// Network Rail Stomp Handler example by ian13
$server = "tcp://publicdatafeeds.networkrail.co.uk:61618";
$user = "username";
$password = "password";
$channel = "TRAIN_MVT_ALL_TOC";
 
$con = new Stomp($server, $user, $password);
if (!$con) {
   die('Connection failed: ' . stomp_connect_error());
}
 
$con->subscribe("/topic/" . $channel);
 
while($con){
   if ($con->hasFrame()){
       $msg = $con->readFrame();
       foreach (json_decode($msg->body) as $event) {
         // do stuff with $event here
       }
       $con->ack($msg);
   }
}
 
die('Connection lost: ' . time());
?>

Basic National Rail Push Port Example

This example connects to the National Rail data feed via STOMP, subscribes to the GIVEN_QUEUE topic, and sets up a loop listening for new frames. It also is limited on the loop for a specific number of frames, which is particularly useful when testing. This can be changed using the number variable. If you want to look at the output, in the 'do stuff area', you can simply add in "print_r($event);". The function gzdecode is also only available from PHP 5.4.

<?php
// National Rail Push Port Stomp Handler example by Joni Ellenby
$server = "tcp://publicdatafeeds.nationalrail.co.uk:61613";
$user = "GIVEN_USER";
$password = "GIVEN_PASS";
$queue = "GIVEN_QUEUE";
 
$number = '20' ;
$i = '0' ;
 
$con = new Stomp($server, $user, $password);
if (!$con) {
   die('Connection failed: ' . stomp_connect_error());
}
 
$con->subscribe("/queue/" . $queue);
 
while($con && $i < $number){
   if ($con->hasFrame()){
       $msg = $con->readFrame();
       $event = gzdecode($msg->body)
       $i++ ;
         // do stuff with $event here
       $con->ack($msg);
   }
}
 
die('Connection lost: ' . time());
?>

Durable Subscriptions

The example above will only receive messages while it is connected to the STOMP server. By specifying a client-id header in the connection, and an activemq.subscriptionName header in the subscription, messages that were sent while the client was offline will be received upon connection.

// Specify a client-id header in the connection

$con = new Stomp($server, $user, $password, array('client-id' => 'somename'));
…
// Specify an activemq.subscriptionName header in the subscription
$con->subscribe("/topic/" . $channel, array('activemq.subscriptionName' => 'somename'));
…

Stomp library

An alternative Stomp client is available from Fuse Source. It has some minor issues, such as injecting spaces in the username/password, and sleeping on message retrieval.

A fixed version can be obtained from here or here.

require_once('Stomp.php');

$con = new Stomp('tcp://publicdatafeeds.networkrail.co.uk:61618/'); $con->clientId = 'someclient'; $con->connect($username, $password);

$con->subscribe("/topic/TRAIN_MVT_ALL_TOC"); $msg = $con->readFrame();

if ($msg != null) {

 foreach (json_decode($msg->body) as $event) {
   // do stuff with $event here
 }
 $con->ack(msg);

}

$con->disconnect();


Network Rail Open Data Feeds
Data Feeds About the Feeds Account States Durable Subscriptions Example Code ( PHP / C# / Java / Ruby / Node.js) • Advanced UsesFAQ Release Notes
RTPPM RTPPM Feed
Train Movements Train Movements Feed Train Activation Train Cancellation Train Movement Train Reinstatement Change of Origin Change of Identity Change of Location TSPEED Field Planned Cancellations Cancellation Codes
TD TD Feed C-Class Messages S-Class Messages Train Describers TD Berths
VSTP VSTP Feed
TSR TSR Feed Route Codes
SCHEDULE SCHEDULE Feed TIPLOC Records Schedule and Location Records Association Records CIF Codes How Scheduling Works Allowances
Reference Data Reference Data Feed TOC Codes CIF Codes Delay Attribution Codes Identifying Locations (STANOX, TIPLOC, NLC and 3-Alpha Codes) STANOX Geographical Areas Train Planning data (BPLAN)