编程 php curl并发代码

2024-11-18 01:45:03 +0800 CST views 2703

#关于php curl的并发代码

1.先写个核心的时间的文件名为.test.php 代码如下

list($usec, $sec) = explode(" ", microtime());
$str= date("Y-m-d H:i:s.").(float)$usec;
file_put_contents("./test.txt",$str."\n",FILE_APPEND);
echo $str;

2,curl并发核心代码

$urls=[            
            "http://127.0.0.10/tp5/pay/public/test.php",
            "http://127.0.0.10/tp5/pay/public/test.php",
            "http://127.0.0.10/tp5/pay/public/test.php",
            "http://127.0.0.10/tp5/pay/public/test.php",
            "http://127.0.0.10/tp5/pay/public/test.php",
            "http://127.0.0.10/tp5/pay/public/test.php",
            "http://127.0.0.10/tp5/pay/public/test.php",
            "http://127.0.0.10/tp5/pay/public/test.php",
            "http://127.0.0.10/tp5/pay/public/test.php",
            "http://127.0.0.10/tp5/pay/public/test.php",
            "http://127.0.0.10/tp5/pay/public/test.php",
            "http://127.0.0.10/tp5/pay/public/test.php",
            "http://127.0.0.10/tp5/pay/public/test.php",
            "http://127.0.0.10/tp5/pay/public/test.php",
            "http://127.0.0.10/tp5/pay/public/test.php",
            "http://127.0.0.10/tp5/pay/public/test.php",
        ];
        $handle=[];
        $running = 0;
        $wait_usec = intval(30);
        $mh = curl_multi_init(); // multi curl handler
        foreach($urls as $key=>$url) {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return don't print
            curl_setopt($ch, CURLOPT_TIMEOUT, 30);
            curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Linux; U; Android 6.4.4; zh-CN; Che1-CL10 Build/Che1-CL10) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 UCBrowser/10.10.0.800 U3/0.8.0 Mobile Safari/534.30');
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // 302 redirect
            //if ($post_data[$key]){curl_setopt ( $ch, CURLOPT_POSTFIELDS, $post_data[$key]);}
            curl_setopt($ch, CURLOPT_MAXREDIRS, 7);
            curl_multi_add_handle($mh, $ch); // 把 curl resource 放进 multi curl handler 里
            $handle[$key] = $ch;
        }
        /* 执行 */
        do {
            curl_multi_exec($mh, $running);
            if ($wait_usec > 0) /* 每个 connect 要间隔多久 */
                usleep($wait_usec); // 250000 = 0.25 sec
        } while ($running > 0);
        foreach($handle as $i => $ch) {
            $content  = curl_multi_getcontent($ch);
            if (curl_errno($ch) == 0){
                 dump($content);
            }
        }
        /* 移除 handle*/
        foreach($handle as $ch) {
            curl_multi_remove_handle($mh, $ch);
        }
        curl_multi_close($mh);

#返回结果

string(26) "2018-05-14 02:01:08.0.3482"
string(26) "2018-05-14 02:01:08.0.3482"
string(26) "2018-05-14 02:01:08.0.3572"
string(26) "2018-05-14 02:01:08.0.3632"
string(26) "2018-05-14 02:01:08.0.3582"
string(26) "2018-05-14 02:01:08.0.3532"
string(26) "2018-05-14 02:01:08.0.3582"
string(26) "2018-05-14 02:01:08.0.3602"
string(26) "2018-05-14 02:01:08.0.3652"
string(26) "2018-05-14 02:01:08.0.3642"
string(26) "2018-05-14 02:01:08.0.3572"
string(26) "2018-05-14 02:01:08.0.3672"
string(26) "2018-05-14 02:01:08.0.3592"
string(26) "2018-05-14 02:01:08.0.3602"
string(26) "2018-05-14 02:01:08.0.3582"
string(26) "2018-05-14 02:01:08.0.3642"
复制全文 生成海报 编程 网络 PHP cURL 并发处理

推荐文章

PHP 压缩包脚本功能说明
2024-11-19 03:35:29 +0800 CST
PHP 允许跨域的终极解决办法
2024-11-19 08:12:52 +0800 CST
OpenCV 检测与跟踪移动物体
2024-11-18 15:27:01 +0800 CST
Vue3中的虚拟滚动有哪些改进?
2024-11-18 23:58:18 +0800 CST
html5在客户端存储数据
2024-11-17 05:02:17 +0800 CST
聚合支付管理系统
2025-07-23 13:33:30 +0800 CST
Nginx负载均衡详解
2024-11-17 07:43:48 +0800 CST
Roop是一款免费开源的AI换脸工具
2024-11-19 08:31:01 +0800 CST
快手小程序商城系统
2024-11-25 13:39:46 +0800 CST
rangeSlider进度条滑块
2024-11-19 06:49:50 +0800 CST
nginx反向代理
2024-11-18 20:44:14 +0800 CST
PHP如何进行MySQL数据备份?
2024-11-18 20:40:25 +0800 CST
前端代码规范 - Commit 提交规范
2024-11-18 10:18:08 +0800 CST
mysql 计算附近的人
2024-11-18 13:51:11 +0800 CST
css模拟了MacBook的外观
2024-11-18 14:07:40 +0800 CST
资源文档库
2024-12-07 20:42:49 +0800 CST
CentOS 镜像源配置
2024-11-18 11:28:06 +0800 CST
Nginx 跨域处理配置
2024-11-18 16:51:51 +0800 CST
API 管理系统售卖系统
2024-11-19 08:54:18 +0800 CST
Golang 随机公平库 satmihir/fair
2024-11-19 03:28:37 +0800 CST
程序员茄子在线接单