{"id":36621940,"url":"https://github.com/blacknell/restapi-service","last_synced_at":"2026-01-12T09:24:46.355Z","repository":{"id":62495002,"uuid":"164605372","full_name":"blacknell/restapi-service","owner":"blacknell","description":"Simple class to expose a REST api","archived":false,"fork":false,"pushed_at":"2024-11-06T18:49:14.000Z","size":115,"stargazers_count":0,"open_issues_count":2,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-06T08:54:32.171Z","etag":null,"topics":["php","rest-api"],"latest_commit_sha":null,"homepage":null,"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/blacknell.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":"2019-01-08T08:49:40.000Z","updated_at":"2021-02-02T13:38:18.000Z","dependencies_parsed_at":"2022-11-02T11:18:10.157Z","dependency_job_id":null,"html_url":"https://github.com/blacknell/restapi-service","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/blacknell/restapi-service","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blacknell%2Frestapi-service","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blacknell%2Frestapi-service/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blacknell%2Frestapi-service/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blacknell%2Frestapi-service/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/blacknell","download_url":"https://codeload.github.com/blacknell/restapi-service/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blacknell%2Frestapi-service/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28337711,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-12T06:09:07.588Z","status":"ssl_error","status_checked_at":"2026-01-12T06:05:18.301Z","response_time":98,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["php","rest-api"],"created_at":"2026-01-12T09:24:45.714Z","updated_at":"2026-01-12T09:24:46.349Z","avatar_url":"https://github.com/blacknell.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# A simple class to expose a RESTful api\n[![Build Status](https://travis-ci.org/blacknell/restapi-service.svg?branch=master)](https://travis-ci.org/blacknell/restapi-service)\n[![Latest Stable Version](https://poser.pugx.org/blacknell/restapi-service/v/stable)](https://packagist.org/packages/blacknell/restapi-service)\n[![Latest Unstable Version](https://poser.pugx.org/blacknell/restapi-service/v/unstable)](https://packagist.org/packages/blacknell/restapi-service)\n[![License](https://poser.pugx.org/blacknell/restapi-service/license)](https://packagist.org/packages/blacknell/restapi-service)\n\n`restapi-service` maps REST API calls to endpoints in protected methods in your derived class.\nIn your class methods process according to the verbs and arguments of the http request.\n\n## Installation\n\nInstall the latest version with\n```\n$ composer require blacknell/restapi-service\n```\n## Basic Usage\n\n* Copy `example/api.php` and derive a class such as in `examples/MyAPI.class.php` into your \nweb server directory\n* Configure a `.htaccess` file to rewrite your RESTful call to your class\n\n## Web Server configuration\n\nFor example, `https://yourserver/myapi/v1/daylight/littlehampton/yesterday` maps to\n `https://yourserver/myapi/v1/api.php?request=daylight/littlehampton/yesterday`\n```\n\u003cIfModule mod_rewrite.c\u003e\n    RewriteEngine On\n    RewriteRule myapi/v1/(.*)$ myapi/v1/api.php?request=$1 [QSA,NC,L]\n\u003c/IfModule\u003e\n```\n\n## Sample code\nSee [example/MyAPI.class.php](https://github.com/blacknell/restapi-service/blob/master/example/MyAPI.class.php)\nto see how `https://yourserver/myapi/v1/daylight/littlehampton/yesterday` generates the following JSON output\n```\n{\n    \"description\": \"Between sunrise and sunset yesterday\",\n    \"sunrise\": {\n        \"date\": \"2019-01-07 08:00:56.000000\",\n        \"timezone_type\": 3,\n        \"timezone\": \"Europe\\/London\"\n    },\n    \"sunset\": {\n        \"date\": \"2019-01-07 16:15:44.000000\",\n        \"timezone_type\": 3,\n        \"timezone\": \"Europe\\/London\"\n    }\n}\n```\n## Additional Concepts\n#### Cross-Origin Resource Sharing (CORS)\nAdditional headers can be added to the constructor of your derived class **before** calling the parent constructor.\nFor example, to allow a client on a website http://myclient.com to access your API add this header call.\n```\n\tpublic function __construct($request, \\Monolog\\Logger $logger = null)\n\t{\n\t\theader('Access-Control-Allow-Origin: http://myclient.com');\n\t\tparent::__construct($request, $logger);\n\t}\n\n```\n#### Authentication\nOveride `RestAPI::isAuthenticated()` to handle authentication and only return `true` if the request is authorised.\nAs a basic example, you could enforce a request to include a header such as `Authentication-Token: xxx` and test this in your derived class.\n```\n\tprotected function isAuthenticated()\n\t{\n\t\t$headers=getallheaders();\n\t\tif($headers['Authentication-Token'] !== 'xxx') {\n\t\t\treturn false;\n\t\t} else {\n\t\t\treturn parent::isAuthenticated();\n\t\t}\n\t}\n```\n\n## Error Handling\nAny endpoint not mapping to a protected function in your derived class results in the following JSON response.\n```\n{\n    \"error\": \"No endpoint\",\n    \"code\": 404\n}\n```\nYour derived class should do the same for invalid verbs or arguments.\nMethods other than GET, POST, PUT or DELETE also result in an error.\n## Logging\nPSR-3 logging is supported via [monolog/monolog](https://github.com/Seldaek/monolog) by passing \nan optional `Logger` object to the API constructor.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fblacknell%2Frestapi-service","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fblacknell%2Frestapi-service","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fblacknell%2Frestapi-service/lists"}