PHP code example of iry / request

1. Go to this page and download the library: Download iry/request library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

iry / request example snippets


use \iry\http\Helper;
Helper::get(url);             //发送一个Get请求
Helper::post(url,$post);      //发送一个post请求
Helper::put(url,'This is test');      //发送一个post请求
Helper::request(url,$argvs,isPost);
Helper::upload(url,'/test.jpg',['id'=>100]);//上传文件
Helper::upload(url,['img'=>'/test2.jpg'],['id'=>101]);//上传文件
Helper::download(url,$dist);  //下载一个文件

//1.简单用法
$res = (new Request($url)) -> getResult();
//2. 多任务并发请求
//常规写法
$http = new Request();
//$http->setThread(20);//设置最大20并发,任务总数超过20会分批处理
$http->add(url,config,requestID);//
$http->add(.....);
$thtp->call(function($requestId,$resVi,$currentRequest,$_this){

});
//连贯写法
(new Request()) -> setThread(20)->add('url','...')->....->add(url_n,'....')->call(function(){

});

use iry\http\Request;

//发送一个Get请求
$res = (new Request($url)) -> getResult();

//发送一个Post请求
$res = (new Request($url,['post'=>[...])) -> getResult();

$http = new Request();
//$http->add($url_1,$cfg_1,request_1_id);  request_1_id 唯一ID 多个请求是用来跟踪请求结果用的
$http->add($url_2,$cfg_2,request_2_id)->add(....)->add(....); //支持连贯调用
//$result = $http->getResult(); or $http->call(..);

$result = $http->getResult();
foreach($result as $itemResult){
	$html = $http->getContent($itemResult);//获取到结果
 	$headerInfo = $http->getInfo($itemResult);//获取到头部信息
 	//.....更多的逻辑代码......
 	//todo 您的业务代码...
 }

//传一个匿名函数给Call 每一个请求完成之后会调用该匿名函数
$http->call(function($requestId,$resVi,$request,$http){
    $content = $http->getContent($resVi);
    $info = $http->getInfo($resVi);
    
    if($info['http_code']===0){
        $http->retry($requestId);//网络错误 加入重试队列
    }
    //todo 你的业务代码...
},3);

//第二个参数 3 最大重试次数

$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(){
 			//.....更多的逻辑代码...... *
 })

//网络错误是触发
error: function($info,$curlError,$this)
before_request:function($每批请求,$this){...}
//每个任务请求前触发
item_before_request:function($idx,$config,$this){...}
//每个任务请求对方响应完后触发
item_after_request:function($idx,$resVi,$currentRequest,$this){...}

//单线程获取
 $res = (new Request($url,['to_file'=>'file address|ture'])) -> getResult();
 //多线程获取
(new Request()) -> add($url_1,['to_file'=>'file address|ture'],requestID_1)
                -> add($url_2,['to_file'=>'file address|ture'],requestID_2)
                ->call( ... )
//多线程批量设置任务
(new Request()) ->setTasks([[url1,['to_file'=>'file/address/or/bool true'],'....']])
->call(...)

    //如需要设置 CURLOPT_HTTPHEADER 和 CURLOPT_ENCODING    
    ['httpheader'=>'...','encoding'=>'...']
    //键 “httpheader” 为:CURLOPT_HTTPHEADER 去除 CURLOPT_之后的值,且不区分大小写。