{"id":20540576,"url":"https://github.com/utopia-php/fetch","last_synced_at":"2025-04-14T08:38:59.996Z","repository":{"id":98436341,"uuid":"516146591","full_name":"utopia-php/fetch","owner":"utopia-php","description":"Lite \u0026 fast micro PHP Fetch library that is **easy to use**.","archived":false,"fork":false,"pushed_at":"2025-04-14T07:34:59.000Z","size":151,"stargazers_count":7,"open_issues_count":1,"forks_count":3,"subscribers_count":6,"default_branch":"main","last_synced_at":"2025-04-14T07:37:52.053Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/utopia-php.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,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2022-07-20T22:01:43.000Z","updated_at":"2025-04-14T07:34:31.000Z","dependencies_parsed_at":null,"dependency_job_id":"975ae5c7-8964-4ba2-819e-045a0be24a55","html_url":"https://github.com/utopia-php/fetch","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/utopia-php%2Ffetch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/utopia-php%2Ffetch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/utopia-php%2Ffetch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/utopia-php%2Ffetch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/utopia-php","download_url":"https://codeload.github.com/utopia-php/fetch/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248843320,"owners_count":21170479,"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":[],"created_at":"2024-11-16T01:16:23.001Z","updated_at":"2025-04-14T08:38:59.972Z","avatar_url":"https://github.com/utopia-php.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Utopia Fetch\nLite \u0026 fast micro PHP library that provides a convenient and flexible way to perform HTTP requests in PHP applications.\n\n## Usage\nCreate an instance of the `Client` class to perform HTTP requests. The instance methods allow for setting request options like headers, timeout, connection timeout, and more.\n\nThe `fetch()` method on a `Client` instance accepts the following parameters:\n- `url` - A **String** containing the URL to which the request is sent.\n- `method` - A **String** containing the HTTP method for the request. The default method is `GET`.\n- `body` - An **array** of data to send as the body of the request, which can be an associative array for form data or a JSON string.\n- `query` - An **associative array** of query parameters.\n\nThe `Response` object returned by `fetch()` provides several methods to inspect the response:\n- `isOk()` - Checks if the response status code is within the 200-299 range.\n- `getBody()` - Retrieves the response body as a string.\n- `getHeaders()` - Fetches the response headers as an associative array.\n- `getStatusCode()` - Gets the response status code.\n- `json()` - Decodes the response body to an associative array.\n- `text()` - Alias for `getBody()`, returns the response body as a string.\n- `blob()` - Returns the response body as a blob.\n\n## Examples\n\n### GET Request\n```php\nrequire_once __DIR__ . '/vendor/autoload.php';\nuse Utopia\\Fetch\\Client;\n\n$client = new Client();\n$url = 'https://httpbin.org/get';\n$method = 'GET';\n$query = ['foo' =\u003e 'bar'];\n\n// Optionally set more configurations\n$client\n  -\u003esetUserAgent('CustomUserAgent/1.0')\n  -\u003esetAllowRedirects(true)\n  -\u003esetMaxRedirects(1)\n  -\u003esetConnectionTimeout(10)\n  -\u003esetTimeout(30);\n\n$resp = $client-\u003efetch(\n    url: $url,\n    method: $method,\n    query: $query\n);\n\nif ($resp-\u003eisOk()) {\n    echo \"Status Code: \" . $resp-\u003egetStatusCode() . \"\\n\";\n    echo \"Response Headers:\\n\";\n    print_r($resp-\u003egetHeaders());\n    echo \"Response Body:\\n\";\n    echo $resp-\u003egetBody();\n} else {\n    echo \"Error: \" . $resp-\u003egetStatusCode() . \"\\n\";\n}\n```\n\n\n### POST Request\n\n```php\nrequire_once __DIR__ . '/vendor/autoload.php';\nuse Utopia\\Fetch\\Client;\n\n$client = new Client();\n$url = 'https://httpbin.org/post';\n$method = 'POST';\n$headers = ['Content-Type' =\u003e 'application/json'];\n$body = ['name' =\u003e 'John Doe'];\n$query = ['foo' =\u003e 'bar'];\n\n// Set request headers\n$client-\u003eaddHeader('Content-Type', 'application/json');\n\n$resp = $client-\u003efetch(\n    url: $url,\n    method: $method,\n    body: $body,\n    query: $query\n);\n\nprint_r($resp-\u003ejson());\n```\n\n### Sending a file\n\n```php\nrequire_once __DIR__ . '/vendor/autoload.php';\nuse Utopia\\Fetch\\Client;\n\n$client = new Client();\n$url = 'http://localhost:8000/upload';\n$method = 'POST';\n\n// Ensure you set the appropriate Content-Type for file upload\n$client-\u003eaddHeader('Content-Type', 'multipart/form-data');\n\n$filePath = realpath(__DIR__ . '/tests/resources/logo.png'); // Absolute path to the file\n$body = ['file' =\u003e new \\CURLFile($filePath, 'image/png', 'logo.png')];\n\n$resp = $client-\u003efetch(\n    url: $url,\n    method: $method,\n    body: $body\n);\n\nprint_r($resp-\u003ejson());\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Futopia-php%2Ffetch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Futopia-php%2Ffetch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Futopia-php%2Ffetch/lists"}