{"id":16894299,"url":"https://github.com/jsor/hal-client","last_synced_at":"2025-06-29T05:38:43.176Z","repository":{"id":45297066,"uuid":"43953628","full_name":"jsor/hal-client","owner":"jsor","description":"A lightweight client for consuming and manipulating Hypertext Application Language (HAL) resources.","archived":false,"fork":false,"pushed_at":"2021-12-23T09:37:45.000Z","size":118,"stargazers_count":22,"open_issues_count":0,"forks_count":11,"subscribers_count":6,"default_branch":"main","last_synced_at":"2024-10-14T17:18:12.152Z","etag":null,"topics":["api","client","hal","http","hypertext-application-language","php"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jsor.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-10-09T12:19:01.000Z","updated_at":"2024-03-21T18:34:51.000Z","dependencies_parsed_at":"2022-09-14T05:21:30.829Z","dependency_job_id":null,"html_url":"https://github.com/jsor/hal-client","commit_stats":null,"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsor%2Fhal-client","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsor%2Fhal-client/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsor%2Fhal-client/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsor%2Fhal-client/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jsor","download_url":"https://codeload.github.com/jsor/hal-client/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221826238,"owners_count":16887119,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","client","hal","http","hypertext-application-language","php"],"created_at":"2024-10-13T17:18:17.128Z","updated_at":"2024-10-28T12:30:06.454Z","avatar_url":"https://github.com/jsor.png","language":"PHP","readme":"HalClient\n=========\n\nA lightweight PHP client for consuming and manipulating\n[Hypertext Application Language (HAL)](https://tools.ietf.org/html/draft-kelly-json-hal)\nresources.\n\n[![Build Status](https://github.com/jsor/hal-client/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/jsor/hal-client/actions/workflows/ci.yml)\n[![Coverage Status](https://coveralls.io/repos/jsor/hal-client/badge.svg?branch=main\u0026service=github)](https://coveralls.io/github/jsor/hal-client?branch=main)\n\n* [Installation](#installation)\n* [Usage](#usage)\n* [License](#license)\n\nInstallation\n------------\n\nInstall the latest version with [Composer](http://getcomposer.org).\n\n```bash\ncomposer require jsor/hal-client\n```\n\nCheck the [Packagist page](https://packagist.org/packages/jsor/hal-client) for\nall available versions.\n\n### HTTP Client dependency\n\nThe Hal client requires a [HttpClientInterface](src/HttpClient/HttpClientInterface.php)\nimplementation which can handle [PSR-7](http://www.php-fig.org/psr/psr-7/)\nrequests and responses.\n\nTo use the default implementations shipped with this library, you need to\ninstall Guzzle 7, 6 or 5.\n\n```bash\ncomposer require guzzlehttp/guzzle:\"^5.0||^6.0||^7.0\"\n```\n\nUsage\n-----\n\nWe will use [Propilex](http://propilex.herokuapp.com) as an example API\nendpoint.\n\n### Create the client\n\nAt a first step, we setup a `HalClient` instance.\n\n```php\nuse Jsor\\HalClient\\HalClient;\n\n$client = new HalClient('http://propilex.herokuapp.com');\n```\n\nWe can now set additional headers (eg. an Authorization header) which are sent\nwith every request.\n\n```php\n$client = $client-\u003ewithHeader('Authorization', 'Bearer 12345');\n```\n\nNote, that a client instance is [immutable](https://en.wikipedia.org/wiki/Immutable_object),\nwhich means, any call to change the state of the instance returns a **new**\ninstance leaving the original instance unchanged.\n\n```php\n// Wrong!\n$client-\u003ewithHeader('Authorization', '...');\n$resource = $client-\u003eget('/protected');\n\n// Correct!\n$client = $client-\u003ewithHeader('Authorization', '...');\n$resource = $client-\u003eget('/protected');\n```\n\n### Browse the API\n\nTo start browsing through the API, we first get the root resource.\n\n```php\n/** @var \\Jsor\\HalClient\\HalResource $rootResource */\n$rootResource = $client-\u003eroot();\n```\n\nWe now follow the `p:documents` link.\n\n\n```php\n/** @var \\Jsor\\HalClient\\HalLink $documentsLink */\n$documentsLink = $rootResource-\u003egetFirstLink('documents');\n\n$documentsResource = $documentsLink-\u003eget();\n\n$totalDocuments = $documentsResource-\u003egetProperty('total');\n\nforeach ($resource-\u003egetResource('documents') as $document) {\n    echo $document-\u003egetProperty('title') . PHP_EOL;\n}\n```\n\nIf there is a second page with more documents, we can follow the `next` link.\n\n```php\nif ($documentsResource-\u003ehasLink('next')) {\n    $nextDocumentsResource = $documentsResource-\u003egetFirstLink('next')-\u003eget();\n}\n```\n\nOk, let's create a new document.\n\n```php\n$newDocument = $documentsResource-\u003epost([\n    'body' =\u003e [\n        'title' =\u003e 'Sampl document',\n        'body'  =\u003e 'Lorem ipsum'\n    ]\n]);\n```\n\nOh noes! A typo in the document title. Let's fix it.\n\n```php\n$changedDocument = $newDocument-\u003eput([\n    'body' =\u003e [\n        'title' =\u003e 'Sampe document',\n        'body'  =\u003e $newDocument-\u003egetProperty('body')\n    ]\n]);\n```\n\nDamn, we give up.\n\n```php\n$changedDocument-\u003edelete();\n```\n\nLicense\n-------\n\nCopyright (c) 2015-2021 Jan Sorgalla.\nReleased under the [MIT License](LICENSE).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjsor%2Fhal-client","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjsor%2Fhal-client","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjsor%2Fhal-client/lists"}