原理:使用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
<!doctype html> <html> <head> <meta charset="utf-8"> <title>demo</title> </head> <bod...
array_merge是最常用的数组合并方法,+号同样也可以,但是却有很大不同。array_merge遇到相同字符串key,后面数组的key会覆盖前面数组的key,+号正好相反。$a = [ 'one' => 'A on...
在编写多进程的实例中我在每个进程中使用如下代码://调用等待信号的处理器 while (true) { pcntl_signal_dispatch(); }开启5个进程,cpu直接100%修正之后的代码://调用等待信号的处理器 while&...
<?php $member = new class { public function getInfo() { ...
<?php /** * @throws Exception */ function curl() { throw new \Exception('err...
有时候我们需要爬一个接口,但是这个接口需要很多参数,包括header和cookie要去编写,使用php curl模拟实在太慢。我们可以通过浏览器的network来复制请求为curl命令。例如我需要模拟请求接口地址:https://www.xkmz.cc/Ajax/Debug/delly,我们只需要在...