aalfiann / parallel-request-php
一个用于创建多个并行请求(非阻塞)的 PHP 类。
1.4.2
2019-05-10 19:39 UTC
Requires
- php: >=5.4
README
一个用于创建多个并行请求(非阻塞)的 PHP 类。
安装
通过 Composer 安装此包。
composer require "aalfiann/parallel-request-php:^1.0"
发送 GET 请求的用法
这将静默发送 GET 请求,不输出任何响应。
use \aalfiann\ParallelRequest; require_once ('vendor/autoload.php'); $req = new ParallelRequest; // You can set request with simple array like this $req->request = ['https://jsonplaceholder.typicode.com/posts/1','https://jsonplaceholder.typicode.com/posts/2']; // Or you can set request array with addRequest() function for($i=1;$i<3;$i++){ $req->addRequest('https://jsonplaceholder.typicode.com/posts/'.$i); } $req->options = [ CURLOPT_NOBODY => false, CURLOPT_HEADER => false, CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false ]; $req->send();
如果您想查看响应
echo var_dump($req->send()->getResponse());
发送 POST 请求的用法
use \aalfiann\ParallelRequest; require_once ('vendor/autoload.php'); $req = new ParallelRequest; // You can set post request with array formatted like this $req->request = [ [ 'url' => 'https://jsonplaceholder.typicode.com/posts', 'post' => [ 'title' => 'foo 1', 'body' => 'bar 1', 'userId' => 1 ] ], [ 'url' => 'https://jsonplaceholder.typicode.com/posts', 'post' => [ 'title' => 'foo 2', 'body' => 'bar 2', 'userId' => 2 ] ] ]; // Or you can set request array with addRequest() function $req->setRequest(array()); //==> cleanup any request first (optional) for($i=1;$i<3;$i++){ $req->addRequest('https://jsonplaceholder.typicode.com/posts',[ 'title' => 'foo '.$i, 'body' => 'bar '.$i, 'userId' => $i ]); } $req->encoded = true; $req->options = [ CURLOPT_NOBODY => false, CURLOPT_HEADER => false, CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false ]; echo var_dump($req->send()->getResponse());
发送自定义请求的用法
如果您想发送自定义请求,如 PUT、PATCH、DELETE 等。
只需添加 CURLOPT_CUSTOMREQUEST => 'PUT'
。
use \aalfiann\ParallelRequest; require_once ('vendor/autoload.php'); $req = new ParallelRequest; // You can set post request with array formatted like this $req->request = [ [ 'url' => 'https://jsonplaceholder.typicode.com/posts/1', 'post' => [ 'id' => 1, 'title' => 'foo 1', 'body' => 'bar 1', 'userId' => 1 ] ], [ 'url' => 'https://jsonplaceholder.typicode.com/posts/2', 'post' => [ 'id' => 2, 'title' => 'foo 2', 'body' => 'bar 2', 'userId' => 1 ] ] ]; // Or you can set request array with addRequest() function $req->setRequest(array()); //==> cleanup any request first (optional) for($i=1;$i<3;$i++){ $req->addRequest('https://jsonplaceholder.typicode.com/posts/'.$i,[ 'id' => $i, 'title' => 'foo '.$i, 'body' => 'bar '.$i, 'userId' => $i ]); } $req->encoded = true; $req->options = [ CURLOPT_NOBODY => false, CURLOPT_HEADER => false, CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false, CURLOPT_CUSTOMREQUEST => 'PUT' ]; echo var_dump($req->send()->getResponse());
链式使用
您还可以创建链。
use \aalfiann\ParallelRequest; require_once ('vendor/autoload.php'); $req = new ParallelRequest; // example simple request echo $req->setRequest('http://jsonplaceholder.typicode.com/posts')->send()->getResponse(); // example complicated request echo var_dump($req-> ->setRequest(array()) //==> cleanup any request first (optional) ->addRequest('https://jsonplaceholder.typicode.com/posts',[ 'title' => 'foo 1', 'body' => 'bar 1', 'userId' => 1 ]) ->addRequest('https://jsonplaceholder.typicode.com/posts/1') ->addRequest('https://jsonplaceholder.typicode.com/posts',[ 'title' => 'foo 2', 'body' => 'bar 2', 'userId' => 2 ]) ->addRequest('https://jsonplaceholder.typicode.com/posts',[ 'userId' => 3 ],false) ->setOptions([ CURLOPT_NOBODY => false, CURLOPT_HEADER => false, CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false ]) ->setEncoded()->setHttpInfo()->send()->getResponse());
如何调试
要完全发送请求,我们需要知道请求中发生了什么
// to see the detail of request $req->httpInfo = 'detail'; // or use chain function like this $req->setHttpInfo('detail') // or if you want only http code info $req->setHttpInfo()
setRequest()、addRequest() 和 addRequestRaw() 的区别
这三个函数是构建请求所必需的。
setRequest()
是在您使用之前需要先创建字符串 / 格式化数组。
示例
// build the formatted array first. // You are able to create more complex form-data or raw data. $request = [ 'https://jsonplaceholder.typicode.com/posts/1', [ 'url' => 'https://jsonplaceholder.typicode.com/posts/2', 'post' => [ 'title' => 'foo 2', 'body' => 'bar 2', 'userId' => 2 ] ], [ 'url' => 'https://jsonplaceholder.typicode.com/posts', 'post' => json_encode([ 'title' => 'foo 3', 'body' => 'bar 3', 'userId' => 3 ]) ] ]; // then you can use setRequest() function $req->setRequest($request);
addRequest()
是您可以在即时创建字符串 / 格式化数组。
示例
// url only $req->addRequest('https://jsonplaceholder.typicode.com/posts/1'); // send request with form-data $req->addRequest('https://jsonplaceholder.typicode.com/posts',[ 'title' => 'foo 2', 'body' => 'bar 2', 'userId' => 2 ]); // send request with data parameter on url $req->addRequest('https://jsonplaceholder.typicode.com/posts',[ 'userId' => 3 ],false);
addRequestRaw()
是您可以通过请求发送原始数据。
示例
// url with raw json data // setEncoded(true) or $req->encoded = true will not work, so you have to encoded this by yourself $req->addRequestRaw('https://jsonplaceholder.typicode.com/posts',json_encode([ 'title' => 'foo 2', 'body' => 'bar 2', 'userId' => 2 ])); // http header is required to send raw json data $req->options = [ CURLOPT_NOBODY => false, CURLOPT_HEADER => false, CURLOPT_HTTPHEADER => ['Content-type: application/json; charset=UTF-8'], CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false ];
函数列表
- setRequest($request) 用于创建请求 URL(字符串或包含 post 数据的数组)。
- addRequest($url,$params=array(),$formdata=true) 用于创建请求 URL(字符串或包含参数数据的数组)。
- addRequestRaw($url,$data) 用于创建包含原始数据的请求 URL。参数数据默认未编码。
- setOptions($options=array()) 用于设置 CURLOPT 的选项。
- setHttpStatusOnly($httpStatusOnly=false) 如果设置为 true,则输出响应将转换为 HTTP 状态码。
- setHttpInfo($httpInfo=false) 如果设置为 true,则输出响应将显示 HTTP 信息状态。设置为 "detail" 以获取更多信息。
- setDelayTime($time=10000) 是 CPU 休息的延迟执行时间。默认为 10000(10ms)微秒。
- setEncoded($encoded=true) 用于编码 post 数据。默认的 post 数据未编码,因此您可以创建更复杂的数据请求。
- send() 是 curl 发送请求(静默,没有任何输出)。
- getResponse() 用于获取输出响应(返回数据可以是字符串或数组)。
- getResponseJson() 用于以 JSON 格式获取输出响应。
注意
- 如果您只创建单个请求,则响应将返回字符串。
- 此类基本上使用
curl_multi_exec()
函数。 - 如果您未指定
$req->options
,则默认使用[CURLOPT_HEADER => false,CURLOPT_RETURNTRANSFER => true]
。