ChartBlocks goes in to beta

A project I’ve been working on for some time now has hit public beta. It is called “ChartBlocks” and allows you to design and publish charts online in a number of formats, with analytics and sharing built right in.

This will be the first public embed of a chart from the platform:

If you’re looking to create vector charts, which you can download and publish, or share with the internet (through Facebook, twitter, an embed on your website) sign up for the beta here.

Parsing CIF files to get train schedule data in PHP

I’ve had some fun looking at what data you can get via the National Rail Open Data scheme, and was really impressed by the ActiveMQ implementation they’ve got for Real Time Train Movement Messages!

The messages National Rail send allow you to plot trains to stations, or even along route, but the message only contains station IDs or train IDs – which is kinda boring. I like to visualise the data, on a map for example.

To get the station name and location for a particular train message one has to access an entirely different database, from another provider. Along comes ATOC with their CIF files, which look very scary compared to ActiveMQ.

To cut a long story as short as I can, the CIF files contain a lot of information (400MB+ files), using string length and new lines to split up the data. You can sign up to download the files on the ATOC website, and the specification for the files is available here.

Why the blog post? Well, I needed a way to parse these files to populate a Mongo database and wanted to promote the PHP CIF parser I’ve started work on: https://github.com/rb-cohen/php-cif-parser

 

PHP Kent – The first meetup

Thursday 18th April 2013 at the LOL Bar in Maidstone.

It’s been a long time coming (the idea started in 2010), but my business partner and I have finally got around to organising a local PHP user group in Kent (UK).

I attend PHP London frequently, but am hoping to meet local developers and share ideas outside of that. Not able to get enough of PHP user groups, I plan to attend both on a monthly basis! :)

For more information on the meetup, like when the next event is scheduled, and to RSVP – please head over to the meetup page.

Getting the raw POST or PUT data of a request in ZF2 MVC

If you’re sending raw data to ZF2 over http (perhaps a json document), you can use the “getContent()” method of Zend\Http\PhpEnvironment\Request.

How does this work in practice?

In a controller you might want to do something like this:

1
2
3
4
5
6
7
8
9
<?php
namespace Application\Controller;</code>
 
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
 
class IndexController extends AbstractActionController {
    public function indexAction() {
        $data = $this->getRequest()->getContent();

But if you’re always expecting a certain format, say JSON, a controller plugin could give you back the array or object rather than a lot of json_decode calls in your controllers.

The plugin:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
 
namespace My\Mvc\Controller\Plugin;
 
use Zend\Mvc\Controller\Plugin\Params as ZendParams;
 
class Params extends ZendParams {
 
    public function fromJson() {
        $body = $this->getController()->getRequest()->getContent();
        if (!empty($body)) {
            $json = json_decode($body, true);
            if (!empty($json)) {
                return $json;
            }
        }
 
        return false;
    }
 
}

And then its as simple as:

1
2
public function indexAction() {
    $data = $this->params()->fromJson();

Help! Sometimes getContent() doesn’t return my data.

The method uses file_get_contents(‘php://input’) to get the raw data from the HTTP request. Unfortuantely, the php://input stream can only be read once per process. I did have a plug-in that called file_get_contents(‘php://input’) too, and if $request->getContent() gets called after this, it will be empty!