{"id":36983109,"url":"https://github.com/chroma-x/php-json-http-client","last_synced_at":"2026-01-13T22:55:16.061Z","repository":{"id":56952605,"uuid":"57300874","full_name":"chroma-x/php-json-http-client","owner":"chroma-x","description":"A JSON HTTP client library. This project also is the reference implementation for extending the PHP Basic HTTP Client.","archived":false,"fork":false,"pushed_at":"2024-08-02T09:25:44.000Z","size":30,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-11-27T14:48:51.865Z","etag":null,"topics":["basic-authentication","client-certificate","composer-package","http-client","https-client","json-client","json-request","php-library","restful-client","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}},"created_at":"2016-04-28T12:39:34.000Z","updated_at":"2025-06-09T09:05:47.000Z","dependencies_parsed_at":"2022-08-21T08:20:24.656Z","dependency_job_id":null,"html_url":"https://github.com/chroma-x/php-json-http-client","commit_stats":null,"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"purl":"pkg:github/chroma-x/php-json-http-client","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chroma-x%2Fphp-json-http-client","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chroma-x%2Fphp-json-http-client/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chroma-x%2Fphp-json-http-client/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chroma-x%2Fphp-json-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-json-http-client/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chroma-x%2Fphp-json-http-client/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28400747,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-13T14:36:09.778Z","status":"ssl_error","status_checked_at":"2026-01-13T14:35:19.697Z","response_time":56,"last_error":"SSL_read: 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","http-client","https-client","json-client","json-request","php-library","restful-client","ssl-client-certificate"],"created_at":"2026-01-13T22:55:15.403Z","updated_at":"2026-01-13T22:55:16.051Z","avatar_url":"https://github.com/chroma-x.png","language":"PHP","readme":"# PHP JSON HTTP Client\n\n[![Build Status](https://travis-ci.org/chroma-x/php-json-http-client.svg?branch=master)](https://travis-ci.org/chroma-x/php-json-http-client)\n[![SensioLabs Insight](https://img.shields.io/sensiolabs/i/6b8db506-121a-4875-8b1d-560b505e0444.svg)](https://insight.sensiolabs.com/projects/6b8db506-121a-4875-8b1d-560b505e0444)\n[![Code Climate](https://codeclimate.com/github/chroma-x/php-json-http-client/badges/gpa.svg)](https://codeclimate.com/github/chroma-x/php-json-http-client)\n[![Latest Stable Version](https://poser.pugx.org/chroma-x/json-http-client/v/stable)](https://packagist.org/packages/chroma-x/json-http-client)\n[![Total Downloads](https://poser.pugx.org/chroma-x/json-http-client/downloads)](https://packagist.org/packages/chroma-x/json-http-client)\n[![License](https://poser.pugx.org/chroma-x/json-http-client/license)](https://packagist.org/packages/chroma-x/json-http-client)\n\nA JSON HTTP client library. This project also is the reference implementation for extending the [PHP Basic HTTP Client](https://github.com/chroma-x/php-basic-http-client).\n\n## Installation\n\n```{json}\n{\n   \t\"require\": {\n        \"chroma-x/json-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\\JsonHttpClient;\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 JsonHttpClient\\JsonHttpClient('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\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\nAccept: application/json\nContent-Type: application/json\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\nAccept: application/json\nContent-Type: application/json\nContent-Length: 102\n\n{\"paramName1\":\"paramValue1\",\"paramName2\":\"paramValue2\",\"paramName3\":{\"key1\":\"value1\",\"key2\":\"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 a 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(HttpTransport::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 a 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-\u003esetVerifyPeer(true);\n```\n\n#### Configuring a Message instance with Body\n\n```{php}\nuse ChromaX\\BasicHttpClient\\Request\\Message\\Cookie\\Cookie;\nuse ChromaX\\BasicHttpClient\\Request\\Message\\Header\\Header;\nuse ChromaX\\BasicHttpClient\\Request\\Message\\Message;\nuse ChromaX\\JsonHttpClient\\Request\\Message\\Body\\JsonBody;\n\n// Configuring a message Body instance\n$messageBody = new JsonBody(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// 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')))\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\\JsonHttpClient\\Request\\JsonRequest;\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 JsonRequest();\n$request\n\t-\u003esetUserAgent('PHP JSON HTTP Client Test 1.0')\n\t-\u003esetUrl($url)\n\t-\u003eaddAuthentication(new BasicAuthentication('username', 'password'))\n\t-\u003esetMethod(JsonRequest::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 /?paramName1=paramValue1\u0026paramName2=paramValue2 HTTP/1.1\nHost: yourapihere-com-98yq3775xff0.runscope.net\nAuthorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=\nUser-Agent: PHP JSON HTTP Client Test 1.0\nCookie: PHPSESSID=\u003cMY_SESSION_ID\u003e\nContent-Type: application/json\nAccept: application/json\nRunscope-Bucket-Auth: 7a64dde7-74d5-4eed-b170-a2ab406eff08\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\\JsonHttpClient\\Request\\JsonRequest;\n\n// Configuring the authentication\n$basicAuthentication = new BasicAuthentication('username', 'password');\n\n// Adding the authentication instance to the Request\n$request = new JsonRequest();\n$response = $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\\JsonHttpClient\\Request\\JsonRequest;\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 JsonRequest();\n$response = $request-\u003eaddAuthentication($clientCertificateAuthentication);\n```\n\n---\n\n## Reading from the resulting Response object\n\n### Getting the response object\n\nIf using the `JsonHttpClient` the response object is returned by the termination methods listed above. If directly using the JsonRequest instance, you can get the JsonResponse object via a getter.\n\n```{php}\n// Getting the response ChromaX\\BasicHttpClient\\Response\\JsonResponse 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 associative array\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 JsonRequest 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## Exception handling\n\nPHP JSON 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- `ChromaX\\BasicHttpClient\\Exception\\HttpResponseException` if parsing the JSON response body fails\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-json-http-client/blob/master/CONTRIBUTING.md) document.**\n\n## License\n\nPHP JSON HTTP Client is under the MIT license.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchroma-x%2Fphp-json-http-client","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchroma-x%2Fphp-json-http-client","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchroma-x%2Fphp-json-http-client/lists"}