{"id":28955830,"url":"https://github.com/kodedphp/http","last_synced_at":"2025-06-23T20:10:15.024Z","repository":{"id":52246833,"uuid":"88722540","full_name":"kodedphp/http","owner":"kodedphp","description":"PSR-7 (HTTP message) library.","archived":false,"fork":false,"pushed_at":"2024-03-17T10:13:43.000Z","size":462,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-06T23:43:05.633Z","etag":null,"topics":["http","http-client","http-factory","psr-17","psr-18","psr-7","request","response"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kodedphp.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,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2017-04-19T08:48:58.000Z","updated_at":"2023-03-14T03:35:43.000Z","dependencies_parsed_at":"2024-03-16T17:16:41.685Z","dependency_job_id":"936491b9-d3d7-4078-addd-8e493807ce84","html_url":"https://github.com/kodedphp/http","commit_stats":{"total_commits":250,"total_committers":3,"mean_commits":83.33333333333333,"dds":"0.028000000000000025","last_synced_commit":"469f264aba17d73ef03e43e95d11b96afb5b73bd"},"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"purl":"pkg:github/kodedphp/http","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kodedphp%2Fhttp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kodedphp%2Fhttp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kodedphp%2Fhttp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kodedphp%2Fhttp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kodedphp","download_url":"https://codeload.github.com/kodedphp/http/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kodedphp%2Fhttp/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261548741,"owners_count":23175499,"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":["http","http-client","http-factory","psr-17","psr-18","psr-7","request","response"],"created_at":"2025-06-23T20:10:14.357Z","updated_at":"2025-06-23T20:10:14.998Z","avatar_url":"https://github.com/kodedphp.png","language":"PHP","readme":"Koded - HTTP Library\n====================\n\n[![Latest Stable Version](https://img.shields.io/packagist/v/koded/http.svg)](https://packagist.org/packages/koded/http)\n[![Build Status](https://travis-ci.org/kodedphp/http.svg?branch=master)](https://travis-ci.org/kodedphp/http)\n[![Code Coverage](https://scrutinizer-ci.com/g/kodedphp/http/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/kodedphp/http/?branch=master)\n[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/kodedphp/http/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/kodedphp/http/?branch=master)\n[![Packagist Downloads](https://img.shields.io/packagist/dt/koded/http.svg)](https://packagist.org/packages/koded/http)\n[![Minimum PHP Version](https://img.shields.io/badge/php-%3E%3D%208.0-8892BF.svg)](https://php.net/)\n[![Software license](https://img.shields.io/badge/License-BSD%203--Clause-blue.svg)](LICENSE)\n\n\nKoded HTTP library implements [PSR-7][1] (HTTP message), [PSR-17][2] (HTTP Factories) and [PSR-18][3] (HTTP Client).\n\nTo have more useful everyday methods for your projects, the request and response instances\nare extended with [additional interfaces](#additional-interfaces).\n\n\nServerRequest\n-------------\n\n```php\nclass ServerRequest extends ClientRequest implements Request {}\n```\n\nThis object represents the **incoming server-side** HTTP request.\n\n![](diagrams/server-request.png)\n\n\nClientRequest\n-------------\n\n```php\nclass ClientRequest implements RequestInterface, JsonSerializable {}\n```\n\nThis object is a representation of an **outgoing client-side** HTTP request.\n\n![](diagrams/client-request.png)\n\n\nServerResponse\n--------------\n\n```php\nclass ServerResponse implements Response, JsonSerializable {}\n```\n\nThis object represents the **outgoing server-side** response.\n\n![](diagrams/server-response.png)\n\n\nUploadedFile\n------------\n\nThis value object represents a file uploaded through the HTTP request.\n\n\nHTTP Factory\n------------\n\nImplementation of [PSR-17][2] (HTTP Factories).\n\n```php\n\u003c?php\n\nuse Koded\\Http\\HttpFactory;\n\n$httpFactory = new HttpFactory;\n\n$clientRequest = $httpFactory-\u003ecreateRequest('GET', '/');\n$serverRequest = $httpFactory-\u003ecreateServerRequest('GET', '/');\n\n$response = $httpFactory-\u003ecreateResponse(201);\n\n$stream = $httpFactory-\u003ecreateStream('Hello there');\n$stream = $httpFactory-\u003ecreateStreamFromFile('file.name', '+w');\n$stream = $httpFactory-\u003ecreateStreamFromResource($resource);\n\n$uri = $httpFactory-\u003ecreateUri('/');\n\n$uploadedFile = $httpFactory-\u003ecreateUploadedFile($stream);\n```\n\nHTTP clients\n============\n\nThere are 2 implementations for `ClientRequest` interface\n- PHP stream\n- curl\n\nTo create instances of HTTP clients, use the `Koded\\Http\\Client\\ClientFactory` class\n\n```php\n\u003c?php\n\nuse Koded\\Http\\Client\\ClientFactory;\n\n$http = new ClientFactory(ClientFactory::CURL); // or ClientFactory::PHP\n\n$http-\u003eget('/', $headers);\n$http-\u003epost('/', $body, $headers);\n$http-\u003eput('/', $body, $headers);\n$http-\u003epatch('/', $body, $headers);\n$http-\u003edelete('/', $headers);\n$http-\u003ehead('/', $headers);\n```\n\n`$headers` are optional.\n\nHTTP Client (PSR-18)\n--------------------\n\nImplementation of [PSR-18][3] (HTTP Client).\n\n```php\n\u003c?php\n\nuse Koded\\Http\\Client\\ClientFactory;\nuse Koded\\Http\\ClientRequest;\n\n$request = new ClientRequest('POST', 'https://...', ['foo' =\u003e 'bar']);\n$response = (new ClientFactory)-\u003esendRequest($request);\n```\n\nAdditional interfaces\n=====================\n![Additional interfaces](./diagrams/interfaces.png)\n\n- `Koded\\Http\\Request`\n- `Koded\\Http\\Response`\n\nThese two may be useful in your project as they provide additional \nmethods for request/response objects state.\n\n### Request\n- `getPath(): string`\n- `getBaseUri(): string`\n- `withAttributes(array $attributes): Request`\n- `isSecure(): bool`\n- `isSafeMethod(): bool`\n- `isXHR(): bool`\n\n### Response\n- `getContentType(): string`\n\nExtendedMessageInterface\n------------------------\n\nBoth `Request` and `Response` extends this interface, thus providing the extra methods:\n- `withHeaders(array $headers): static`\n- `replaceHeaders(array $headers): static`\n- `getFlattenedHeaders(): array`\n- `getCanonicalizedHeaders(array $names = []): string`\n\nHttpInputValidator\n------------------\n\nThe idea here is to have a basic mechanism for validating the incoming request data.\nValidation is done in an instance of `HttpInputValidator` object by calling the \n`Request::validate(HttpInputValidator $validator)` method.\n\n`HttpInputValidator::validate()` should return `array`, in case of\n- empty array, the validation went fine\n- a hash (`['key' =\u003e 'value', ...]`), with information what went wrong if incoming data is not valid\n\nThe error handling is done in the application. A naive example:\n\n```php\nclass FormValidator implements HttpInputValidator {\n\n    public function validate(Data $input): array {\n        if (empty($input-\u003eget('username'))) {\n            return ['message' =\u003e 'Username is required'];\n        }\n       return []; \n    }\n}\n\n// Somewhere in your app, use the `Request` object to run validation\n\nif ($response = $request-\u003evalidate(new FormValidator)) {\n    // {\"message\":\"Username is required\",\"status\":400}\n    return $response;\n}\n```\n\nThe error response will always have a status code set (`status` value) in the error message.  \nIf the status code is not provided in the validation, the default is `400 Bad Request`.\n\n\nLicense\n-------\n\nThe code is distributed under the terms of [The 3-Clause BSD license](LICENSE).\n\n\n[1]: https://www.php-fig.org/psr/psr-7/\n[2]: https://www.php-fig.org/psr/psr-17/\n[3]: https://www.php-fig.org/psr/psr-18/\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkodedphp%2Fhttp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkodedphp%2Fhttp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkodedphp%2Fhttp/lists"}