PHPackages                             brandonlamb/php-hal - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. brandonlamb/php-hal

ActiveLibrary[API Development](/categories/api)

brandonlamb/php-hal
===================

REST HAL library as a C-extension for PHP

v1.0.2(11y ago)41813MITCPHP &gt;=5.4.0

Since Jul 20Pushed 11y ago1 watchersCompare

[ Source](https://github.com/brandonlamb/php-hal)[ Packagist](https://packagist.org/packages/brandonlamb/php-hal)[ RSS](/packages/brandonlamb-php-hal/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (3)Dependencies (2)Versions (4)Used By (0)

php-hal
=======

[](#php-hal)

PHP library for representing HAL resources for REST API

[![Build Status](https://camo.githubusercontent.com/3ecf6478d92a5499a695bac90477dbd53845d84e87397e579c9ee89b655ee648/68747470733a2f2f7472617669732d63692e6f72672f6272616e646f6e6c616d622f7068702d68616c2e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/brandonlamb/php-hal)[![Latest Stable Version](https://camo.githubusercontent.com/83edbb71cb30705ad9b71ea6eb4395cc3e7a2f89de089649e95a65f9c9891037/68747470733a2f2f706f7365722e707567782e6f72672f6272616e646f6e6c616d622f7068702d68616c2f762f737461626c652e706e67)](https://packagist.org/packages/brandonlamb/php-hal)[![Total Downloads](https://camo.githubusercontent.com/b425cf0b0adfc6f50c38e5ed04f1ad47f4b49043a681b1334c2214d2e8871a2b/68747470733a2f2f706f7365722e707567782e6f72672f6272616e646f6e6c616d622f7068702d68616c2f646f776e6c6f6164732e706e67)](https://packagist.org/packages/brandonlamb/php-hal)

PhpHal [![Build Status](https://camo.githubusercontent.com/f448e7644a911cf66983c49238e821451274cee72f2fdced2fe7f853e5e3af59/68747470733a2f2f7472617669732d63692e6f72672f6c6576656c337068702f7265736f757263652e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/level3php/resource)
============================================================================================================================================================================================================================================================================================

[](#phphal-)

PhpHal is a library for representing and consuming resources in different [hypermedia](http://en.wikipedia.org/wiki/Hypermedia)formats.

A resource in a \[HATEOAS API\] () must describe its own capabilities and interconnections, which is the third level of [Three Levels of the REST Maturity Model](http://www.infoq.com/news/2010/03/RESTLevels)

### Why Hypermedia?

[](#why-hypermedia)

As you can read in the prologue of [Designing Hypermedia APIs](http://www.designinghypermediaapis.com/) book:

> Hypermedia APIs embrace the principles that make the web great: flexibility, standardization, and loose coupling to any given service. They take into account the principles of systems design [enumerated by Roy Fielding in his thesis](http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm), but with a little less sytems theory jargon.

> Hypermedia designs scale better, are more easily changed and promote decoupling and encapsulation, with all the benefits those things bring. On the downside, it is not necessarily the most latency-tolerant design, and caches can get stale if you're not careful. It may not be as efficient on an individual request level as other designs.

> \-- [Steve Klabnik](http://www.steveklabnik.com/)

### Which Hypermedia specification should I use?

[](#which-hypermedia-specification-should-i-use)

Hypermedia is being defined these days. Only the best APIs implement Hypermedia. Currently there is no de facto standard, so you must choose between the differents specifications.

PhpHal currenly implements or is planned to implement these specifications:

- [HAL](http://stateless.co/hal_specification.html): This is the most common and active. It has a JSON and a XML version.
- [Siren](https://github.com/kevinswiber/siren): Currently being defined. It implements some useful things like actions, classes, etc.
- [Collection+JSON](http://amundsen.com/media-types/collection/): This is fully designed to be a CRUD oriented API.

Requirements
------------

[](#requirements)

- PHP 5.4.x

Installation
------------

[](#installation)

The recommended way to install PhpHal is [through composer](http://getcomposer.org). You can see [the package information on Packagist.](https://packagist.org/packages/brandonlamb/php-hal)

```
{
    "require": {
        "brandonlamb/php-hal": "dev-master"
    }
}
```

Examples
--------

[](#examples)

### Writer

[](#writer)

#### Basic Resource with Link as `application/hal+json`

[](#basic-resource-with-link-as-applicationhaljson)

```
use PhpHal\Link;
use PhpHal\Resource;
use PhpHal\Format\Writer\Hal;

$resource = new Resource();
$resource->setURI('/foo');
$resource->setLink('foo', new Link('/bar'));
$resource->setData([
    'foo' => 'bar',
    'baz' => 'qux'
]);

$writer = new Hal\JsonWriter(true);
echo $writer->execute($resource);
```

```
{
    "foo": "bar",
    "baz": "qux",
    "_links": {
        "self": {
            "href": "/foo"
        },
        "foo": {
            "href": "/bar"
        }
    }
}
```

#### Resource with embedded resources as `aapplication/vnd.siren+json`

[](#resource-with-embedded-resources-as-aapplicationvndsirenjson)

```
use PhpHal\Link;
use PhpHal\Resource;
use PhpHal\Format\Writer\Siren;

$resource = new Resource();
$resource->setRepositoryKey('index');
$resource->setURI('/index?page=2');
$resource->setLink('prev', new Link('/index?page=1'));
$resource->setLink('next', new Link('/index?page=3'));
$resource->addData('count', 5);

$subresource = [];
foreach (range(1, 5) as $value) {
    $subresource = new Resource();
    $subresource->addData('value', $value);

    $subresources[] = $subresource;
}

$resource->addResources('subresources', $subresources);

$writer = new Siren\JsonWriter(true);
echo $writer->execute($resource);
```

```
{
    "class": [
        "index"
    ],
    "properties": {
        "count": 5
    },
    "entities": [
        {
            "rel": "subresources",
            "class": [
                "index",
                "subresources"
            ],
            "properties": {
                "value": 1
            }
        },
        ...
        {
            "rel": "subresources",
            "class": [
                "index",
                "subresources"
            ],
            "properties": {
                "value": 5
            }
        }
    ],
    "links": [
        {
            "rel": "self",
            "href": "/index?page=2"
        },
        {
            "rel": "prev",
            "href": "/index?page=1"
        },
        {
            "rel": "next",
            "href": "/index?page=3"
        }
    ]
}
```

#### Resource with linked resource as `application/hal+xml`

[](#resource-with-linked-resource-as-applicationhalxml)

```
use PhpHal\Link;
use PhpHal\Resource;
use PhpHal\Format\Writer\Hal;

$author = new Resource();
$author->setURI('/john-doe');
$author->setTitle('John Doe');

$article = new Resource();
$article->setURI('/lorem-ipsum');
$article->addData('description', 'Lorem ipsum dolor sit amet ...');
$article->linkResource('author', $author);

$writer = new Hal\XmlWriter(true);
echo $writer->execute($article);
```

```

  Lorem ipsum dolor sit amet ...

```

### Reader

[](#reader)

#### Basic Resource with Link as `application/hal+json`

[](#basic-resource-with-link-as-applicationhaljson-1)

```
use PhpHal\Format\Reader\Hal;

$json = '{"foo":"bar","baz":"qux","_links":{"self":{"href":"/foo"},"foo":{"href":"/bar"}}}';

$reader = new Hal\JsonReader();
$resource = $reader->execute($json);
print_r($resource);
```

```
PhpHal\Resource Object
(
    [uri:protected] => /foo
    [links:protected] => Array
        (
            [foo] => PhpHal\Link Object
                (
                    [href:protected] => /bar
                )

        )

    [data:protected] => Array
        (
            [foo] => bar
            [baz] => qux
        )

)
```

Tests
-----

[](#tests)

Tests are in the `tests` folder. To run them, you need PHPUnit. Example:

```
$ phpunit --configuration phpunit.xml.dist

```

License
-------

[](#license)

MIT, see [LICENSE](LICENSE)

###  Health Score

29

—

LowBetter than 57% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~65 days

Total

3

Last Release

4234d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/34cbf13be4b33a8e9a67a2bac1edd118301c30fb86df4f91797055abe0180e3b?d=identicon)[brandonlamb](/maintainers/brandonlamb)

---

Top Contributors

[![brandonlamb](https://avatars.githubusercontent.com/u/89181?v=4)](https://github.com/brandonlamb "brandonlamb (19 commits)")

---

Tags

extension

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/brandonlamb-php-hal/health.svg)

```
[![Health](https://phpackages.com/badges/brandonlamb-php-hal/health.svg)](https://phpackages.com/packages/brandonlamb-php-hal)
```

###  Alternatives

[lspbupt/yii2-dingtalk

dingtalk API for yii2 framework

2420.5k](/packages/lspbupt-yii2-dingtalk)[apexwire/yii2-restclient

Tools to use API as ActiveRecord for Yii2

143.5k](/packages/apexwire-yii2-restclient)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
