PHP code example of paulzi / multicurl
1. Go to this page and download the library: Download paulzi/multicurl 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/ */
paulzi / multicurl example snippets
function generateCurl($url)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_ENCODING, '');
return $curl;
}
// usage MultiCurl
$request = new MultiCurlRequest();
$request->curl = generateCurl('https://google.com');
$request->onSuccess = function($request, $response, $content) {
var_dump('Success', $content);
};
$request->onError = function($request, $response, $content, $errCode, $errMsg) {
var_dump('Error', $errMsg);
};
$loader = new MultiCurl();
$loader->add($request);
$loader->run();
// usage MultiCurlQueue
$request1 = new MultiCurlRequest();
$request1->curl = generateCurl('https://google.com');
$request1->onSuccess = function($request, $response, $content) {
var_dump('Request1 Success', $content);
};
$request1->onRetry = function($request, $response, $content, $errCode, $errMsg) {
var_dump('Request1 Retry', $errMsg);
};
$request1->onError = function($request, $response, $content, $errCode, $errMsg) {
var_dump('Request1 Error', $errMsg);
};
$request2 = new MultiCurlRequest();
$request2->curl = generateCurl('http://none.noexist');
$request2->onSuccess = function($request, $response, $content) {
var_dump('Request2 Success', $content);
};
$request2->onRetry = function($request, $response, $content, $errCode, $errMsg) {
var_dump('Request2 Retry', $errMsg);
};
$request2->onError = function($request, $response, $content, $errCode, $errMsg) {
var_dump('Request2 Error', $errMsg);
};
$loader = new MultiCurlQueue();
$loader->threads = 2;
$loader->retry = 3;
$loader->run([$request1, $request2]);