原理:使用curl_init()创建多个请求实例,再使用curl_multi_init()批量执行创建的多个请求实例。
文件1:curl.php
<?php
$threads=500;//并发请求次数
$url='http://blog.cn/index.php?';//请求的url
//创建一个未定义的curl句柄数组
$ch=array();
//创建批处理cURL的句柄
$mh = curl_multi_init();
//创建并发请求次数个url用于后面给curl分配
for ($i=0; $i <$threads ; $i++) {
//有多少请求,创建多少curl会话
$ch[$i]=curl_init();
curl_setopt($ch[$i], CURLOPT_URL, $url.rand(1,1000));//随机参数,避免缓存
curl_setopt($ch[$i], CURLOPT_HEADER, 0);
//创建的会话分配给curl批处理句柄
curl_multi_add_handle($mh,$ch[$i]);
}
$running=null;
//所有的curl会话分配给$mh这个curl批量处理句柄来执行
do {
usleep(10000);
curl_multi_exec($mh,$running);
} while ($running > 0);
//关闭已经创建的会话句柄
for ($i=0; $i <$threads ; $i++) {
curl_multi_remove_handle($mh, $ch[$i]);
}
//关闭批处理句柄
curl_multi_close($mh);
?>文件2:index.php
<?php
file_put_contents('1.txt',date('Y-m-d H:i:s',time())."\r\n",FILE_APPEND );
?>文件3:1.txt
2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:06 2017-07-30 22:30:07 2017-07-30 22:30:07 2017-07-30 22:30:07 2017-07-30 22:30:07 2017-07-30 22:30:07 2017-07-30 22:30:07 2017-07-30 22:30:07 2017-07-30 22:30:07 2017-07-30 22:30:07 2017-07-30 22:30:07 2017-07-30 22:30:07 2017-07-30 22:30:07 2017-07-30 22:30:07 2017-07-30 22:30:07 2017-07-30 22:30:07 2017-07-30 22:30:07 2017-07-30 22:30:07 2017-07-30 22:30:07 2017-07-30 22:30:07
基本同时请求的还是比较多的,受带宽和CPU影响,多线程的访问,并不是一定会同时,线程是否立即执行决定权是CPU
项目需要使用websocket推送最新订单,客户服务器非linux不支持swoole,因此使用原生,直接上代码(1).PHP服务端<?php ini_set('error_reporting', E_ALL ^ E_NOTICE); ini_set...
如果想在windows中执行php,并且让php脚本在后台运行,可以用下面的cmd命令start /b php D:\wwwroot\default\demo1\run.php例如上面的命令意思后台运行run.php,如果想用php编写异步代码: ...
<?php $member = new class { public function getInfo() { ...
php7新增的特性(1).强制限制只能返回一种类型<?php class task { } //must return an integer function add(): int { &nb...
在使用ftok生成ipc进程通信key尝试将第二个参数项目标识符传入字符串报错:PHP Warning: ftok(): Project identifier is invalid,查阅资料发现第二个字符串只能是1个字符串,长度为1....
有时候我们需要爬一个接口,但是这个接口需要很多参数,包括header和cookie要去编写,使用php curl模拟实在太慢。我们可以通过浏览器的network来复制请求为curl命令。例如我需要模拟请求接口地址:https://www.xkmz.cc/Ajax/Debug/delly,我们只需要在...