{"id":21538189,"url":"https://github.com/imroychen/request","last_synced_at":"2025-04-10T03:17:23.284Z","repository":{"id":43287266,"uuid":"396626426","full_name":"imroychen/request","owner":"imroychen","description":"Support Http requests such as Get/Post/Put/Upload/download. Single-threaded and multi-threaded seamless switching. This package is an encapsulation of the \"CURL\" library, which can easily implement various network requests, API requests, etc. Single-threaded and multi-threaded concurrent requests can be seamlessly switched at any time","archived":false,"fork":false,"pushed_at":"2023-02-10T13:43:35.000Z","size":37,"stargazers_count":1,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-10T03:17:11.999Z","etag":null,"topics":["curl","curl-multi","multi-threaded-request","php-request","post","request"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/imroychen.png","metadata":{"files":{"readme":"README-EN.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2021-08-16T05:02:28.000Z","updated_at":"2022-01-11T03:52:53.000Z","dependencies_parsed_at":"2024-11-24T04:11:06.307Z","dependency_job_id":null,"html_url":"https://github.com/imroychen/request","commit_stats":{"total_commits":16,"total_committers":2,"mean_commits":8.0,"dds":0.0625,"last_synced_commit":"401e8a80cc3134b2e51f5f599a342613cbfabdb1"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imroychen%2Frequest","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imroychen%2Frequest/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imroychen%2Frequest/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imroychen%2Frequest/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/imroychen","download_url":"https://codeload.github.com/imroychen/request/tar.gz/refs/heads/master","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","curl-multi","multi-threaded-request","php-request","post","request"],"created_at":"2024-11-24T04:11:01.328Z","updated_at":"2025-04-10T03:17:23.264Z","avatar_url":"https://github.com/imroychen.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":" [中文简体](README.md) --------  **English**\r\n\r\nSupport Http requests such as Get/Post/Put/Upload/download. \r\nSingle-threaded and multi-threaded seamless switching. \r\nThis package is an encapsulation of the \"CURL\" library, \r\nwhich can easily implement various network requests, API requests, etc. \r\nSingle-threaded and multi-threaded concurrent requests can be seamlessly switched at any time\r\n# installation\r\n```shell\r\ncomposer require iry/request\r\ncomposer update\r\n```\r\n---\r\n# Usage examples\r\n## 1. Helper function\r\n```php\r\n\\iry\\http\\Helper::get(url);             //Send a Get request\r\n\\iry\\http\\Helper::post(url,$post);      //Send a Post request\r\n\\iry\\http\\Helper::put(url,'This is test'); \r\n\\iry\\http\\Helper::request(url,$argvs,isPost);\r\n\\iry\\http\\Helper::upload(url,'/test.jpg',['id'=\u003e100]);              //upload file=\u003e'test.jpg'\r\n\\iry\\http\\Helper::upload(url,['img'=\u003e'/test2.jpg'],['id'=\u003e101]);    //upload\r\n\\iry\\http\\Helper::download(url,$dist);  //Download a file\r\n\r\n```\r\n---\r\n## 2. Request Instructions:\r\n7. E.g.\r\n```php\r\n//1. Simple usage\r\n$res = (new Request($url)) -\u003e getResult();\r\n//2. Multi-task concurrent request\r\n//Conventional style\r\n$http = new Request();\r\n//$http-\u003esetThread(20);//设置最大20并发，任务总数超过20会分批处理\r\n$http-\u003eadd(url,config,requestID);//\r\n$http-\u003eadd(.....);\r\n$thtp-\u003ecall(function($requestId,$resVi,$currentRequest,$_this){\r\n\r\n});\r\n//Usage of chain method\r\n(new Request()) -\u003e setThread(20)-\u003eadd('url','...')-\u003e....-\u003eadd(url_n,'....')-\u003ecall(function(){\r\n\r\n});\r\n```\r\nSupported methods:\r\n1. getResult ：_Get all request results, return an array, each element is the result of a task_\r\n2. call: _Send the request and process the result through an anonymous function（**Recommended Use**)_\r\n3. setThread：_Set the number of threads_\r\n4. on: _Listen for events_\r\n5. add:_Add a request task_\r\n6. setTasks:_Batch setup tasks_\r\n---\r\n---\r\n\r\n## 3. Examples of various usages of Request：\r\n### ①. Simple to use [applicable to small return data]\r\n```php\r\nuse iry\\http\\Request;\r\n\r\n//Send a Get request\r\n$res = (new Request($url)) -\u003e getResult();\r\n\r\n//Send a Post request\r\n$res = (new Request($url,['post'=\u003e[...])) -\u003e getResult();\r\n```\r\n### ②. Use \"ADD\" to add multiple tasks to achieve multi-threaded concurrent requests\r\nsend request\r\n```php\r\n$http = new Request();\r\n//$http-\u003eadd($url_1,$cfg_1,request_1_id);  //request_1_id: Unique ID, used to track the result of each request in multiple requests\r\n$http-\u003eadd($url_2,$cfg_2,request_2_id)-\u003eadd(....)-\u003eadd(....); //Support chain call\r\n//$result = $http-\u003egetResult(); or $http-\u003ecall(..);\r\n```\r\n#### getResult\r\nReturn a two-dimensional array, requestID is the key, \r\nand the logic code for processing the result is as follows\r\n```php\r\n$result = $http-\u003egetResult();\r\nforeach($result as $itemResult){\r\n\t$html = $http-\u003egetContent($itemResult);\r\n \t$headerInfo = $http-\u003egetInfo($itemResult);\r\n \t//.....More logic codes......\r\n \t//todo Your codes...\r\n }\r\n```\r\nThe above is the most common business process, but there is a \u003ci\u003e\u003cb\u003edisadvantage\u003c/b\u003e\u003c/i\u003e, \r\nwhich must be processed in a unified manner after all requests are completed.\r\nDuring the task, the task repeatedly occupies the memory space. \r\nAt the same time, as long as one is slow, the result processing of all requests will be delayed.\r\n\r\nSo the following method is recommended: Use the $http-\u003ecall() method instead of $http-\u003egetResult()\r\n#### call Method\r\narguments：\r\n\r\n1. $callback: function($requestID,$resVi,$request,$this){....}\r\n2. $maxRetryTimes: Maximum number of retries on error, default 0 (no retry)\r\n\r\n```php\r\n//Pass an anonymous function to Call, the anonymous function will be called after each request is completed\r\n$http-\u003ecall(function($requestId,$resVi,$request,$http){\r\n    $content = $http-\u003egetContent($resVi);\r\n    $info = $http-\u003egetInfo($resVi);\r\n    \r\n    if($info['http_code']===0){\r\n        $http-\u003eretry($requestId);//Network error Add the retry queue\r\n    }\r\n    //todo Your business code...\r\n},3);\r\n\r\n//The second parameter \"3\": the maximum number of retries\r\n```\r\n### ③. retry： Method can be easily added to retry\r\n\r\n    Refer to the code above\r\n\r\n### ④. Add tasks in batches\r\n```php\r\n$http = new Request();\r\n$http-\u003esetTasks([ [url1,$config],[url2,$config] ]);\r\n$http-\u003ecall(function(){\r\n    //....\r\n});\r\n```\r\n\r\n### ⑤. Listen for events\r\n```php\r\n$http-\u003eon( 'before_request', function($this,$每批请求){...} );\r\n$http-\u003eon( 'error', function($info,$curlError,$this){...} )\r\n     -\u003eon( 'item_after_request', function($idx,$resVi,$currentRequest,$this){...} )\r\n\t -\u003ecall(function(){\r\n \t\t\t//.....Your business code...... *\r\n })\r\n```\r\nSupported events\r\n```php\r\n//Triggered on network error\r\nerror: function($info,$curlError,$this)\r\nbefore_request:function($每批请求,$this){...}\r\n//Triggered before each task request\r\nitem_before_request:function($idx,$config,$this){...}\r\n//Triggered after each task responds\r\nitem_after_request:function($idx,$resVi,$currentRequest,$this){...}\r\n```\r\n### ⑥. Download files, especially large files.\r\n     Writing files while downloading will not take up much memory\r\n     Usage as above\r\n```php\r\n//Single task single thread request\r\n $res = (new Request($url,['to_file'=\u003e'file address|ture'])) -\u003e getResult();\r\n //Multi-task concurrent request\r\n(new Request()) -\u003e add($url_1,['to_file'=\u003e'file address|ture'],requestID_1)\r\n                -\u003e add($url_2,['to_file'=\u003e'file address|ture'],requestID_2)\r\n                -\u003ecall( ... )\r\n/Quickly set up tasks in batches\r\n(new Request()) -\u003esetTasks([[url1,['to_file'=\u003e'file/address/or/bool true'],'....']])\r\n-\u003ecall(...)\r\n```\r\n### ⑦ __construct,add The second parameter $config\r\n[Refer to : \"curl_setopt\" arguments](https://www.php.net/manual/zh/function.curl-setopt.php)\r\n\r\nconfig :\r\n```php\r\n    //E.g.: CURLOPT_HTTPHEADER and CURLOPT_ENCODING    \r\n    ['httpheader'=\u003e'...','encoding'=\u003e'...']\r\n    \r\n    // “httpheader” ：CURLOPT_HTTPHEADER removes the value after CURLOPT_, and is not case sensitive。\r\n```\r\n\r\n---\r\n## Please read before using [README.txt](./README.txt)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fimroychen%2Frequest","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fimroychen%2Frequest","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fimroychen%2Frequest/lists"}