PHP code example of zircote / hal

1. Go to this page and download the library: Download zircote/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/ */

    

zircote / hal example snippets



use Hal\Resource,
    Hal\Link;
/* Create a new Resource Parent */
$parent = new Resource('/dogs');
/* Add any relevent links */
$parent->setLink(new Link('/dogs?q={text}', 'search'));
$dogs[1] =  new Resource('/dogs/1');
$dogs[1]->setData(
    array(
        'id' => '1', 
        'name' => 'tiber', 
        'color' => 'black'
    )
);
$dogs[2] =  new Resource(
    '/dogs/2',array(
        'id' => '2', 
        'name' => 'sally', 
        'color' => 'white'
    )
);
$dogs[3] =  new Resource(
    '/dogs/3',array(
        'id' => '3', 
        'name' => 'fido', 
        'color' => 'gray'
    )
);
/* Add the embedded resources */
foreach ($dogs as $dog) {
    $parent->setEmbedded('dog', $dog);
}
echo (string) $parent;


echo $parent->getXML()->asXML();