Created
March 17, 2015 08:03
-
-
Save guoxiangke/4025ef2a52c953b2ecb8 to your computer and use it in GitHub Desktop.
Multiple cURL Requests with PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="zh-cn"> | |
<head> | |
<meta charset="utf-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<title>Multiple cURL Requests with PHP</title> | |
<!-- Bootstrap --> | |
<link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.2/css/bootstrap.min.css"> | |
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries --> | |
<!-- WARNING: Respond.js doesn't work if you view the page via file:// --> | |
<!--[if lt IE 9]> | |
<script src="http://cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script> | |
<script src="http://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script> | |
<![endif]--> | |
</head> | |
<body> | |
<div class="container"> | |
<h1>Hello Multiple cURL Requests with PHP</h1> | |
<br> | |
<br> | |
<div class="alert alert-info" role="alert"> | |
<p>Browser: <script>document.write(navigator.appName)</script></p> | |
<p>Version: <script>document.write(navigator.appVersion)</script></p> | |
</div> | |
<?php | |
/** | |
* @author [email protected] | |
* @url http://www.trackself.com/code/curl_multi.html | |
* @url http://www.phpied.com/simultaneuos-http-requests-in-php-with-curl/ | |
* Tools | |
* https://www.dotcom-tools.com/website-speed-test.aspx | |
* http://gtmetrix.com/ | |
* http://tools.pingdom.com/fpt/ | |
*/ | |
function multiRequest($data, $options = array()) { | |
// array of curl handles | |
$curly = array(); | |
// data to be returned | |
$result = array(); | |
// multi handle | |
$mh = curl_multi_init(); | |
// loop through $data and create curl handles | |
// then add them to the multi-handle | |
foreach ($data as $id => $d) { | |
$curly[$id] = curl_init(); | |
$url = (is_array($d) && !empty($d['url'])) ? $d['url'] : $d; | |
curl_setopt($curly[$id], CURLOPT_URL, $url); | |
curl_setopt($curly[$id], CURLOPT_HEADER, 0); | |
curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1); | |
// post? | |
if (is_array($d)) { | |
if (!empty($d['post'])) { | |
curl_setopt($curly[$id], CURLOPT_POST, 1); | |
curl_setopt($curly[$id], CURLOPT_POSTFIELDS, $d['post']); | |
} | |
} | |
// extra options? | |
if (!empty($options)) { | |
curl_setopt_array($curly[$id], $options); | |
} | |
curl_multi_add_handle($mh, $curly[$id]); | |
} | |
// execute the handles | |
$running = null; | |
$begin = getMillisecond(); | |
do { | |
curl_multi_exec($mh, $running); | |
} while($running > 0); | |
// get content and remove handles | |
foreach($curly as $id => $c) { | |
$result[$id] = curl_multi_getcontent($c); | |
// if($id==1) | |
// print_r($result['5']); | |
$return[$id]['end'] = getMillisecond(); | |
$return[$id]['begin'] = $begin; | |
$return[$id]['during'] = $return[$id]['end'] - $return[$id]['begin']; | |
//get http status codes | |
$return[$id]['code'] = curl_getinfo($c, CURLINFO_HTTP_CODE); | |
curl_multi_remove_handle($mh, $c); | |
} | |
// all done | |
curl_multi_close($mh); | |
return array($return); | |
} | |
function getMillisecond() { | |
list($s1, $s2) = explode(' ', microtime()); | |
return (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000); | |
} | |
if (isset($_POST['url']) && isset($_POST['times'])) { | |
$times = $_POST['times']; | |
$urls = array(); | |
while ( $times >0) { | |
$urls[] = $_POST['url']; | |
$times--; | |
} | |
$result = multiRequest($urls); | |
} | |
if(isset($result)) : | |
?> | |
<h3>Test Result Summary <a href="#form">↓</a> </h3> | |
<?php | |
$temps = $result[0]; | |
unset($temps[0]); | |
$max = -9999999; //will hold max val | |
$min = 9999999; //will hold max val | |
$total = 0; | |
foreach ($temps as $key => $v) { | |
if($v['during']>$max) $max = $v['during']; | |
if($v['during']<$min) $min = $v['during']; | |
$total += $v['during']; | |
} | |
$count = count($temps); | |
$average = $total/$count; | |
?> | |
<table class="table"> | |
<thead> | |
<th>Total</th> | |
<th>Maxtime(s)</th> | |
<th>Mintime(s)</th> | |
<th>Average Time(s)</th> | |
</thead> | |
<tbody> | |
<tr class="info"> | |
<td><?php echo $count;?></td> | |
<td><?php echo $max/1000;?></td> | |
<td><?php echo $min/1000;?></td> | |
<td><?php echo $average/1000;?></td> | |
</tr> | |
</tbody> | |
</table> | |
<h3>Details<a href="#form">↓</a> </h3> | |
<table class="table table-hover"> | |
<thead> | |
<tr> | |
<th>#</th> | |
<th>During(s)</th> | |
<th>Code(http status codes return)</th> | |
<th>Begin(time begin request s)</th> | |
<th>End(time end of request s)</th> | |
</tr> | |
</thead> | |
<tbody> | |
<?php foreach ($result[0] as $key => $value) { | |
?> | |
<tr <?php if($value['code']!='200' || $key == 0) echo 'class="danger"';?>> | |
<th><?php echo $key;?></th> | |
<th><?php echo $value['during']/1000;?></th> | |
<th><?php echo $value['code'];?></th> | |
<th><?php echo $value['begin']/1000;?></th> | |
<th><?php echo $value['end']/1000;?></th> | |
</tr> | |
<?php | |
}?> | |
</tbody> | |
</table> | |
<?php endif;?> | |
<form action="curl.php" method="post" id="form"> | |
<div class="input-group input-group-lg"> | |
<span class="input-group-addon" id="sizing-addon1">Reuest</span> | |
<input type="url" name="url" class="form-control" placeholder="Url" aria-describedby="sizing-addon1" value="<?php if (isset($_POST['url'])) echo $_POST['url']; ?>"> | |
</div> | |
<br> | |
<div class="input-group input-group-lg"> | |
<span class="input-group-addon" id="sizing-addon1">Times </span> | |
<input type="number" name="times" class="form-control" placeholder="Number of requests" aria-describedby="sizing-addon1" value="<?php if (isset($_POST['times'])) echo $_POST['times']; ?>"> | |
</div> | |
<br> | |
<input type="submit" value="Request" class="btn btn-default btn-lg btn-danger"/> | |
</form> | |
</div> | |
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> | |
<script src="http://cdn.bootcss.com/jquery/1.11.2/jquery.min.js"></script> | |
<!-- Include all compiled plugins (below), or include individual files as needed --> | |
<script src="http://cdn.bootcss.com/bootstrap/3.3.2/js/bootstrap.min.js"></script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment