HAR, JSON Schema and PHP

One of the many projects I’m working on that are starting to weave together into something cohesive is a PHP wrapper for the BrowserMob Proxy. What is interesting with that from an automation perspective is inspecting the HTTP response codes when loading a page; WebDriver will not grow this feature so you need to get them via a proxy. That information is returned to the calling script a HAR (HTTP ARchive) which is a specifically formatted JSON file.

As with when dealing with any file format, the first thing when parsing it is to make sure that it is in the right format. For XML we use DTDs or XML Schemas and for JSON we use JSON Schemas.

Determining that we want to do that is a lot easier than actually doing it. Especially in PHP. As usual, there are a handful of projects, all with a different level of compatibility/completeness with with the JSON Schema RFC, missing license information and not packaged for easy inclusion in projects. Ah. The joys of PHP.

One thing I didn’t want to do was write a schema from scratch; that is just no fun at all. So I looked to Jari Bakken’s HAR project for help. There he has schema for HAR files in JavaScript and in raw JSON. Awesome. I can recycle the latter one.


Remember I mentioned the varying degree of completeness in PHP validation projects? Ya. The use of $ref is the area they all seem to be lacking. Which meant a couple hours of copy-paste and debugging to remove those and make one huge schema file the results of which can be seen here.

Armed with a working schema I decided to use the php-json-schema project to validate my har file.

   * @expectedException \Json\ValidationException
   public function testBadHar() {
     $validator = new \Json\Validator(dirname(__FILE__) . '/../src/har_schema.json');


What I like about this particular schema project is that you load the schema once and then validate any number of time with it. Of course, it uses a different license than the one I use for my stuff and is distributed by composer and not pear, but those can be worked around. In the meantime, I hope the schema is useful to people.

Post a Comment

Your email is never published nor shared. Required fields are marked *