{"id":37007555,"url":"https://github.com/chroma-x/php-basic-http-client","last_synced_at":"2026-01-14T00:49:05.994Z","repository":{"id":56952613,"uuid":"57138794","full_name":"chroma-x/php-basic-http-client","owner":"chroma-x","description":"A basic yet extensible HTTP client library providing different authentication methods written in PHP.","archived":false,"fork":false,"pushed_at":"2024-08-02T09:26:31.000Z","size":112,"stargazers_count":1,"open_issues_count":1,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-11-23T12:13:51.268Z","etag":null,"topics":["basic-authentication","client-certificate","composer-package","extensible","http-client","https-client","php-library","ssl-client-certificate"],"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/chroma-x.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","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,"publiccode":null,"codemeta":null}},"created_at":"2016-04-26T15:15:49.000Z","updated_at":"2024-08-02T09:26:19.000Z","dependencies_parsed_at":"2024-08-01T17:10:36.757Z","dependency_job_id":null,"html_url":"https://github.com/chroma-x/php-basic-http-client","commit_stats":{"total_commits":62,"total_committers":2,"mean_commits":31.0,"dds":"0.016129032258064502","last_synced_commit":"d0bdb88d8d80cc5bf6af1ae7d7eba367cae19619"},"previous_names":[],"tags_count":27,"template":false,"template_full_name":null,"purl":"pkg:github/chroma-x/php-basic-http-client","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chroma-x%2Fphp-basic-http-client","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chroma-x%2Fphp-basic-http-client/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chroma-x%2Fphp-basic-http-client/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chroma-x%2Fphp-basic-http-client/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chroma-x","download_url":"https://codeload.github.com/chroma-x/php-basic-http-client/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chroma-x%2Fphp-basic-http-client/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28406643,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-14T00:40:43.272Z","status":"ssl_error","status_checked_at":"2026-01-14T00:40:42.636Z","response_time":56,"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":["basic-authentication","client-certificate","composer-package","extensible","http-client","https-client","php-library","ssl-client-certificate"],"created_at":"2026-01-14T00:49:05.256Z","updated_at":"2026-01-14T00:49:05.964Z","avatar_url":"https://github.com/chroma-x.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PHP Basic HTTP Client\n\n[![Build Status](https://travis-ci.org/chroma-x/php-basic-http-client.svg?branch=master)](https://travis-ci.org/chroma-x/php-basic-http-client)\n[![SensioLabs Insight](https://img.shields.io/sensiolabs/i/0bc596c2-5849-4e0f-a46f-a7b94d28f97c.svg)](https://insight.sensiolabs.com/projects/0bc596c2-5849-4e0f-a46f-a7b94d28f97c)\n[![Code Climate](https://codeclimate.com/github/chroma-x/php-basic-http-client/badges/gpa.svg)](https://codeclimate.com/github/chroma-x/php-basic-http-client)\n[![Codacy Badge](https://api.codacy.com/project/badge/Grade/03db69a367dd4bbbbbd03e4612dfdbe5)](https://www.codacy.com/app/Bonscho/php-basic-http-client?utm_source=github.com\u0026amp;utm_medium=referral\u0026amp;utm_content=chroma-x/php-basic-http-client\u0026amp;utm_campaign=Badge_Grade)\n[![Latest Stable Version](https://poser.pugx.org/chroma-x/basic-http-client/v/stable)](https://packagist.org/packages/chroma-x/basic-http-client)\n[![Total Downloads](https://poser.pugx.org/chroma-x/basic-http-client/downloads)](https://packagist.org/packages/chroma-x/basic-http-client)\n[![License](https://poser.pugx.org/chroma-x/basic-http-client/license)](https://packagist.org/packages/chroma-x/basic-http-client)\n\nA basic yet extensible HTTP client library providing different authentication methods written in PHP.\n\n## What about PSR-7?\n\nPHP Basic HTTP Client is an alternative to other very good implementations like [Guzzle](https://github.com/Guzzle3/http) that are following the [PSR-7 guidelines](http://www.php-fig.org/psr/psr-7/meta/).\n\n**This project not follows these guidelines for different reasons.**\n\n1. PSR-7 is heavily over engineered due to also match complex edge cases. \n2. Objects implementing the PSR-7 interfaces have to be immutable wich is resulting in an unusual API from the PHP dev point of view and an unneccessarily increased need of performance. \n\nFind out more at the [„PSR-7 is imminent, and here's my issues with it“](https://evertpot.com/psr-7-issues/) blog post by PHP-FIG member [Evert Pot](https://evertpot.com/) and this [discussion at Stackoverflow](http://stackoverflow.com/questions/31360786/psr7-http-message-why-immutable).\n\n## Installation\n\n```{json}\n{\n   \t\"require\": {\n        \"chroma-x/basic-http-client\": \"~4.0\"\n    }\n}\n```\n\n## Usage\n\n### Autoloading and namesapce\n\n```{php}  \nrequire_once('path/to/vendor/autoload.php');\n```\n\n### Simple usage\n\n#### Preparing the HTTP client\n\n```{php}\nuse ChromaX\\BasicHttpClient;\nuse ChromaX\\BasicHttpClient\\Request\\Authentication;\nuse ChromaX\\BasicHttpClient\\Request\\Message;\n\n// Instantiating a basic HTTP client with the endpoints URL\n// If the endpoint uses the `HTTPS` schema a `HttpsTransport` instance will be used automatically.\n$client = new BasicHttpClient\\BasicHttpClient('http://requestb.in/1aipzl31');\n\n// Adding an authentication method\n$client\n\t-\u003egetRequest()\n\t-\u003eaddAuthentication(new Authentication\\BasicAuthentication('username', 'password'));\n\n// Adding custom HTTP request headers and a session cookie\n$client\n\t-\u003egetRequest()\n\t-\u003egetMessage()\n\t-\u003eaddHeader(new Message\\Header\\Header('Content-Type', array('application/x-www-form-urlencoded')))\n\t-\u003eaddHeader(new Message\\Header\\Header('Accept', array('text/html', 'text/*')))\n\t-\u003eaddCookie(new Message\\Cookie\\Cookie('PHPSESSID', '\u003cMY_SESSION_ID\u003e'));\n```\n\n#### Performing requests and read the response\n\n##### Body-less requests (GET, HEAD and DELETE)\n\nPerfoming the following `GET` request with additional query parameters\n\n```{php}\n$response = $client-\u003eget(array(\n\t'paramName1' =\u003e 'paramValue1',\n\t'paramName2' =\u003e 'paramValue2'\n));\n```\n\nwill result in the following HTTP request.\n\n```{http}\nGET /1aipzl31?paramName1=paramValue1\u0026paramName2=paramValue2 HTTP/1.1\nHost: requestb.in\nAuthorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=\nUser-Agent: PHP Basic HTTP Client 1.0\nCookie: PHPSESSID=\u003cMY_SESSION_ID\u003e\nContent-Type: application/x-www-form-urlencoded\nAccept: text/html, text/*\n```\n\nThe same mechanic is offered to perform `HEAD` and `DELETE` requests wich all are body-less.\n\n##### Body-full requests (POST, PUT, PATCH)\n\nPerfoming the following `POST` request with body data\n\n```{php}\n$response = $client-\u003epost(array(\n\t'paramName1' =\u003e 'paramValue1',\n\t'paramName2' =\u003e 'paramValue2',\n\t'paramName3' =\u003e array(\n\t\t'key1' =\u003e 'value1',\n\t\t'key2' =\u003e 'value2'\n\t)\n));\n```\n\nwill result in the following HTTP request.\n\n```{http}\nPOST /1aipzl31?paramName1=paramValue1\u0026paramName2=paramValue2 HTTP/1.1\nHost: requestb.in\nAuthorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=\nUser-Agent: PHP Basic HTTP Client 1.0\nCookie: PHPSESSID=\u003cMY_SESSION_ID\u003e\nContent-Type: application/x-www-form-urlencoded\nAccept: text/html, text/*\nContent-Length: 101\n\nparamName1=paramValue1\u0026paramName2=paramValue2\u0026paramName3%5Bkey1%5D=value1\u0026paramName3%5Bkey2%5D=value2\n```\n\nThe same mechanic is offered to perform `PUT` and `PATCH` requests wich all are body-full.\n\n---\n\n### Detailed usage\n\nThe following example shows the usage with a more detailed configuration. \n\n#### Configuring an HTTP Transport instance\n\n```{php}\nuse ChromaX\\BasicHttpClient\\Request\\Transport\\HttpTransport;\n\n// Configuring a Transport instance\n$transport = new HttpTransport();\n$transport\n\t-\u003esetHttpVersion(HttpsTransport::HTTP_VERSION_1_1)\n\t-\u003esetTimeout(5)\n\t-\u003esetReuseConnection(true)\n\t-\u003esetAllowCaching(true)\n\t-\u003esetFollowRedirects(true)\n\t-\u003esetMaxRedirects(10);\n```\n\n#### Configuring an HTTPS Transport instance\n\n```{php}\nuse ChromaX\\BasicHttpClient\\Request\\Transport\\HttpsTransport;\n\n// Configuring a Transport instance\n$transport = new HttpsTransport();\n$transport\n\t-\u003esetHttpVersion(HttpsTransport::HTTP_VERSION_1_1)\n\t-\u003esetTimeout(5)\n\t-\u003esetReuseConnection(true)\n\t-\u003esetAllowCaching(true)\n\t-\u003esetFollowRedirects(true)\n\t-\u003esetMaxRedirects(10)\n\t-\u003esetVerifyHost(true)\n\t-\u003esetVerifyPeer(true);\n```\n\n#### Configuring a Message instance with Body\n\n```{php}\nuse ChromaX\\BasicHttpClient\\Request\\Message\\Body\\Body;\nuse ChromaX\\BasicHttpClient\\Request\\Message\\Cookie\\Cookie;\nuse ChromaX\\BasicHttpClient\\Request\\Message\\Header\\Header;\nuse ChromaX\\BasicHttpClient\\Request\\Message\\Message;\n\n// Configuring a message Body instance\n$messageBody = new Body();\n$messageBody-\u003esetBodyText(\n\tjson_encode(\n\t\tarray(\n\t\t\t'paramName1' =\u003e 'paramValue1',\n\t\t\t'paramName2' =\u003e 'paramValue2',\n\t\t\t'paramName3' =\u003e array(\n\t\t\t\t'key1' =\u003e 'value1',\n\t\t\t\t'key2' =\u003e 'value2'\n\t\t\t)\n\t\t)\n\t)\n);\n\n// Configuring a Message instance\n$message = new Message();\n$message\n\t-\u003eaddHeader(new Header('Content-Type', array('application/json')))\n\t-\u003eaddHeader(new Header('Accept', array('application/json', 'text/*')))\n\t-\u003eaddHeader(new Header('Runscope-Bucket-Auth', array('7a64dde7-74d5-4eed-b170-a2ab406eff08')))\n\t-\u003eaddCookie(new Cookie('PHPSESSID', '\u003cMY_SESSION_ID\u003e'))\n\t-\u003esetBody($messageBody);\n```\n\n##### Message and request Header instances\n\n**Please note, that headers have some unusual behaviours.** Header names have an uniform way of nomenclature so the following three getter calls would have the same result.\n\n```{php}\n$header1 = $message-\u003egetHeaderByName('Content-Type');\n$header2 = $message-\u003egetHeaderByName('content-type');\n$header3 = $message-\u003egetHeaderByName('CONTENT-Type');\n```\n\nTo allow multiple request headers using the same name, the method `addAdditionalHeader` provides such a logic.\n\n```{php}\n// Add or replace a request header\n$message-\u003eaddHeader(new Header('Custom-Header', array('CustomHeaderValue')));\n// Add a request header and keep the existing one untouched\n$message-\u003eaddAdditionalHeader(new Header('Custom-Header', array('AnotherCustomHeaderValue')));\n```\n\n#### Configuring an endpoints URL, build the Request instance and perform the HTTP request\n\nFor more information about the usage of the URL object please take a look at the [PHP URL Util](https://github.com/chroma-x/php-url-util) project.\n\n```{php}\nuse ChromaX\\BasicHttpClient\\Request\\Authentication\\BasicAuthentication;\nuse ChromaX\\BasicHttpClient\\Request\\Request;\nuse ChromaX\\UrlUtil\\Url;\n\n// Setting up the endpoints URL\n$url = new Url('https://john:secret@yourapihere-com-98yq3775xff0.runscope.net:443/path/to/resource?arg1=123#fragment');\n\n// Configuring and performing a Request\n$request = new Request();\n$request\n\t-\u003esetUserAgent('PHP Basic HTTP Client Test 1.0')\n\t-\u003esetUrl($url)\n\t-\u003eaddAuthentication(new BasicAuthentication('username', 'password'))\n\t-\u003esetQueryParameters(\n\t\tarray(\n\t\t\t'paramName1' =\u003e 'paramValue1',\n\t\t\t'paramName2' =\u003e 'paramValue2'\n\t\t)\n\t)\n\t-\u003esetMethod(Request::REQUEST_METHOD_POST)\n\t-\u003esetTransport($transport)\n\t-\u003esetMessage($message)\n\t-\u003eperform();\n```\n\nThe resulting HTTP request would be the following.\n\n```{http}\nPOST /?arg1=123#fragment HTTP/1.1\nHost: yourapihere-com-98yq3775xff0.runscope.net\nAuthorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=\nUser-Agent: PHP Basic HTTP Client Test 1.0\nCookie: PHPSESSID=\u003cMY_SESSION_ID\u003e\nContent-Type: application/json\nAccept: application/json, text/*\nRunscope-Bucket-Auth: 7a64dde7-74d5-4eed-b170-a2ab406eff08\nCustom-Header: CustomHeaderValue\nCustom-Header: AnotherCustomHeaderValue\nContent-Length: 102\n\n{\"paramName1\":\"paramValue1\",\"paramName2\":\"paramValue2\",\"paramName3\":{\"key1\":\"value1\",\"key2\":\"value2\"}}\n```\n\n### Usage of authentication methods\n\nYou can add one or more Authentication instances to every Request instance. At the moment this project provides classes for [HTTP Basic Authentication](https://en.wikipedia.org/wiki/Basic_access_authentication) and [SSL Client Certificate Authentication](https://en.wikipedia.org/wiki/Transport_Layer_Security#Client-authenticated_TLS_handshake).\n\n#### HTTP Basic Authentication\n\nRequired credentials are a *username* and a *password* that get provided to the class constructor as arguments.\n\n```{php}\nuse ChromaX\\BasicHttpClient\\Request\\Authentication\\BasicAuthentication;\nuse ChromaX\\BasicHttpClient\\Request\\Request;\n\n// Configuring the authentication\n$basicAuthentication = new BasicAuthentication('username', 'password');\n\n// Adding the authentication instance to the Request\n$request = new Request();\n$request-\u003eaddAuthentication($basicAuthentication);\n```\n\n#### SSL Client Certificate Authentication\n\nRequired credentials are a *Certificate Authority Certificate*, a *Client Certificate* and the password that is used to decrypt the Client Certificate that get provided to the class constructor as arguments.\n\n```{php}\nuse ChromaX\\BasicHttpClient\\Request\\Authentication\\ClientCertificateAuthentication;\nuse ChromaX\\BasicHttpClient\\Request\\Request;\n\n// Configuring the authentication\n$clientCertificateAuthentication = new ClientCertificateAuthentication(\n\t'/var/www/project/clientCert/ca.crt',\n\t'/var/www/project/clientCert/client.crt',\n\t'clientCertPassword'\n);\n\n// Adding the authentication instance to the Request\n$request = new Request();\n$request-\u003eaddAuthentication($clientCertificateAuthentication);\n```\n\n---\n\n## Reading from the resulting Response object\n\n### Getting the response object\n\nIf using the `BasicHttpClient` the response object is returned by the termination methods listed above. If directly using the Request instance, you can get the Response object via a getter.\n\n```{php}\n// Getting the response ChromaX\\BasicHttpClient\\Response\\Response object\n$response = $request-\u003egetResponse();\n\n// Reading the HTTP status code as integer; will return `200`\necho print_r($response-\u003egetStatusCode(), true) . PHP_EOL;\n\n// Reading the HTTP status text as string; will return `HTTP/1.1 200 OK`\necho print_r($response-\u003egetStatusText(), true) . PHP_EOL;\n\n// Reading the HTTP response headers as array of ChromaX\\BasicHttpClient\\Response\\Header\\Header objects\necho print_r($response-\u003egetHeaders(), true) . PHP_EOL;\n\n// Reading the HTTP response body as string\necho print_r($response-\u003egetBody(), true) . PHP_EOL;\n```\n\n---\n\n## Getting effective Request information\n\nAfter successful performing the request, the effective request information is tracked back to the Request object. They can get accessed as follows.\n\n```{php}\n// Getting the effective endpoint URL including the query parameters\necho print_r($request-\u003egetEffectiveEndpoint(), true) . PHP_EOL;\n\n// Getting the effective HTTP status, f.e. `POST /?paramName1=paramValue1\u0026paramName2=paramValue2\u0026paramName3=1\u0026paramName4=42 HTTP/1.1`\necho print_r($request-\u003egetEffectiveStatus(), true) . PHP_EOL;\n\n// Getting the effective raw request headers as string\necho print_r($request-\u003egetEffectiveRawHeader(), true) . PHP_EOL;\n\n// Getting the effective request headers as array of `ChromaX\\BasicHttpClient\\Request\\Message\\Header\\Header` objects\necho print_r($request-\u003egetEffectiveHeaders(), true) . PHP_EOL.PHP_EOL;\n```\n\n---\n\n## Getting some transactional statistics\n\n```{php}\n// Getting the statistics ChromaX\\BasicHttpClient\\Response\\Statistics\\Statistics object\n$statistics = $request-\u003egetResponse()-\u003egetStatistics();\n\n// Reading the redirection URL if the server responds with an redirect HTTP header and \n// followRedirects is set to false\necho print_r($statistics-\u003egetRedirectEndpoint(), true).PHP_EOL;\n\n// Reading the numbers of redirection as integer\necho print_r($statistics-\u003egetRedirectCount(), true).PHP_EOL;\n\n// Getting the time in seconds the redirect utilized as float\necho print_r($statistics-\u003egetRedirectTime(), true).PHP_EOL;\n\n// Getting the time in seconds that was utilized until the connection was established\necho print_r($statistics-\u003egetConnectionEstablishTime(), true).PHP_EOL;\n\n// Getting the time in seconds that was utilized until the DNS hostname lookup was done\necho print_r($statistics-\u003egetHostLookupTime(), true).PHP_EOL;\n\n// Getting the time in seconds that was utilized before the first data was sent\necho print_r($statistics-\u003egetPreTransferTime(), true).PHP_EOL;\n\n// Getting the time in seconds that was utilized before the first data was received\necho print_r($statistics-\u003egetStartTransferTime(), true).PHP_EOL;\n\n// Getting the time in seconds that was utilized to perfom the request an read the response\necho print_r($statistics-\u003egetTotalTime(), true).PHP_EOL;\n```\n\n---\n\n## Extending the Basic HTTP Client\n\nEvery part of the client is based upon proper interfaces. Most class instances can get injected into the client itself. \nIf you want to extend the client just write some classes implementing the according interface and you´re done with that. \n\nTake a look at the [PHP JSON HTTP Client](https://github.com/chroma-x/php-json-http-client) which is an extension of the PHP Basic HTTP Client.\n\n---\n\n## Exception handling\n\nPHP Basic HTTP Client provides different exceptions – also provided by the PHP Common Exceptions project – for proper handling.  \nYou can find more information about [PHP Common Exceptions at Github](https://github.com/chroma-x/php-common-exceptions).\n\n### Exceptions to be expected\n\nIn general you should expect that any setter method could thrown an `\\InvalidArgumentException`. The following exceptions could get thrown while using PHP Basic HTTP Client.\n\n- `ChromaX\\CommonException\\IoException\\FileNotFoundException` on configuring a `ClientCertificateAuthentication`instance\n- `ChromaX\\CommonException\\IoException\\FileReadableException` on configuring a `ClientCertificateAuthentication`instance\n- `ChromaX\\BasicHttpClient\\Exception\\HttpRequestAuthenticationException` on performing a request\n- `ChromaX\\BasicHttpClient\\Exception\\HttpRequestException` on performing a request\n- `ChromaX\\CommonException\\NetworkException\\ConnectionTimeoutException` on performing a request\n- `ChromaX\\CommonException\\NetworkException\\CurlException` on performing a request\n\n---\n\n## Contribution\n\nContributing to our projects is always very appreciated.  \n**But: please follow the contribution guidelines written down in the [CONTRIBUTING.md](https://github.com/chroma-x/php-basic-http-client/blob/master/CONTRIBUTING.md) document.**\n\n## License\n\nPHP Basic HTTP Client is under the MIT license.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchroma-x%2Fphp-basic-http-client","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchroma-x%2Fphp-basic-http-client","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchroma-x%2Fphp-basic-http-client/lists"}