wiki'd

by JoKeru

Run parallel cURL requests using PHP and BASH

[cc lang='php']
<?php< p ?>

// my linux curl
function my_curl (\$i, \$url, \$post_data, \$return=0, \$auth='', \$proxy='', \$timeout=5) {
\$curl="/usr/bin/curl -s";
if (\$post_data!='') \$curl.=" --data '\$post_data'";
if (\$timeout!='') \$curl.=" --connect-timeout \$timeout";
if (\$auth!='') {
list(\$user,\$pass)=explode(':',\$auth);
\$curl.=" --basic --user \$user:\$pass";
}
if (\$proxy!='') {

list(\$proxy_ip,\$proxy_port,\$proxy_user,\$proxy_pass)=explode(':',\$proxy);
\$curl.=" --proxy \$proxy_ip:\$proxy_port --proxy-user \$proxy_user:\$proxy_pass";
}
if (\$return==0) {
\$curl.=" \$url >/dev/null 2>/dev/null &";
} else {
\$curl.=" \$url >return-\$i 2>/dev/null &";
}
return("\$curl\n");
}

\$main_start=microtime(true);
echo "Starting main @ \$main_start
";

// cleanup return files if any
foreach (glob("return-*") as \$filename) unlink(\$filename);

// start the requests fire
\$main_timeout = 2; # main script timeout in seconds
\$max = 200; # number of requests to send
\$return = 1; # 1 / 0 -> enable / disable return transfer
\$auth = ''; # user:pass for password protected folders
\$proxy = ''; # proxy info PROXY_IP:PROXY_PORT:PROXY_USER:PROXY_PASS
\$curl_timeout = 1; # curl timeout value in seconds
//
\$to_run='';
for (\$i=0; \$i\<\$max; \$i++) {
\$url='http://10.20.30.40/post.php';
\$post_data="var=web-\$i";
\$to_run.=my_curl(\$i, \$url, \$post_data, \$return, \$auth, \$proxy, \$timeout);
}
file_put_contents('runner.sh',\$to_run);
echo "Starting requests to all \$max scripts @ ".microtime(true).'
';
shell_exec('/bin/bash runner.sh');
echo "Finished requests to all \$max scripts @ ".microtime(true).'
';

// wait for requests to finish
\$finished=false;
while(!\$finished) {
\$run=shell_exec('ps -ef | grep -v grep | grep -c curl');
if (\$run>0) {
\$now=microtime(true);
if (\$now-\$main_start>\$main_timeout) {
\$finished=true;
echo 'Main Timeout: only '.(\$max-\$run).' scripts finished running @ '.\$now.'
';
} else {
sleep(.1);
}
}
if (\$run==0) {
\$finished=true;
echo "
Finished running \$max scripts @ ".microtime(true).'
';
}
}

// process output if (\$return==1) { \$out=array(); foreach (glob("return-\*") as \$filename) \$out[]=file\_get\_contents(\$filename); echo(' '); print_r($out); echo(' '); } else { echo "No return values "; } echo ' * * * * * Exiting main @ '.microtime(true).' '; ?\> [/cc]

Comments