CURL并发测试POST和DELETE请求

<?php
$follower_ids = range(1000, 2000, 1);
$prefix = "http://a.b.com/123/following";
$mh = curl_multi_init();
$argv1 = $argv[1];
if (!empty($argv1) && is_string($argv1)) {
switch (strtolower($argv1)) {
case 'post':
foreach ($follower_ids as $follower_id) {
$conn[$follower_id] = curl_init($prefix);//初始化各个子连接
curl_setopt($conn[$follower_id], CURLOPT_RETURNTRANSFER, 1);//不直接输出到浏览器
    curl_setopt($conn[$follower_id], CURLOPT_POST, 1);
   curl_setopt($conn[$follower_id], CURLOPT_POSTFIELDS, "fuid={$follower_id}");
   curl_multi_add_handle ($mh,$conn[$follower_id]);//加入多处理句柄
}
break;
case 'delete':
foreach ($follower_ids as $follower_id) {
   $conn[$follower_id] = curl_init("{$prefix}?fuid={$follower_id}");//初始化各个子连接
curl_setopt($conn[$follower_id], CURLOPT_RETURNTRANSFER, 1);//不直接输出到浏览器
   curl_setopt($conn[$follower_id], CURLOPT_CUSTOMREQUEST, "DELETE");
   curl_setopt($conn[$follower_id], CURLOPT_POSTFIELDS, "fuid={$follower_id}");
   curl_multi_add_handle ($mh,$conn[$follower_id]);//加入多处理句柄
}
break;
default:
die("Must provide the method!");
}
}
$active = 0;//连接数
do {
    do{
        //这里$active会被改写成当前未处理数
        //全部处理成功$active会变成0
        $mrc  = curl_multi_exec($mh, $active);
        //这个循环的目的是尽可能的读写,直到无法继续读写为止(返回CURLM_OK)
        //返回(CURLM_CALL_MULTI_PERFORM)就表示还能继续向网络读写
    }while($mrc==CURLM_CALL_MULTI_PERFORM);
  //如果一切正常,那么我们要做一个轮询,每隔一定时间(默认是1秒)重新请求一次
  //这就是curl_multi_select的作用,它在等待过程中,如果有就返回目前可以读写的句柄数量,以便
  //继续读写操作,0则没有可以读写的句柄(完成了)
} while ($mrc==CURLM_OK&& $active &&curl_multi_select($mh)!=-1);//直到出错或者全部读写完毕
if ($mrc != CURLM_OK) {
    print "Curl multi read error $mrc/n";
}
 // retrieve data
foreach ($follower_ids as $follower_id) {
    if (($err = curl_error($conn[$follower_id])) == '') {
        $res[$follower_id]=curl_multi_getcontent($conn[$follower_id]);
    } else {
        print "Curl error on handle $follower_id: $err/n";
    }
    curl_multi_remove_handle($mh,$conn[$follower_id]);
    curl_close($conn[$follower_id]);
}
curl_multi_close($mh);
print_r($res);
?> 

你可能感兴趣的:(delete)