中文简体 -------- English
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
composer require iry/request
composer update
\iry\http\Helper::get(url); //Send a Get request
\iry\http\Helper::post(url,$post); //Send a Post request
\iry\http\Helper::put(url,'This is test');
\iry\http\Helper::request(url,$argvs,isPost);
\iry\http\Helper::upload(url,'/test.jpg',['id'=>100]); //upload file=>'test.jpg'
\iry\http\Helper::upload(url,['img'=>'/test2.jpg'],['id'=>101]); //upload
\iry\http\Helper::download(url,$dist); //Download a file
- E.g.
//1. Simple usage
$res = (new Request($url)) -> getResult();
//2. Multi-task concurrent request
//Conventional style
$http = new Request();
//$http->setThread(20);//设置最大20并发,任务总数超过20会分批处理
$http->add(url,config,requestID);//
$http->add(.....);
$thtp->call(function($requestId,$resVi,$currentRequest,$_this){
});
//Usage of chain method
(new Request()) -> setThread(20)->add('url','...')->....->add(url_n,'....')->call(function(){
});
Supported methods:
- getResult :Get all request results, return an array, each element is the result of a task
- call: Send the request and process the result through an anonymous function(Recommended Use)
- setThread:Set the number of threads
- on: Listen for events
- add:Add a request task
- setTasks:Batch setup tasks
use iry\http\Request;
//Send a Get request
$res = (new Request($url)) -> getResult();
//Send a Post request
$res = (new Request($url,['post'=>[...])) -> getResult();
send request
$http = new Request();
//$http->add($url_1,$cfg_1,request_1_id); //request_1_id: Unique ID, used to track the result of each request in multiple requests
$http->add($url_2,$cfg_2,request_2_id)->add(....)->add(....); //Support chain call
//$result = $http->getResult(); or $http->call(..);
Return a two-dimensional array, requestID is the key, and the logic code for processing the result is as follows
$result = $http->getResult();
foreach($result as $itemResult){
$html = $http->getContent($itemResult);
$headerInfo = $http->getInfo($itemResult);
//.....More logic codes......
//todo Your codes...
}
The above is the most common business process, but there is a disadvantage, which must be processed in a unified manner after all requests are completed. During the task, the task repeatedly occupies the memory space. At the same time, as long as one is slow, the result processing of all requests will be delayed.
So the following method is recommended: Use the $http->call() method instead of $http->getResult()
arguments:
- $callback: function($requestID,$resVi,$request,$this){....}
- $maxRetryTimes: Maximum number of retries on error, default 0 (no retry)
//Pass an anonymous function to Call, the anonymous function will be called after each request is completed
$http->call(function($requestId,$resVi,$request,$http){
$content = $http->getContent($resVi);
$info = $http->getInfo($resVi);
if($info['http_code']===0){
$http->retry($requestId);//Network error Add the retry queue
}
//todo Your business code...
},3);
//The second parameter "3": the maximum number of retries
Refer to the code above
$http = new Request();
$http->setTasks([ [url1,$config],[url2,$config] ]);
$http->call(function(){
//....
});
$http->on( 'before_request', function($this,$每批请求){...} );
$http->on( 'error', function($info,$curlError,$this){...} )
->on( 'item_after_request', function($idx,$resVi,$currentRequest,$this){...} )
->call(function(){
//.....Your business code...... *
})
Supported events
//Triggered on network error
error: function($info,$curlError,$this)
before_request:function($每批请求,$this){...}
//Triggered before each task request
item_before_request:function($idx,$config,$this){...}
//Triggered after each task responds
item_after_request:function($idx,$resVi,$currentRequest,$this){...}
Writing files while downloading will not take up much memory
Usage as above
//Single task single thread request
$res = (new Request($url,['to_file'=>'file address|ture'])) -> getResult();
//Multi-task concurrent request
(new Request()) -> add($url_1,['to_file'=>'file address|ture'],requestID_1)
-> add($url_2,['to_file'=>'file address|ture'],requestID_2)
->call( ... )
/Quickly set up tasks in batches
(new Request()) ->setTasks([[url1,['to_file'=>'file/address/or/bool true'],'....']])
->call(...)
Refer to : "curl_setopt" arguments
config :
//E.g.: CURLOPT_HTTPHEADER and CURLOPT_ENCODING
['httpheader'=>'...','encoding'=>'...']
// “httpheader” :CURLOPT_HTTPHEADER removes the value after CURLOPT_, and is not case sensitive。