{"id":18840047,"url":"https://github.com/alexsasharegan/http_lib","last_synced_at":"2026-04-24T12:07:33.324Z","repository":{"id":56944775,"uuid":"66344506","full_name":"alexsasharegan/Http_lib","owner":"alexsasharegan","description":"An ultra simple \u0026 lightweight library for making file-based API's easier in PHP.","archived":false,"fork":false,"pushed_at":"2018-03-18T08:00:52.000Z","size":246,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-01T22:06:41.694Z","etag":null,"topics":["callback","json","micro-framework","micro-service","php","rest-api"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alexsasharegan.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-08-23T07:34:10.000Z","updated_at":"2016-09-28T00:21:57.000Z","dependencies_parsed_at":"2022-08-21T02:40:22.293Z","dependency_job_id":null,"html_url":"https://github.com/alexsasharegan/Http_lib","commit_stats":null,"previous_names":[],"tags_count":33,"template":false,"template_full_name":null,"purl":"pkg:github/alexsasharegan/Http_lib","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexsasharegan%2FHttp_lib","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexsasharegan%2FHttp_lib/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexsasharegan%2FHttp_lib/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexsasharegan%2FHttp_lib/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alexsasharegan","download_url":"https://codeload.github.com/alexsasharegan/Http_lib/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexsasharegan%2FHttp_lib/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32222542,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-24T10:26:35.452Z","status":"ssl_error","status_checked_at":"2026-04-24T10:25:27.643Z","response_time":64,"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":["callback","json","micro-framework","micro-service","php","rest-api"],"created_at":"2024-11-08T02:44:49.439Z","updated_at":"2026-04-24T12:07:33.296Z","avatar_url":"https://github.com/alexsasharegan.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Http\n[![Latest Stable Version](https://poser.pugx.org/alexsasharegan/http/v/stable)](https://packagist.org/packages/alexsasharegan/http)\n[![Total Downloads](https://poser.pugx.org/alexsasharegan/http/downloads)](https://packagist.org/packages/alexsasharegan/http)\n[![Latest Unstable Version](https://poser.pugx.org/alexsasharegan/http/v/unstable)](https://packagist.org/packages/alexsasharegan/http)\n[![License](https://poser.pugx.org/alexsasharegan/http/license)](https://packagist.org/packages/alexsasharegan/http)\n\nA lightweight, dependency free library that makes writing file-based RESTful JSON API endpoints easier in PHP.\n\n## Setup\n\nClone the repo into your project. Assuming your restful endpoints live in an `/api` directory, I would recommend either making an `/api/vendor` folder or just a plain `/api/libs` folder and cloning this repo inside there.\n\n- In your project, `require_once` the path to the `Http_Autoloader.php`.\n\n```php\n\u003c?php\n\nrequire_once 'path/to/Http_Autoloader.php';\n\n# ...or potentially...\n\nrequire_once 'project_root/dist/api/vendor/Http_lib/Http_Autoloader.php';\n```\n\n## Instantiation\n\n```php\n\u003c?php\n\n$http = new Http;\n```\n\n## Properties\n\nProperties for class `Http` represented in json:\n\n```json\n{\n  \"request\": \"type: class Http\\Request\",\n  \"response\": \"type: class Http\\Response\",\n  \"get\": \"type: callable (callback handle)\",\n  \"post\": \"type: callable (callback handle)\",\n  \"put\": \"type: callable (callback handle)\",\n  \"patch\": \"type: callable (callback handle)\",\n  \"delete\": \"type: callable (callback handle)\"\n}\n```\n\nSome example properties for the class `Http\\Request` represented in json (they vary based on the request itself):\n\n```json\n{\n  \"body\": {\n    \"content\": \"this is some test content from a json request\"\n  },\n  \"method\": \"POST\",\n  \"requestURI\": \"/php/my_libs_tests/Http.test.php/1?stuff=some+stuff\",\n  \"query\": {\n    \"stuff\": \"some stuff\"\n  },\n  \"file\": \"Http.test.php\",\n  \"contentType\": \"application/json\",\n  \"cookies\": \"PHPSESSID=01kf9mqndmpr8guqe6tk87nka7; _ga=GA1.1.162483231.1471457216\",\n  \"host\": \"localhost\",\n  \"port\": \"80\",\n  \"pathInfo\": \"/1\",\n  \"scriptName\": \"/php/my_libs_tests/Http.test.php\",\n  \"URIComponents\": {\n    \"path\": \"/php/my_libs_tests/Http.test.php/1\",\n    \"query\": \"stuff=some+stuff\"\n  },\n  \"userAgent\": \"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36\"\n}\n```\n\nProperties for `Http\\Reponse` are private. This allows the response object to manage the response data and serialize it on send.\n\n## Methods\n\nThe `Http` class has a method available for each major HTTP verb _(get, post, put, patch, delete)_. These allow you to attach your callbacks to be run on each appropriate request method. You can pass in the string name of your callback, or write your function inline as a closure. The callback will be called with the instance of `Http`.\n\n#### Callback Reference\n\n```php\n\u003c?php  \n\nfunction myPostCallback($http) {\n  # code ...\n}\n\n$http = new Http;\n$http-\u003epost('myPostCallback');\n```\n\n#### Inline Closure\n\n```php\n\u003c?php  \n\n$http = new Http;\n$http-\u003epost(\n  function ($http) {\n    # code ...\n  }\n);\n\n# also possible\n$myGlobalVar = [1,2,3];\n(new Http)\n  -\u003eget(\n    function ($http) {\n      # code ...\n    }\n  )\n  -\u003epost(\n    function ($http, $myGlobalVar) {\n      # code ...\n    }\n    , $myGlobalVar\n  )\n  -\u003eexec();\n```\n\nTo get values off the parsed request body, call `Http\\Request::get( string $key )`.\n\nWhen writing your callbacks, you can build up your response with two methods:\n\n  - `Http\\Response::set( string $key, mixed $value )`\n\n    ##### Parameters\n    * **key:** the name for the value you wish to set\n    * **value:** the value you wish to set\n\n  - `Http\\Response::set_array( array $array )`\n\n    ##### Parameters\n    * **array:** an associative array of values to set on the response\n\nThe last line in your callback will be a call to `Http::send`. This exits execution completely after sending the response.\n\n  - `Http::send( [ int $statusCode = 200, string $contentType = \"application/json\", string $content = '' ] )`\n\n    ##### Parameters\n    * **statusCode:** a valid HTTP status code to return\n    * **contentType:** a valid MIME Type to set the response header\n    * **content:** if you set Content-Type to something other than json, you can send your custom data with this parameter. No serialization will be performed on this content.\n    * **_Note:_** any undefined routes will return a status code `405` with a json formatted error message\n    ```json\n      {\n        \"error\": \"No route has been defined for this request method.\"\n      }\n    ```\n\nIf you use a `try {} catch(Exception $e) {}` block in your error handling, you can call `Http::handleError( Exception $e )` in your catch block, and it will automatically reply with a `500` code and a json payload containing the error.\n\nOnce you have defined all your necessary HTTP method callbacks, you can let your instance of `Http` run the appropriate callback by simply calling:\n\n```php\n\u003c?php\n\n$http-\u003eexec();\n```\n\n## Examples\n\n```php\n\u003c?php\n# this example uses the Database and Html library as well\nrequire_once 'path/to/vendor/autoload.php';\n\n# alias our classes for cleanliness\nuse Database\\MySQL;\nuse Http\\Http;\nuse Html\\Html;\n\n# the callbacks for each http method\n# get called with the instance of Http\\Http\nfunction get($http) {\n  # instantiate our MySQL object with a connection config\n  $db = new MySQL([\n    'hostName'     =\u003e '1.1.1.1',\n    'databaseName' =\u003e 'myDatabase',\n    'dbUserName'   =\u003e 'admin',\n    'dbPassword'   =\u003e 'adminPass',\n  ]);\n\n  # make a select query and pull $id from the request query string\n  $db-\u003equery(\n    \"SELECT * FROM `sellers` WHERE `id` = {$http-\u003erequest-\u003equery('id')}\"\n  )\n  # this func gets called once for each row\n  # 'use' pulls in $http from the closure's parent scope\n  # sometimes we need to pass by reference like this: use( \u0026$var )\n  -\u003eiterateResult(\n    function ( $row ) use ( $http ){\n      $http-\u003eresponse-\u003eset_array($row);\n    }\n  );\n  # nesting operations to dumb the column names into response['sellers']\n  $http-\u003eresponse-\u003eset( 'sellersColumns', $db-\u003egetColumns('sellers') );\n  # test setting different types\n  $http-\u003eresponse-\u003eset( 'test', [\n    'one' =\u003e 1,\n    'two' =\u003e 'two',\n    'three' =\u003e true,\n    'four' =\u003e [1,2,3],\n  ]);\n  # send what's in our response object\n  $http-\u003esend();\n}\n\n# make our instance of Http\\Http\n$http = new Http;\n# chain our calls together\n$http\n  -\u003eget( 'get' )\n  -\u003epost(\n    function ( $http ) {\n      # code ...\n    }\n  )\n  # there is a default exception handler,\n  # but you can set a custom exception handler\n  # like this:\n  -\u003eerror(\n    function ( Exception $e ) use ( $http ) {\n      $http-\u003esend(500, 'text/html', new Html('code', $e));\n    }\n  )\n  # execute the route\n  -\u003eexec();\n```\n\n```php\n\u003c?php\n\nrequire_once 'path/to/vendor/autoload.php';\nuse Http\\Http;\nuse Http\\Response;\n\nHttp::redirect('/index.html');\n# this sends the location header \u0026 exits execution!\n# the redirect location defaults to '/'\n\n$current_http_status = Http::status();\n# calling it without any arguments gets the current status code\n\n$prev_http_status = Http::status(404);\n# setting a new status will set the response code and return the old status\n\nHttp::status(Response::HTTP_NOT_FOUND);\n# there are a number of constants available for valid status codes\n# while it can be verbose, it can add readability to your code\n# here is the fully namespaced list:\nHttp\\Response::HTTP_CONTINUE = 100;\nHttp\\Response::HTTP_SWITCHING_PROTOCOLS = 101;\nHttp\\Response::HTTP_PROCESSING = 102;            // RFC2518\nHttp\\Response::HTTP_OK = 200;\nHttp\\Response::HTTP_CREATED = 201;\nHttp\\Response::HTTP_ACCEPTED = 202;\nHttp\\Response::HTTP_NON_AUTHORITATIVE_INFORMATION = 203;\nHttp\\Response::HTTP_NO_CONTENT = 204;\nHttp\\Response::HTTP_RESET_CONTENT = 205;\nHttp\\Response::HTTP_PARTIAL_CONTENT = 206;\nHttp\\Response::HTTP_MULTI_STATUS = 207;          // RFC4918\nHttp\\Response::HTTP_ALREADY_REPORTED = 208;      // RFC5842\nHttp\\Response::HTTP_IM_USED = 226;               // RFC3229\nHttp\\Response::HTTP_MULTIPLE_CHOICES = 300;\nHttp\\Response::HTTP_MOVED_PERMANENTLY = 301;\nHttp\\Response::HTTP_FOUND = 302;\nHttp\\Response::HTTP_SEE_OTHER = 303;\nHttp\\Response::HTTP_NOT_MODIFIED = 304;\nHttp\\Response::HTTP_USE_PROXY = 305;\nHttp\\Response::HTTP_RESERVED = 306;\nHttp\\Response::HTTP_TEMPORARY_REDIRECT = 307;\nHttp\\Response::HTTP_PERMANENTLY_REDIRECT = 308;  // RFC7238\nHttp\\Response::HTTP_BAD_REQUEST = 400;\nHttp\\Response::HTTP_UNAUTHORIZED = 401;\nHttp\\Response::HTTP_PAYMENT_REQUIRED = 402;\nHttp\\Response::HTTP_FORBIDDEN = 403;\nHttp\\Response::HTTP_NOT_FOUND = 404;\nHttp\\Response::HTTP_METHOD_NOT_ALLOWED = 405;\nHttp\\Response::HTTP_NOT_ACCEPTABLE = 406;\nHttp\\Response::HTTP_PROXY_AUTHENTICATION_REQUIRED = 407;\nHttp\\Response::HTTP_REQUEST_TIMEOUT = 408;\nHttp\\Response::HTTP_CONFLICT = 409;\nHttp\\Response::HTTP_GONE = 410;\nHttp\\Response::HTTP_LENGTH_REQUIRED = 411;\nHttp\\Response::HTTP_PRECONDITION_FAILED = 412;\nHttp\\Response::HTTP_REQUEST_ENTITY_TOO_LARGE = 413;\nHttp\\Response::HTTP_REQUEST_URI_TOO_LONG = 414;\nHttp\\Response::HTTP_UNSUPPORTED_MEDIA_TYPE = 415;\nHttp\\Response::HTTP_REQUESTED_RANGE_NOT_SATISFIABLE = 416;\nHttp\\Response::HTTP_EXPECTATION_FAILED = 417;\nHttp\\Response::HTTP_I_AM_A_TEAPOT = 418;                                               // RFC2324\nHttp\\Response::HTTP_MISDIRECTED_REQUEST = 421;                                         // RFC7540\nHttp\\Response::HTTP_UNPROCESSABLE_ENTITY = 422;                                        // RFC4918\nHttp\\Response::HTTP_LOCKED = 423;                                                      // RFC4918\nHttp\\Response::HTTP_FAILED_DEPENDENCY = 424;                                           // RFC4918\nHttp\\Response::HTTP_RESERVED_FOR_WEBDAV_ADVANCED_COLLECTIONS_EXPIRED_PROPOSAL = 425;   // RFC2817\nHttp\\Response::HTTP_UPGRADE_REQUIRED = 426;                                            // RFC2817\nHttp\\Response::HTTP_PRECONDITION_REQUIRED = 428;                                       // RFC6585\nHttp\\Response::HTTP_TOO_MANY_REQUESTS = 429;                                           // RFC6585\nHttp\\Response::HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE = 431;                             // RFC6585\nHttp\\Response::HTTP_UNAVAILABLE_FOR_LEGAL_REASONS = 451;\nHttp\\Response::HTTP_INTERNAL_SERVER_ERROR = 500;\nHttp\\Response::HTTP_NOT_IMPLEMENTED = 501;\nHttp\\Response::HTTP_BAD_GATEWAY = 502;\nHttp\\Response::HTTP_SERVICE_UNAVAILABLE = 503;\nHttp\\Response::HTTP_GATEWAY_TIMEOUT = 504;\nHttp\\Response::HTTP_VERSION_NOT_SUPPORTED = 505;\nHttp\\Response::HTTP_VARIANT_ALSO_NEGOTIATES_EXPERIMENTAL = 506;                        // RFC2295\nHttp\\Response::HTTP_INSUFFICIENT_STORAGE = 507;                                        // RFC4918\nHttp\\Response::HTTP_LOOP_DETECTED = 508;                                               // RFC5842\nHttp\\Response::HTTP_NOT_EXTENDED = 510;                                                // RFC2774\nHttp\\Response::HTTP_NETWORK_AUTHENTICATION_REQUIRED = 511;\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexsasharegan%2Fhttp_lib","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falexsasharegan%2Fhttp_lib","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexsasharegan%2Fhttp_lib/lists"}