原理:使用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
<?PHP $a=0.5*100; if(is_int($a)){ echo'int'; } else{ echo'not int'; } ...
首先看看以下代码:代码1:<?php $a=0.1; $b=0.7; if($a+$b==0.8) { echo "1"; } else{ echo "2"; } ?>代码2:<?php &n...
if($_SERVER['REQUEST_METHOD'] == 'POST') { echo('This is post '); } elseif ($_SERVER['...
001源码:/* * $xml_str是xml字符串 */ function xmltoarray($xml_str) { //禁止XML实体扩展攻击 libxml_disable_entity_loader(true); //拒绝包含...
xmlrpc协议是通过http请求xml数据进行通信。webservice中和它相同的是soap。soap调用的确很简单,但是创建wsdl太繁琐,效率低下。xmlrpc很好的解决这个问题。(1).创建xmlrpc服务端(求和函数api)function getSum($method,$ar...
<?php $member = new class { public function getInfo() { ...