PHP code example of aalfiann / parallel-request-php
1. Go to this page and download the library: Download aalfiann/parallel-request-php 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/ */
aalfiann / parallel-request-php example snippets
use \aalfiann\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());
use \aalfiann\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());
use \aalfiann\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());
// 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()
// 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);
// 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);
// 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
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.