{"id":15477668,"url":"https://github.com/byjg/php-webrequest","last_synced_at":"2025-10-10T08:04:59.860Z","repository":{"id":34974596,"uuid":"39052575","full_name":"byjg/php-webrequest","owner":"byjg","description":"A lightweight and highly customized CURL wrapper for making RESt calls and a wrapper for call dynamically SOAP requests.","archived":false,"fork":false,"pushed_at":"2025-08-23T23:36:09.000Z","size":261,"stargazers_count":5,"open_issues_count":1,"forks_count":5,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-09T06:11:34.426Z","etag":null,"topics":["curl","hacktoberfest","php"],"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/byjg.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","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,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null},"funding":{"github":"byjg"}},"created_at":"2015-07-14T03:58:25.000Z","updated_at":"2025-01-19T20:28:43.000Z","dependencies_parsed_at":"2024-05-30T19:36:18.915Z","dependency_job_id":"b2fcd53c-f379-4d14-ab9d-54d865df2236","html_url":"https://github.com/byjg/php-webrequest","commit_stats":null,"previous_names":["byjg/webrequest"],"tags_count":18,"template":false,"template_full_name":null,"purl":"pkg:github/byjg/php-webrequest","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/byjg%2Fphp-webrequest","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/byjg%2Fphp-webrequest/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/byjg%2Fphp-webrequest/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/byjg%2Fphp-webrequest/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/byjg","download_url":"https://codeload.github.com/byjg/php-webrequest/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/byjg%2Fphp-webrequest/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279003294,"owners_count":26083555,"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","status":"online","status_checked_at":"2025-10-10T02:00:06.843Z","response_time":62,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["curl","hacktoberfest","php"],"created_at":"2024-10-02T04:01:18.106Z","updated_at":"2025-10-10T08:04:59.829Z","avatar_url":"https://github.com/byjg.png","language":"PHP","funding_links":["https://github.com/sponsors/byjg"],"categories":[],"sub_categories":[],"readme":"# Web Request\n\n[![Opensource ByJG](https://img.shields.io/badge/opensource-byjg-success.svg)](http://opensource.byjg.com)\n[![Build Status](https://github.com/byjg/webrequest/actions/workflows/phpunit.yml/badge.svg?branch=master)](https://github.com/byjg/webrequest/actions/workflows/phpunit.yml)\n[![GitHub source](https://img.shields.io/badge/Github-source-informational?logo=github)](https://github.com/byjg/webrequest/)\n[![GitHub license](https://img.shields.io/github/license/byjg/webrequest.svg)](https://opensource.byjg.com/opensource/licensing.html)\n[![GitHub release](https://img.shields.io/github/release/byjg/webrequest.svg)](https://github.com/byjg/webrequest/releases/)\n\nA lightweight PSR-7 implementation and highly customized CURL wrapper for making RESt calls.\n\n## Main Features\n\nThis class implements:\n* PSR-7 Request and Response;\n* PSR-17 Http Factories;\n* PSR-18 Http Client;\n* Helper to create Request instances with the most common use cases;\n* Wrapper to execute several requests in parallel;\n\n## PSR-7 Implementation and basic usage\n\nSince the implementation follow the PSR7 implementation there is no much explanation about the usage.\n\nThe key elements are:\n\n* URI - Will define the URI with parameters, path, host, schema, etc\n* Request - Will set the request headers and method;\n* Response - Will receive the response header, body and status code.\n\nMore information about the PSR-7 [here](https://www.php-fig.org/psr/psr-7/).\n\nThe implementation to send the request object is defined by the class `HttpClient`.\nThis class follow partially the PSR-18 implementation.\nSo, once you have a Request instance defined just need to call `HttpClient::sendRequest($request);`\n\n### Basic Usage\n\n```php\n\u003c?php\n$uri = \\ByJG\\Util\\Uri::getInstanceFromString('http://www.example.com/page');\n$request = \\ByJG\\WebRequest\\Psr7\\Request::getInstance($uri);\n$response = \\ByJG\\WebRequest\\HttpClient::getInstance()-\u003esendRequest($request);\n```\n\n### Passing arguments\n\n```php\n\u003c?php\n$uri = \\ByJG\\Util\\Uri::getInstanceFromString('http://www.example.com/page')\n    -\u003ewithQuery(http_build_query(['param'=\u003e'value']));\n\n$request = \\ByJG\\WebRequest\\Psr7\\Request::getInstance($uri);\n$response = \\ByJG\\WebRequest\\HttpClient::getInstance()-\u003esendRequest($request);\n```\n\n## Helper Classes\n\nThe WebRequest package has Helper classes to make it easy to create Request instances for some use cases.\n\n### Passing a string payload (JSON)\n\n```php\n\u003c?php\n$uri = \\ByJG\\Util\\Uri::getInstanceFromString('http://www.example.com/page');\n$request = \\ByJG\\WebRequest\\Helper\\RequestJson::build(\n   $uri,\n   \"POST\",\n   '{teste: \"value\"}'  // Support an associate array\n);\n$response = \\ByJG\\WebRequest\\HttpClient::getInstance()-\u003esendRequest($request);\n```\n\n### Create a Form Url Encoded (emulate HTTP form)\n\n```php\n\u003c?php\n$uri = \\ByJG\\Util\\Uri::getInstanceFromString('http://www.example.com/page');\n$request = \\ByJG\\WebRequest\\Helper\\RequestFormUrlEncoded::build(\n   $uri,\n   [\"param\" =\u003e \"value\"]\n);\n$response = \\ByJG\\WebRequest\\HttpClient::getInstance()-\u003esendRequest($request);\n```\n\n### Create a Multi Part request (upload documents)\n\n```php\n\u003c?php\n$uri = \\ByJG\\Util\\Uri::getInstanceFromString('http://www.example.com/page');\n\n// Define the contents to upload using a list of MultiPartItem objects\n$uploadFile = [];\n$uploadFile[] = new \\ByJG\\WebRequest\\MultiPartItem('field1', 'value1');\n$uploadFile[] = new \\ByJG\\WebRequest\\MultiPartItem(\n    'field2',\n    '{\"key\": \"value2\"}',\n    'filename.json',\n    'application/json; charset=UTF-8'\n);\n$uploadFile[] = new \\ByJG\\WebRequest\\MultiPartItem('field3', 'value3');\n\n// Use the Wrapper to create the Request\n$request = \\ByJG\\WebRequest\\Helper\\RequestMultiPart::build(Uri::getInstanceFromString($uri),\n    \"POST\",\n    $uploadFile\n);\n\n// Do the request as usual\n$response = \\ByJG\\WebRequest\\HttpClient::getInstance()-\u003esendRequest($request);\n```\n\n## Customizing the Http Client\n\nThe customizations options are:\n\n```php\n\u003c?php\n\n$client = \\ByJG\\WebRequest\\HttpClient::getInstance()\n    -\u003ewithNoFollowRedirect()         // HttpClient will not follow redirects (status codes 301 and 302). Default is follow\n    -\u003ewithNoSSLVerification()        // HttpClient will not validate the SSL certificate. Default is validate.\n    -\u003ewithProxy($uri)                // Define a http Proxy based on the URI.\n    -\u003ewithCurlOption($key, $value)   // Set an arbitrary CURL option (use with caution)\n;\n\n```\n\n## HttpClientParallel\n\nYou can use the HttpClient to do several differents requests in parallel.\n\nTo use this funcionallity you need:\n\n1. Create a instance of the HttpClientParallel class\n2. Add the RequestInterface instance\n3. Execute\n\nThe results will be processed as soon is ready.\n\nBelow a basic example:\n\n```php\n\u003c?php\n// Create the instances of the requirements\n$httpClient = \\ByJG\\WebRequest\\HttpClient::getInstance();\n\n$onSucess = function ($response, $id) {\n    // Do something with Response object\n};\n\n$onError = function ($error, $id) use (\u0026$fail) {\n    // Do something\n};\n\n// Create the HttpClientParallel\n$multi = new \\ByJG\\WebRequest\\HttpClientParallel(\n    $httpClient,\n    $onSucess,\n    $onError\n);\n\n// Add the request to run in parallel\n$request1 = Request::getInstance($uri1);\n$request2 = Request::getInstance($uri2);\n$request3 = Request::getInstance($uri3);\n\n$multi\n    -\u003eaddRequest($request1)\n    -\u003eaddRequest($request2)\n    -\u003eaddRequest($request3);\n\n// Start execute and wait to finish\n// The results will be get from the closure defined above.\n$multi-\u003eexecute();\n```\n\n## Mocking Http Client\n\nThe class `MockClient` has the same methods that HttpClient except by:\n\n* Do not send any request to the server;\n* You can add the expected Response object;\n* You can collect information from the CURL after submit the request.\n\n### Setting the expected response object\n\n```php\n\u003c?php\n$expectedResponse = new Response(200);\n\n$mock = $this-\u003eobject = new MockClient($expectedResponse);\n$response = $mock-\u003esendRequest(new Request(\"http://example.com\"));\n\nassertEquals($expectedResponse, $response);\n```\n\n### Debuging the CURL options\n\n```php\n\u003c?php\n$expectedResponse = new Response(200);\n\n$mock = $this-\u003eobject = new MockClient($expectedResponse);\n$response = $mock-\u003esendRequest(new Request(\"http://example.com\"));\n\n$expectedCurlOptions = [\n    CURLOPT_CONNECTTIMEOUT =\u003e 30,\n    CURLOPT_TIMEOUT =\u003e 30,\n    CURLOPT_HEADER =\u003e true,\n    CURLOPT_RETURNTRANSFER =\u003e true,\n    CURLOPT_USERAGENT =\u003e \"Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)\",\n    CURLOPT_FOLLOWLOCATION =\u003e true,\n    CURLOPT_SSL_VERIFYHOST =\u003e 2,\n    CURLOPT_SSL_VERIFYPEER =\u003e 1,\n    CURLOPT_HTTPHEADER =\u003e [\n        'Host: localhost:8080'\n    ],\n];\n\nassertEquals($expectedCurlOptions, $mock-\u003egetCurlConfiguration());\n```\n\n### Other methods in the MockClient\n\nThe methods below are available *after* the execution of the method `sendRequest()`:\n\n* getCurlConfiguration()\n* getRequestedObject()\n* getExpectedResponse()\n\n## Install\n\n```bash\ncomposer install \"byjg/webrequest\"\n```\n\n## Running Tests\n\n### Starting the server\n\nWe provide a docker-compose to enable start the test server easily.\n\n```bash\ndocker-compose up -d\n```\n\n### Running the integration tests\n\n```bash\nvendor/bin/phpunit\n```\n\n### Stopping the server\n\n```bash\ndocker-compose down\n```\n\n## Dependencies\n\n```mermaid\nflowchart TD\n    byjg/webrequest --\u003e psr/http-message\n    byjg/webrequest --\u003e psr/http-client\n    byjg/webrequest --\u003e ext-json\n    byjg/webrequest --\u003e byjg/uri\n```\n\n----\n[Open source ByJG](http://opensource.byjg.com)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbyjg%2Fphp-webrequest","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbyjg%2Fphp-webrequest","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbyjg%2Fphp-webrequest/lists"}