tomphp / hal-client
此包已被废弃且不再维护。未建议替代包。
HAL API导航客户端库
v0.1.1
2015-04-12 23:48 UTC
Requires
- php: >=5.5
- beberlei/assert: ~2.3
- guzzlehttp/guzzle: ~5.2
- phly/http: 0.11.*
- phpunit/phpunit: *
- psr/http-message: 0.9.*
Requires (Dev)
- behat/behat: ~3.0
- phpspec/phpspec: 2.*@dev
- squizlabs/php_codesniffer: ~2.3
This package is not auto-updated.
Last update: 2023-11-11 12:45:17 UTC
README
HAL API导航客户端库。
安装
该库目前处于早期开发阶段,许多内容都可能发生变化。
$ composer require tomphp/hal-client
示例
给定如下API
GET http://api.demo-cocktails.com/recipes
{ "_links": { "self": { "href": "http://api.demo-cocktails.com/recipes" } }, "count": 3, "_embedded": { "recipes": [ { "_links": { "self": { "href": "http://api.demo-cocktails.com/recipes/1" } }, "name": "Mojito" }, { "_links": { "self": { "href": "http://api.demo-cocktails.com/recipes/2" } }, "name": "Pina Colada" }, { "_links": { "self": { "href": "http://api.demo-cocktails.com/recipes/3" } }, "name": "Daquiri" } ] } }
GET http://api.demo-cocktails.com/recipes/1
{ "_links": { "self": { "href": "http://api.demo-cocktails.com/recipes/1" } }, "name": "Mojito", "rating": 5, "ingredients": [ {"name": "White Rum"}, {"name": "Soda"}, {"name": "Lime Juice"}, {"name": "Sugar"}, {"name": "Mint Leaves"} ] }
<?php 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;
方法
<?php 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.
限制
当前库只能向HAL+JSON API发送GET请求。
计划功能
- 更好的错误报告
- 用于遍历树的迭代器
- HAL+XML处理器
- 原始JSON处理器
- 原始XML处理器
- 其他用于更新的HTTP方法
- CURIES
贡献
请务必!
注意:需要PSR-2规范和测试。