{"id":15251802,"url":"https://github.com/apimatic/unirest-php","last_synced_at":"2025-04-04T22:05:22.214Z","repository":{"id":19942007,"uuid":"23208683","full_name":"apimatic/unirest-php","owner":"apimatic","description":"Unirest in PHP: Simplified, lightweight HTTP library.","archived":false,"fork":false,"pushed_at":"2025-02-10T08:01:02.000Z","size":399,"stargazers_count":21,"open_issues_count":0,"forks_count":11,"subscribers_count":5,"default_branch":"v2-master","last_synced_at":"2025-03-28T21:05:54.003Z","etag":null,"topics":["client","http","http-client","php","rest"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/apimatic.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":"2014-08-22T01:24:36.000Z","updated_at":"2025-03-23T19:58:47.000Z","dependencies_parsed_at":"2023-09-22T09:52:00.475Z","dependency_job_id":"f7ef1508-77e6-413f-b388-7b99e0b5dd21","html_url":"https://github.com/apimatic/unirest-php","commit_stats":{"total_commits":275,"total_committers":38,"mean_commits":"7.2368421052631575","dds":0.7127272727272727,"last_synced_commit":"206b24ca6e34eafad4ad7b4ebbeebe993fea1db6"},"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apimatic%2Funirest-php","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apimatic%2Funirest-php/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apimatic%2Funirest-php/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apimatic%2Funirest-php/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/apimatic","download_url":"https://codeload.github.com/apimatic/unirest-php/tar.gz/refs/heads/v2-master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247256110,"owners_count":20909240,"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":["client","http","http-client","php","rest"],"created_at":"2024-09-29T18:06:55.063Z","updated_at":"2025-04-04T22:05:22.192Z","avatar_url":"https://github.com/apimatic.png","language":"PHP","readme":"# Unirest for PHP\n\n[![version][packagist-version]][packagist-url]\n[![Downloads][packagist-downloads]][packagist-url]\n[![Tests](https://github.com/apimatic/unirest-php/actions/workflows/php.yml/badge.svg)](https://github.com/apimatic/unirest-php/actions/workflows/php.yml)\n[![License][packagist-license]][license-url]\n\nUnirest is a set of lightweight HTTP libraries available in [multiple languages](http://unirest.io).\n\nThis fork is maintained by [APIMatic](https://www.apimatic.io) for its Code Generator as a Service.\n\n## Features\n\n* Request class to create custom requests\n* Simple HttpClientInterface to execute a Request\n* Automatic JSON parsing into a native object for JSON responses\n* Response data class to store http response information\n* Supports form parameters, file uploads and custom body entities\n* Supports gzip\n* Supports Basic, Digest, Negotiate, NTLM Authentication natively\n* Configuration class manage all the HttpClient's configurations\n* Customizable timeout\n* Customizable retries and backoff\n* Customizable default headers for every request (DRY)\n\n## Supported PHP Versions\n- PHP 7.2\n- PHP 7.4\n- PHP 8.0\n- PHP 8.1\n- PHP 8.2\n\n## Installation\n\nTo install `apimatic/unirest-php` with Composer, just add the following to your `composer.json` file:\n\n```json\n{\n    \"require\": {\n        \"apimatic/unirest-php\": \"^4.0.0\"\n    }\n}\n```\n\nor by running the following command:\n\n```shell\ncomposer require apimatic/unirest-php\n```\n\n## Usage\n\n### Creating a HttpClient with Default Configurations\nYou can create a variable at class level and instantiate it with an instance of `HttpClient`, like:\n\n```php\nprivate $httpClient = new \\Unirest\\HttpClient();\n```\n\n### Creating a HttpClient with Custom Configurations\nTo create a client with custom configurations you first required an instance of `Configuration`, and then add it to the HttpClient during its initialization, like:\n```php\n$configurations = \\Unirest\\Configuration::init()\n    -\u003etimeout(10)\n    -\u003eenableRetries(true)\n    -\u003eretryInterval(2.5);\n$httpClient = new \\Unirest\\HttpClient($configurations);\n```\nThis `Configuration` instance can further be customized by setting properties like: `maximumRetryWaitTime`, `verifyPeer`, `defaultHeaders`, etc. Check out [Advanced Configuration](#advanced-configuration) for more information.\n\n### Creating a Request\nAfter the initialization of HttpClient, you will be needing an instance of `Request` that is required to be exchanged as `Response`.\n```php\n$request = new \\Unirest\\Request\\Request(\n    'http://mockbin.com/request',\n    RequestMethod::GET,\n    ['headerKey' =\u003e 'headerValue'],\n    Unirest\\Request\\Body::json([\"key\" =\u003e \"value\"]'),\n    RetryOption::ENABLE_RETRY\n);\n```\nLet's look at a working example of sending the above request:\n\n```php\n$headers = array('Accept' =\u003e 'application/json');\n$query = array('foo' =\u003e 'hello', 'bar' =\u003e 'world');\n\n$response = $this-\u003ehttpClient-\u003eexecute($request);\n\n$response-\u003egetStatusCode(); // HTTP Status code\n$response-\u003egetHeaders();    // Headers\n$response-\u003egetBody();       // Parsed body\n$response-\u003egetRawBody();    // Unparsed body\n```\n\n### JSON Requests *(`application/json`)*\n\nA JSON Request can be constructed using the `Unirest\\Request\\Body::Json` helper:\n\n```php\n$headers = array('Accept' =\u003e 'application/json');\n$data = array('name' =\u003e 'ahmad', 'company' =\u003e 'mashape');\n\n$body = Unirest\\Request\\Body::Json($data);\n$request = new \\Unirest\\Request\\Request(\n    'http://mockbin.com/request',\n    RequestMethod::POST,\n    $headers,\n    $body\n);\n$response = $this-\u003ehttpClient-\u003eexecute($request);\n```\n\n**Notes:**\n- `Content-Type` headers will be automatically set to `application/json` \n- the data variable will be processed through [`json_encode`](http://php.net/manual/en/function.json-encode.php) with default values for arguments.\n- an error will be thrown if the [JSON Extension](http://php.net/manual/en/book.json.php) is not available.\n\n### Form Requests *(`application/x-www-form-urlencoded`)*\n\nA typical Form Request can be constructed using the `Unirest\\Request\\Body::Form` helper:\n\n```php\n$headers = array('Accept' =\u003e 'application/json');\n$data = array('name' =\u003e 'ahmad', 'company' =\u003e 'mashape');\n\n$body = Unirest\\Request\\Body::Form($data);\n$request = new \\Unirest\\Request\\Request(\n    'http://mockbin.com/request',\n    RequestMethod::POST,\n    $headers,\n    $body\n);\n$response = $this-\u003ehttpClient-\u003eexecute($request);\n```\n\n**Notes:** \n- `Content-Type` headers will be automatically set to `application/x-www-form-urlencoded`\n- the final data array will be processed through [`http_build_query`](http://php.net/manual/en/function.http-build-query.php) with default values for arguments.\n\n### Multipart Requests *(`multipart/form-data`)*\n\nA Multipart Request can be constructed using the `Unirest\\Request\\Body::Multipart` helper:\n\n```php\n$headers = array('Accept' =\u003e 'application/json');\n$data = array('name' =\u003e 'ahmad', 'company' =\u003e 'mashape');\n\n$body = Unirest\\Request\\Body::Multipart($data);\n$request = new \\Unirest\\Request\\Request(\n    'http://mockbin.com/request',\n    RequestMethod::POST,\n    $headers,\n    $body\n);\n$response = $this-\u003ehttpClient-\u003eexecute($request);\n```\n\n**Notes:** \n\n- `Content-Type` headers will be automatically set to `multipart/form-data`.\n- an auto-generated `--boundary` will be set.\n\n### Multipart File Upload\n\nsimply add an array of files as the second argument to to the `Multipart` helper:\n\n```php\n$headers = array('Accept' =\u003e 'application/json');\n$data = array('name' =\u003e 'ahmad', 'company' =\u003e 'mashape');\n$files = array('bio' =\u003e '/path/to/bio.txt', 'avatar' =\u003e '/path/to/avatar.jpg');\n\n$body = Unirest\\Request\\Body::Multipart($data, $files);\n$request = new \\Unirest\\Request\\Request(\n    'http://mockbin.com/request',\n    RequestMethod::POST,\n    $headers,\n    $body\n);\n$response = $this-\u003ehttpClient-\u003eexecute($request);\n ```\n\nIf you wish to further customize the properties of files uploaded you can do so with the `Unirest\\Request\\Body::File` helper:\n\n```php\n$headers = array('Accept' =\u003e 'application/json');\n$body = array(\n    'name' =\u003e 'ahmad', \n    'company' =\u003e 'mashape'\n    'bio' =\u003e Unirest\\Request\\Body::File('/path/to/bio.txt', 'text/plain'),\n    'avatar' =\u003e Unirest\\Request\\Body::File('/path/to/my_avatar.jpg', 'text/plain', 'avatar.jpg')\n);\n$request = new \\Unirest\\Request\\Request(\n    'http://mockbin.com/request',\n    RequestMethod::POST,\n    $headers,\n    $body\n);\n$response = $this-\u003ehttpClient-\u003eexecute($request);\n ```\n\n**Note**: we did not use the `Unirest\\Request\\Body::multipart` helper in this example, it is not needed when manually adding files.\n \n### Custom Body\n\nSending a custom body such rather than using the `Unirest\\Request\\Body` helpers is also possible, for example, using a [`serialize`](http://php.net/manual/en/function.serialize.php) body string with a custom `Content-Type`:\n\n```php\n$headers = array('Accept' =\u003e 'application/json', 'Content-Type' =\u003e 'application/x-php-serialized');\n$body = serialize((array('foo' =\u003e 'hello', 'bar' =\u003e 'world'));\n$request = new \\Unirest\\Request\\Request(\n    'http://mockbin.com/request',\n    RequestMethod::POST,\n    $headers,\n    $body\n);\n$response = $this-\u003ehttpClient-\u003eexecute($request);\n```\n\n### Authentication\nFor Authentication you need httpClient instance with custom configurations, So, create `Configuration` instance like:\n\n```php\n// Basic auth\n$configuration = Configuration::init()\n    -\u003eauth('username', 'password', CURLAUTH_BASIC);\n```\n\nThe third parameter, which is a bitmask, will Unirest which HTTP authentication method(s) you want it to use for your proxy authentication.\n\nIf more than one bit is set, Unirest *(at PHP's libcurl level)* will first query the site to see what authentication methods it supports and then pick the best one you allow it to use. *For some methods, this will induce an extra network round-trip.*\n\n**Supported Methods**\n\n| Method               | Description                                                                                                                                                                                                     |\n| -------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `CURLAUTH_BASIC`     | HTTP Basic authentication. This is the default choice                                                                                                                                                           | \n| `CURLAUTH_DIGEST`    | HTTP Digest authentication. as defined in [RFC 2617](http://www.ietf.org/rfc/rfc2617.txt)                                                                                                                       | \n| `CURLAUTH_DIGEST_IE` | HTTP Digest authentication with an IE flavor. *The IE flavor is simply that libcurl will use a special \"quirk\" that IE is known to have used before version 7 and that some servers require the client to use.* | \n| `CURLAUTH_NEGOTIATE` | HTTP Negotiate (SPNEGO) authentication. as defined in [RFC 4559](http://www.ietf.org/rfc/rfc4559.txt)                                                                                                           |\n| `CURLAUTH_NTLM`      | HTTP NTLM authentication. A proprietary protocol invented and used by Microsoft.                                                                                                                                |\n| `CURLAUTH_NTLM_WB`   | NTLM delegating to winbind helper. Authentication is performed by a separate binary application. *see [libcurl docs](http://curl.haxx.se/libcurl/c/CURLOPT_HTTPAUTH.html) for more info*                        | \n| `CURLAUTH_ANY`       | This is a convenience macro that sets all bits and thus makes libcurl pick any it finds suitable. libcurl will automatically select the one it finds most secure.                                               |\n| `CURLAUTH_ANYSAFE`   | This is a convenience macro that sets all bits except Basic and thus makes libcurl pick any it finds suitable. libcurl will automatically select the one it finds most secure.                                  |\n| `CURLAUTH_ONLY`      | This is a meta symbol. OR this value together with a single specific auth value to force libcurl to probe for un-restricted auth and if not, only that single auth algorithm is acceptable.                     |\n\n```php\n// custom auth method\n$configuration = Configuration::init()\n    -\u003eproxyAuth('username', 'password', CURLAUTH_DIGEST);\n```\n### Cookies\n\nSet a cookie string to specify the contents of a cookie header. Multiple cookies are separated with a semicolon followed by a space (e.g., \"fruit=apple; colour=red\")\n\n```php\n$configuration = Configuration::init()\n    -\u003ecookie($cookie);\n```\n\nSet a cookie file path for enabling cookie reading and storing cookies across multiple sequence of requests.\n\n```php\n$this-\u003erequest-\u003ecookieFile($cookieFile)\n```\n\n`$cookieFile` must be a correct path with write permission.\n\n### Response Object\n\nUpon receiving a response Unirest returns the result in the form of an Object, this object should always have the same keys for each language regarding to the response details.\n- `getStatusCode()` - HTTP Response Status Code (Example `200`)\n- `getHeaders()` - HTTP Response Headers\n- `getBody()` - Parsed response body where applicable, for example JSON responses are parsed to Objects / Associative Arrays.\n- `getRawBody()` - Un-parsed response body\n\n### Advanced Configuration\n\nYou can set some advanced configuration to tune Unirest-PHP:\n\n#### Custom JSON Decode Flags\n\nUnirest uses PHP's [JSON Extension](http://php.net/manual/en/book.json.php) for automatically decoding JSON responses.\nsometime you may want to return associative arrays, limit the depth of recursion, or use any of the [customization flags](http://php.net/manual/en/json.constants.php).\n\nTo do so, simply set the desired options using the `jsonOpts` request method:\n\n```php\n$configuration = Configuration::init()\n    -\u003ejsonOpts(true, 512, JSON_NUMERIC_CHECK \u0026 JSON_FORCE_OBJECT \u0026 JSON_UNESCAPED_SLASHES);\n```\n\n#### Timeout\n\nYou can set a custom timeout value (in **seconds**):\n\n```php\n$configuration = Configuration::init()\n    -\u003etimeout(5); // 5s timeout\n```\n\n#### Retries Related\n\n```php\n$configuration = Configuration::init()\n    -\u003eenableRetries(true)               // To enable retries feature\n    -\u003emaxNumberOfRetries(10)            // To set max number of retries\n    -\u003eretryOnTimeout(false)             // Should we retry on timeout\n    -\u003eretryInterval(20)                 // Initial retry interval in seconds\n    -\u003emaximumRetryWaitTime(30)          // Maximum retry wait time\n    -\u003ebackoffFactor(1.1)                // Backoff factor to be used to increase retry interval\n    -\u003ehttpStatusCodesToRetry([400,401]) // Http status codes to retry against\n    -\u003ehttpMethodsToRetry(['POST'])      // Http methods to retry against\n```\n\n#### Proxy\n\nSet the proxy to use for the upcoming request.\n\nyou can also set the proxy type to be one of `CURLPROXY_HTTP`, `CURLPROXY_HTTP_1_0`, `CURLPROXY_SOCKS4`, `CURLPROXY_SOCKS5`, `CURLPROXY_SOCKS4A`, and `CURLPROXY_SOCKS5_HOSTNAME`.\n\n*check the [cURL docs](http://curl.haxx.se/libcurl/c/CURLOPT_PROXYTYPE.html) for more info*.\n\n```php\n// quick setup with default port: 1080\n$configuration = Configuration::init()\n    -\u003eproxy('10.10.10.1');\n\n// custom port and proxy type\n$configuration = Configuration::init()\n    -\u003eproxy('10.10.10.1', 8080, CURLPROXY_HTTP);\n\n// enable tunneling\n$configuration = Configuration::init()\n    -\u003eproxy('10.10.10.1', 8080, CURLPROXY_HTTP, true);\n```\n\n##### Proxy Authentication\n\nPassing a username, password *(optional)*, defaults to Basic Authentication:\n\n```php\n// basic auth\n$configuration = Configuration::init()\n    -\u003eproxyAuth('username', 'password');\n```\n\nThe third parameter, which is a bitmask, will Unirest which HTTP authentication method(s) you want it to use for your proxy authentication. \n\nIf more than one bit is set, Unirest *(at PHP's libcurl level)* will first query the site to see what authentication methods it supports and then pick the best one you allow it to use. *For some methods, this will induce an extra network round-trip.*\n\nSee [Authentication](#authentication) for more details on methods supported.\n\n```php\n// basic auth\n$configuration = Configuration::init()\n    -\u003eproxyAuth('username', 'password', CURLAUTH_DIGEST);\n```\n\n#### Default Request Headers\n\nYou can set default headers that will be sent on every request:\n\n```php\n$configuration = Configuration::init()\n    -\u003edefaultHeader('Header1', 'Value1')\n    -\u003edefaultHeader('Header2', 'Value2');\n```\n\nYou can set default headers in bulk by passing an array:\n\n```php\n$configuration = Configuration::init()\n    -\u003edefaultHeaders([\n        'Header1' =\u003e 'Value1',\n        'Header2' =\u003e 'Value2'\n    ]);\n```\n\nYou can clear the default headers anytime with:\n\n```php\n$configuration = Configuration::init()\n    -\u003eclearDefaultHeaders();\n```\n\n#### Default cURL Options\n\nYou can set default [cURL options](http://php.net/manual/en/function.curl-setopt.php) that will be sent on every request:\n\n```php\n$configuration = Configuration::init()\n    -\u003ecurlOpt(CURLOPT_COOKIE, 'foo=bar');\n```\n\nYou can set options bulk by passing an array:\n\n```php\n$configuration = Configuration::init()\n    -\u003ecurlOpts(array(\n    CURLOPT_COOKIE =\u003e 'foo=bar'\n));\n```\n\nYou can clear the default options anytime with:\n\n```php\n$configuration = Configuration::init()\n    -\u003eclearCurlOpts();\n```\n\n#### SSL validation\n\nYou can explicitly enable or disable SSL certificate validation when consuming an SSL protected endpoint:\n\n```php\n$configuration = Configuration::init()\n    -\u003everifyPeer(false); // Disables SSL cert validation\n```\n\nBy default is `true`.\n\n#### Utility Methods\n\n```php\n// alias for `curl_getinfo`\n$httpClient-\u003egetInfo();\n\n```\n\n----\n\n[license-url]: https://github.com/apimatic/unirest-php/blob/master/LICENSE\n[travis-url]: https://travis-ci.org/apimatic/unirest-php\n[travis-image]: https://img.shields.io/travis/apimatic/unirest-php.svg?style=flat\n[packagist-url]: https://packagist.org/packages/apimatic/unirest-php\n[packagist-license]: https://img.shields.io/packagist/l/apimatic/unirest-php.svg?style=flat\n[packagist-version]: https://img.shields.io/packagist/v/apimatic/unirest-php.svg?style=flat\n[packagist-downloads]: https://img.shields.io/packagist/dm/apimatic/unirest-php.svg?style=flat\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapimatic%2Funirest-php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fapimatic%2Funirest-php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapimatic%2Funirest-php/lists"}