{"id":21538182,"url":"https://github.com/othercodes/rest","last_synced_at":"2025-04-10T03:17:17.790Z","repository":{"id":51261941,"uuid":"55184501","full_name":"othercodes/rest","owner":"othercodes","description":"Rest client to make GET, POST, PUT, DELETE, PATCH, etc calls.","archived":false,"fork":false,"pushed_at":"2023-10-10T09:36:30.000Z","size":106,"stargazers_count":10,"open_issues_count":0,"forks_count":4,"subscribers_count":3,"default_branch":"develop","last_synced_at":"2025-04-10T03:17:08.307Z","etag":null,"topics":["curl","decoder","encoder","http","http-client","rest"],"latest_commit_sha":null,"homepage":"https://othercodes.github.io/rest","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/othercodes.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-03-31T21:34:59.000Z","updated_at":"2024-11-25T17:36:11.000Z","dependencies_parsed_at":"2024-06-21T13:24:39.905Z","dependency_job_id":null,"html_url":"https://github.com/othercodes/rest","commit_stats":{"total_commits":51,"total_committers":3,"mean_commits":17.0,"dds":0.4117647058823529,"last_synced_commit":"f9a8391dc580beb252282aa482feed6c26b2e4b0"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/othercodes%2Frest","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/othercodes%2Frest/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/othercodes%2Frest/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/othercodes%2Frest/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/othercodes","download_url":"https://codeload.github.com/othercodes/rest/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248148247,"owners_count":21055548,"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":["curl","decoder","encoder","http","http-client","rest"],"created_at":"2024-11-24T04:11:00.257Z","updated_at":"2025-04-10T03:17:17.770Z","avatar_url":"https://github.com/othercodes.png","language":"PHP","readme":"# Rest Client\n\n[![Tests](https://github.com/othercodes/rest/actions/workflows/test.yml/badge.svg)](https://github.com/othercodes/rest/actions/workflows/test.yml) [![Latest Stable Version](https://poser.pugx.org/othercode/rest/v/stable)](https://packagist.org/packages/othercode/rest) [![License](https://poser.pugx.org/othercode/rest/license)](https://packagist.org/packages/othercode/rest)\n[![Total Downloads](https://poser.pugx.org/othercode/rest/downloads)](https://packagist.org/packages/othercode/rest)\n\n[Rest client](http://othercode.es/packages/rest-client.html) to make GET, POST, PUT, DELETE, PATCH, etc.\n\n## Installation\n\nTo install the package we only have to add the dependency to ***composer.json*** file:\n\n```json\n{\n  \"require\": {\n    \"othercode/rest\": \"*\"\n  }\n}\n```\n\nAnd run the following command:\n\n```bash\ncomposer install\n```\n\n## Usage\n\nTo use the Rest we only have to instantiate it and configure the params we want. We can\nestablish the configuration accessing to the `-\u003econfiguration-\u003econfigure_property`, for example\nto configure the url of the call we only have to set the `-\u003econfiguration-\u003eurl parameter` like we can see as follows:\n\n```php\n$api = new \\OtherCode\\Rest\\Rest();\n$api-\u003econfiguration-\u003eurl = \"https://jsonplaceholder.typicode.com/\";\n```\n\nor\n\n```php\n$api = new \\OtherCode\\Rest\\Rest(new \\OtherCode\\Rest\\Core\\Configuration(array(\n    'url' =\u003e 'https://jsonplaceholder.typicode.com/',\n)));\n```\n\nAfter this we have to set the type of call and the parameters that we wil use, in this case we are\ngoing to perform a **GET** request to the **\"posts/1\"** endpoint:\n\n```php\n$response = $api-\u003eget(\"posts/1\");\n```\n\nThe rest client will throw a `ConnectionException` if there are any problem related to the connection.\n\n**NOTE: These errors are related to the session cURL, here is\nthe [complete list](https://curl.haxx.se/libcurl/c/libcurl-errors.html)**\n\n## Methods\n\nThe available methods to work with are:\n\n#### `get()`\n\nPerform a GET request.\n\n| Parameters | Type   | Description                                    |\n|------------|--------|------------------------------------------------|\n| `$url`     | String | Required. The URL to which the request is made |\n| `$data`    | Array  | Optional. Associative array of data parameters |\n\n**Return**: Response object\n\n#### `head()`\n\nPerform a HEAD request.\n\n| Parameters | Type   | Description                                    |\n|------------|--------|------------------------------------------------|\n| `$url`     | String | Required. The URL to which the request is made |\n\n**Return**: Response object (no body)\n\n#### `post()`\n\nPerform a POST request.\n\n| Parameters | Type   | Description                                    |\n|------------|--------|------------------------------------------------|\n| `$url`     | String | Required. The URL to which the request is made |\n| `$data`    | Array  | Optional. Associative array of data parameters |\n\n**Return**: Response object\n\n#### `delete()`\n\nPerform a DELETE request.\n\n| Parameters | Type   | Description                                    |\n|------------|--------|------------------------------------------------|\n| `$url`     | String | Required. The URL to which the request is made |\n| `$data`    | Array  | Optional. Associative array of data parameters |\n\n**Return**: Response object\n\n#### `put()`\n\nPerform a PUT request.\n\n| Parameters | Type   | Description                                    |\n|------------|--------|------------------------------------------------|\n| `$url`     | String | Required. The URL to which the request is made |\n| `$data`    | Array  | Optional. Associative array of data parameters |\n\n**Return**: Response object\n\n#### `patch()`\n\nPerform a PATCH request.\n\n| Parameters | Type   | Description                                    |\n|------------|--------|------------------------------------------------|\n| `$url`     | String | Required. The URL to which the request is made |\n| `$data`    | Array  | Optional. Associative array of data parameters |\n\n**Return**: Response object\n\n#### `getMetadata()`\n\nReturn the metadata of the request.\n\n**Return**: Array\n\n#### `getError()`\n\nReturn the last known error.\n\n**Return**: `Error` object\n\n#### `getPayloads()`\n\nReturn an array with the `Response` and `Request` objects.\n\n**Return**: Array\n\n#### `setDecoder()`\n\nSet a new Decoder.\n\n| Parameters | Type   | Description                                                 |\n|------------|--------|-------------------------------------------------------------|\n| `$name`    | String | Required. The unique name of the decoder.                   |\n| `$decoder` | String | Optional. The class name with namespace of the new decoder. |\n\n**Return**: Rest object\n\n#### `setEncoder()`\n\nSet a new Encoder.\n\n| Parameters | Type   | Description                                                 |\n|------------|--------|-------------------------------------------------------------|\n| `$name`    | String | Required. The unique name of the encoder.                   |\n| `$encoder` | String | Optional. The class name with namespace of the new encoder. |\n\n**Return**: Rest object\n\n#### `setModule()`\n\nSet a new Module.\n\n| Parameters | Type   | Description                                                                              |\n|------------|--------|------------------------------------------------------------------------------------------|\n| `$name`    | String | Required. The unique name of the module.                                                 |\n| `$module`  | String | Required. The class name with namespace of the new module.                               |\n| `$hook`    | String | Optional. The hook name (after/before) that will trigger the module, 'after' by default. |\n\n**Return**: Rest object\n\n#### `unsetModule()`\n\nUnregister a module.\n\n| Parameters    | Type   | Description                                                          |\n|---------------|--------|----------------------------------------------------------------------|\n| `$moduleName` | String | Required. The unique name of the decoder.                            |\n| `$hook`       | String | Optional. The hook name (after/before) from where delete the module. |\n\n**Return**: Rest object\n\n#### `addHeader()`\n\nAdd a new header.\n\n| Parameters | Type   | Description                              |\n|------------|--------|------------------------------------------|\n| `$header`  | String | Required. The unique name of the header. |\n| `$value`   | String | Requires. The value of the header.       |\n\n**Return**: Rest object\n\n#### `addHeaders()`\n\nAdd an array of headers.\n\n| Parameters | Type   | Description                    |\n|------------|--------|--------------------------------|\n| `$headers` | String | Required. An array of headers. |\n\n**Return**: Rest object\n\n**NOTE: We can use the `addHeader()` and `addHeaders()` methods with the `Rest` instance or with the `configuration`\nobject**\n\n```php\n$api-\u003eaddHeader('some_header','some_value');\n$api-\u003eaddHeaders(array('some_header' =\u003e 'some_value','other_header' =\u003e 'other_value'));\n```\n\nis the same as\n\n```php\n$api-\u003econfiguration-\u003eaddHeader('some_header','some_value');\n$api-\u003econfiguration-\u003eaddHeaders(array('some_header' =\u003e 'some_value','other_header' =\u003e 'other_value'));\n```\n\n#### `removeHeader()`\n\nRemove a header offset.\n\n| Parameters | Type   | Description                              |\n|------------|--------|------------------------------------------|\n| `$header`  | String | Required. The unique name of the header. |\n\n**Return**: Rest object\n\n#### `removeHeaders()`\n\nRemove an array of headers.\n\n| Parameters | Type   | Description                              |\n|------------|--------|------------------------------------------|\n| `$headers` | String | Required. An array of headers to remove. |\n\n**Return**: Rest object\n\n## Modules\n\nThis package allow you to create modules to perform task before and after the request.\nTo create a new module we only have to use this template:\n\n```php\nclass CustomModule extends BaseModule\n{\n    public function run()\n    {\n        // do something\n    }\n}\n```\n\n**IMPORTANT: Any module MUST extends BaseModule**\n\nThe only method that is mandatory is `-\u003erun()`, this method execute your custom code of the module.\n\nOnce we have our module we can register it with the `-\u003esetModule()` method. This method needs three parameters,\nthe first one is the name of the module, the second one is the complete namespace of the module, and the third one\nis the hook name for our module, it can be \"before\" and \"after\" depends on when we want to launch our module.\n\n```php\n$api-\u003esetModule('module_name','Module\\Complete\\Namespace','after');\n```\n\nFor \"before\" modules you can use all the properties of the Request object.\n\n* `method`\n* `url`\n* `headers`\n* `body`\n\nFor \"after\" modules you can use all the properties of the Response object.\n\n* `code`\n* `content_type`\n* `charset`\n* `body`\n* `headers`\n* `error`\n* `metadata`\n\nAll modules are executed in the order that we register them into the Rest client, this also affect\nto Decoders and Encoders.\n\n## Decoders\n\nA decoder is a kind of module that allows you to automatically decode de response in xml or json, to use them\nwe only have to set the decoder we want with the `-\u003esetDecoder()` method:\n\n```php\n$api-\u003esetDecoder(\"json\");\n```\n\nThe default allowed values for this method are: ***json***, ***xml*** and ***xmlrpc***. All the decoders are always\nexecuted\nin the \"after\" hook.\n\n### Custom Decoders\n\nTo create a new decoder we only have to use this template:\n\n```php\nclass CustomDecoder extends BaseDecoder\n{\n\tprotected $contentType = 'application/json';\n\n\tprotected function decode()\n\t{\n\t\t// decode $this-\u003ebody\n\t}\n}\n```\n\nLike in modules, we have the Response object available to work. The $contentType property is the content-type\nthat will trigger the decoder, in the example above all responses with content-type \"application/json\" will\ntrigger this decoder.\n\n### Quick Calls\n\nWe can do quick calls using the `\\OtherCode\\Rest\\Payloads\\Request::call()` method. This static method returns a\nRest instance, so we can use all the methods from it.\n\n```\n$response = \\OtherCode\\Rest\\Payloads\\Request::call('https://jsonplaceholder.typicode.com')\n    -\u003esetDecoder('json')\n    -\u003eget('/posts/1');\n```\n\n## Complete Example\n\n```php\nrequire_once '../autoload.php';\n\ntry {\n\n    $api = new \\OtherCode\\Rest\\Rest(new \\OtherCode\\Rest\\Core\\Configuration(array(\n        'url' =\u003e 'https://jsonplaceholder.typicode.com/',\n        'httpheader' =\u003e array(\n            'some_header' =\u003e 'some_value',\n        )\n    )));\n    $api-\u003esetDecoder(\"json\");\n    \n    $response = $api-\u003eget(\"posts/1\");\n    var_dump($response);\n    \n} catch (\\Exception $e) {\n    print \"\u003e \" . $e-\u003egetMessage() . \"\\n\"\n}\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fothercodes%2Frest","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fothercodes%2Frest","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fothercodes%2Frest/lists"}