php使用curl多线程提交

php不想java可以直接继承Thread接口或者实现Runnable接口,进行多线程的开发,不过php中提供了强大的curl工具支持多线程,

php中可以使用curl_multi_init()等来模仿并行处理和多线程程序功能。同时可以使用curl_multi_init()模仿多线程的提交。如下面程序所示:

<?php
include "log.php";
 $stime=microtime(true); 
$urls = array(
   "http://localhost/index.php",
   "http://localhost/index.php",
   "http://localhost/index.php",
   "http://localhost/index.php",
    "http://localhost/index.php",
   "http://localhost/index.php",
   "http://localhost/index.php",
);

$mh = curl_multi_init();

foreach ($urls as $i => $url) {
    $conn[$i] = curl_init($url);
    curl_setopt($conn[$i], CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($conn[$i], CURLOPT_POST, 1);
    curl_setopt($conn[$i], CURLOPT_POSTFIELDS, array('offset'=>$i)); //提交数据
    curl_multi_add_handle($mh, $conn[$i]);
}

do {
    $status = curl_multi_exec($mh, $active);
    $info = curl_multi_info_read($mh);
    if (false !== $info) {
        //var_dump($info);
    }
} while ($status === CURLM_CALL_MULTI_PERFORM || $active);

foreach ($urls as $i => $url) {
    $res[$i] = curl_multi_getcontent($conn[$i]); //获取返回内容
    curl_close($conn[$i]);
}
echo "<pre>";
print_r($res);
$etime=microtime(true);//获取程序执行结束的时间    
$total=$etime-$stime;   //计算差值     
$str_total = var_export($total, TRUE);
echo $str_total;
程序启动7个curl分别向index.php页面发送请求,在index.php中可以根据发送过来的参数来处理请求,就可以模拟多进程的并行提交了.....同时也可以应用进行

数据访问的测试!

             

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值