Wallogit.com
2017 © Pedro Peláez
Multi Curl request with batch and parallel request
Multi Curl with batch and parallel request. As the name suggest, you can make a batch request or parallel request at once., (*1)
Will split array data provided, and request part by part to minimize workload during request instead of requesting at once. Defining how much the number of data per request is at your own choices. The batch request simply divided array of url(s) part by part and doing the request one by one until finish. While waiting for next queue of request, you can provide callback for each batch to execute., (*2)
Passed in the whole set of array data and will request all at once., (*3)
Library can be installed using composer, just require it :, (*4)
composer require arch/multicurl
1) Inside file just include Arch\MultiCurl\MultiCurl at the top of page:, (*5)
2) Instantiate library and call the available method :, (*6)
<?php
use Arch\MultiCurl\MultiCurl;
$baseUrl = 'https://maps.googleapis.com/maps/api/geocode/json?';
$key = '&key=Google_Map_Keys';
$urls = array(
$baseUrl . 'latlng=3.114536,101.659844'.$key,
$baseUrl . 'latlng=3.084217,101.595965'.$key,
$baseUrl . 'latlng=3.064248,101.437435'.$key,
$baseUrl . 'latlng=3.114536,101.659844'.$key
);
$perRequest = 3; // 3 endpoints per request
$curlMulti = new MultiCurl( $urls, $perRequest );
$curlMulti->execute( function ( $data ) { // execute curl with callback for each request
echo count($data);
});
1) new MultiCurl( $urls, $perRequest ) accept two parameter for initial configuration., (*7)
2) Change default operation for curl either to batch request or all at once by calling setBatchOperation() method, default is true for batch request operation :, (*8)
$curlMulti = new MultiCurl( $urls, $perRequest ); $curlMulti->setBatchOperation( false ); $result = $curlMulti->execute(); print_r( $result );
3) Providing callback in execute( fn(x) ) for batch request whenever its finish executing the request for each batch. As example, we have 3 batch requests, and the callback provided will run 3 times for each batch with the result it received. This callback have 1 arguments for result set
receive from the endpoints. This callback is optional., (*9)
// As example, we have 3 batch requests
$curlMulti = new MultiCurl( $urls, $perRequest );
$curlMulti->execute( function ( $data ) { // execute curl with callback for each request, will run 3 times
echo count($data);
});