PHP code example of aeq / hal

1. Go to this page and download the library: Download aeq/hal library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

aeq / hal example snippets


$posts = $resource->getLink('posts')->follow();

$explorer = new Explorer();
$explorer->setClientAdapter(new GuzzleClientAdapter(new Client()));
$explorer->setSerializer(new JsonSerializer());

$response = $explorer->request('GET', '/my/api/');

$resource = $explorer->explore($response);

$posts = $resource->getLink('posts')->follow();
foreach($posts as $post) {
    $publisher = $post->getLink('publisher')->follow();
    $company = $publisher->getLink('company')->follow();
}

$posts = $resource->getEmbedded('posts');
foreach($posts as $post) {
    $publisher = $post->getEmbedded('publisher');
    $company = $publisher->getEmbedded('company');
}

use Aeq\Hal\Client\ClientAdapterInterface;

class MyCustomClientAdapter implements ClientAdapterInterface
{
    public function request($method, $uri = null, array $options = [])
    {
        // call your custom client and return the response
    }
}


$explorer->setClientAdapter(new MyCustomClientAdapter());

use Aeq\Hal\Serializer\SerializerInterface;

class MyCustomSerializer implements SerializerInterface
{
    public function serialize($data)
    {
        // serialize your data: $data
    }

    public function deserialize($str)
    {
        // deserialize the string: $str
    }
}

$explorer->setSerializer(new MyCustomSerializer());

$explorer->setEventManager(new EventManager());

class MyHandler
{
    public function handle(EventInterface $event)
    {
        // process your stuff
    }
}

$explorer->listenOnEvent(PostClientRequestEvent::class, new MyHandler());