PHP code example of tomphp / hal-client

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

    

tomphp / hal-client example snippets




use TomPHP\HalClient\Client;

$recipes = Client::create()->get('http://api.demo-cocktails.com/recipes');

echo "There are currently " . $recipes->count->getValue() . " cocktails" . PHP_EOL;

$cocktail = $recipes[0]->getLink('self')->get();
// or
$cocktail = $recipes->findMatching(['name' => 'Mojito'])[0]->getLink('self')->get();

echo $cocktail->name->getValue() . " has a " . $cocktail->rating->getValue() . " start rating." .PHP_EOL;



use TomPHP\HalClient\Client;

$resource = Client::create();

// Methods for resources
$resource->getField('field_name'); // Specifically access field named 'field_name'
$resource->getLink('link_name'); // Specifically access link named 'link_name'
$resource->getResouce('resource_name'); // Specifically access resource named 'resource_name'

$resource->field_name; // Alias for $resource->getField('field_name');

// Methods for fields
$field->getValue(); // Return the value contained in the field
$field->person->name->getValue(); // Access a sub field by name

// Methods for links
$link->get(); // Makes a get request to the link's href and returns the resource

// Methods for collections
$coll[5]; // Access element 5 in a collection
$coll->findMatching(['age' => 20]); // Return a collection with all maps in the collection which a field called 'age' which is set to 20.