PHP Example: Difference between revisions
PeterHicks (talk | contribs) |
Expanded details of PECL Stomp library and usage examples |
||
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]. | |||
==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> | <code> | ||
Line 29: | Line 36: | ||
</code> | </code> | ||
Optionally, you can specify a client-id header, which will ensure message persistence (if enabled on the server) and queue messages whilst your subscriber is offline. | ===Durable Subscriptions=== | ||
The example above will only receive messages while it is connected to the STOMP server. Optionally, you can specify a client-id header, which will ensure message persistence (if enabled on the server) and queue messages whilst your subscriber is offline. | |||
<code> | <code> | ||
Line 37: | Line 46: | ||
Further documentation on the PHP Stomp library is available at [http://www.php.net/manual/en/class.stomp.php http://www.php.net/manual/en/class.stomp.php] | Further documentation on the PHP Stomp library is available at [http://www.php.net/manual/en/class.stomp.php http://www.php.net/manual/en/class.stomp.php] | ||
==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. |
Revision as of 15:55, 28 April 2013
There are two ways to access the Network Rail data feeds from PHP: the PECL Stomp package or the Stomp library.
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 pecl install stomp
at the command line.
Documentation for the PECL Stomp package can be found on the PHP:Stomp Manual page.
Basic 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.
<?php
// Network Rail Stomp Handler example by ian13
$server = "tcp://datafeeds.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());
?>
Durable Subscriptions
The example above will only receive messages while it is connected to the STOMP server. Optionally, you can specify a client-id header, which will ensure message persistence (if enabled on the server) and queue messages whilst your subscriber is offline.
$con = new Stomp($server, $user, $password, array('client-id' => 'somename');
Further documentation on the PHP Stomp library is available at http://www.php.net/manual/en/class.stomp.php
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://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();