Difference between revisions of "PHP Example"

From Open Rail Data Wiki
Jump to navigation Jump to search
m (categorisation)
m (Redirected to new PHP examples page)
 
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]. 
+
#REDIRECT [[PHP_Examples]]
 
 
==PECL Stomp Package ==
 
 
 
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.
 
 
 
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===
 
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. 
 
 
 
<code>
 
<?php
 
// Network Rail Stomp Handler example by ian13
 
$server = "tcp://datafeeds.networkrail.co.uk:61618";
 
$user = "username";
 
$password = "password";
 
$channel = "TRAIN_MVT_ALL_TOC";
 
&nbsp;
 
$con = new Stomp($server, $user, $password);
 
if (!$con) {
 
    die('Connection failed: ' . stomp_connect_error());
 
}
 
&nbsp;
 
$con->subscribe("/topic/" . $channel);
 
&nbsp;
 
while($con){
 
    if ($con->hasFrame()){
 
        $msg = $con->readFrame();
 
        foreach (json_decode($msg->body) as $event) {
 
          // 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.
 
 
 
<code>
 
// Specify a client-id header in the connection
 
$con = new Stomp($server, $user, $password, array('client-id' => 'somename'));
 
&hellip;
 
// Specify an activemq.subscriptionName header in the subscription
 
$con->subscribe("/topic/" . $channel, array('activemq.subscriptionName' => 'somename'));
 
&hellip;
 
</code>
 
 
 
 
 
==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.
 
 
 
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>
 
  require_once('Stomp.php');
 
 
 
  $con = new Stomp('tcp://datafeeds.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();
 
</code>
 
 
 
[[Category:Network Rail Data Feeds]]
 

Latest revision as of 17:52, 9 July 2013

Redirect to: