编程 php curl并发代码

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

#关于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 并发处理

推荐文章

如何使用go-redis库与Redis数据库
2024-11-17 04:52:02 +0800 CST
浅谈CSRF攻击
2024-11-18 09:45:14 +0800 CST
thinkphp swoole websocket 结合的demo
2024-11-18 10:18:17 +0800 CST
如何优化网页的 SEO 架构
2024-11-18 14:32:08 +0800 CST
markdowns滚动事件
2024-11-19 10:07:32 +0800 CST
Python 获取网络时间和本地时间
2024-11-18 21:53:35 +0800 CST
软件定制开发流程
2024-11-19 05:52:28 +0800 CST
使用 node-ssh 实现自动化部署
2024-11-18 20:06:21 +0800 CST
2025,重新认识 HTML!
2025-02-07 14:40:00 +0800 CST
从Go开发者的视角看Rust
2024-11-18 11:49:49 +0800 CST
H5端向App端通信(Uniapp 必会)
2025-02-20 10:32:26 +0800 CST
Vue中的样式绑定是如何实现的?
2024-11-18 10:52:14 +0800 CST
Vue3中的v-for指令有什么新特性?
2024-11-18 12:34:09 +0800 CST
PyMySQL - Python中非常有用的库
2024-11-18 14:43:28 +0800 CST
html一个包含iPhoneX和MacBook模拟器
2024-11-19 08:03:47 +0800 CST
Nginx rewrite 的用法
2024-11-18 22:59:02 +0800 CST
程序员茄子在线接单