惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
Vercel News
Vercel News
F
Fortinet All Blogs
月光博客
月光博客
G
Google Developers Blog
博客园 - Franky
GbyAI
GbyAI
The Cloudflare Blog
I
InfoQ
雷峰网
雷峰网
WordPress大学
WordPress大学
罗磊的独立博客
大猫的无限游戏
大猫的无限游戏
T
The Blog of Author Tim Ferriss
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 聂微东
小众软件
小众软件
腾讯CDC
B
Blog
量子位
V
V2EX
S
SegmentFault 最新的问题
Google DeepMind News
Google DeepMind News

博客园 - Cheney Shen

如果将 macOS Mojave 降级为 macOS Sierra 或者更低版本 pip源提示“not a trusted or secure host” 解决 Window系统下MongoDB安装及远程访问 Windows下安装appium桌面版和命令行版 Command 'java' not found during running appium sudo npm install -g cnpm --registry=https://registry.npm.taobao.org Pycharm 中错误ImportError: No module named appium curl如何发起DELETE/PUT请求?(备忘) Ubuntu 11.10下安装配置Zend Studio 9.0破解版详细步骤 iphone开源汇总(转) iphone开源汇总(转) python中的cursor[备忘] 在main函数执行之前和执行之后执行的方法 Intel苹果电脑Mac+Win+Linux多重系统启动(+公用分区)终极解决方案(备忘) IGT笔试题,正整数N等于M个不同的正整数之和的问题 如何通过一个结构体成员变量的地址找到该结构体的首地址?[备忘] 为特定应用程序关闭恢复窗口功能 [Mac OS X Lion][转] System Events, Key Code and Keystroke from Doug's AppleScripts Mac下Perl脚本如何运行
CURL并发测试POST和DELETE请求
Cheney Shen · 2012-01-12 · via 博客园 - Cheney Shen

<?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);

?>