[TOC] ### 并发 post请求 ``` /** * @param $nodes * [ [ 'url'=>'http://192.168.0.120:8010/api/user/get_info', 'data'=>['uid'=>1,'user_login'=>'admin'] ], [ 'url'=>'http://127.0.0.1:8010/api/user/get_info', 'data'=>['uid'=>1,'user_login'=>'1'] ], ]; * @return array * [ 'http://192.168.0.120:8010/api/user/get_info'=>'{"status":"1","data":{"user_id":"1","user_login":"admin","user_name":"系统管理员"}', 'http://127.0.0.1:8010/api/user/get_info'=>'{"status":"1","data":{"user_id":"1","user_login":"1","user_name":"1"}', ] */ function multiple_threads_post($nodes){ $mh = curl_multi_init(); $curl_array = array(); foreach($nodes as $i => $item) { $curl_array[$i] = curl_init($item['url']); curl_setopt($curl_array[$i], CURLOPT_RETURNTRANSFER, true); curl_setopt($curl_array[$i], CURLOPT_CONNECTTIMEOUT ,2); curl_setopt($curl_array[$i], CURLOPT_POST ,1); curl_setopt($curl_array[$i], CURLOPT_POSTFIELDS ,$item['data']); curl_multi_add_handle($mh, $curl_array[$i]); } $running = NULL; do { curl_multi_exec($mh,$running); } while($running > 0); $res = array(); foreach($nodes as $i => $item) { $res[$item['url']] = curl_multi_getcontent($curl_array[$i]); } foreach($nodes as $i => $item){ curl_multi_remove_handle($mh, $curl_array[$i]); } curl_multi_close($mh); return $res; } /* 测试代码 */ $nodes=[ [ 'url'=>'http://192.168.0.120:8010/api/user/get_info', 'data'=>['uid'=>1,'app_id'=>'system','authen'=>'a90fc09aac9bc7af435f3d0de231421e6dcee8c6b1e24b23172e43376c11a991','dept_id'=>'1','user_login'=>'admin'] ], [ 'url'=>'http://127.0.0.1:8010/api/user/get_info', 'data'=>['uid'=>1,'app_id'=>'system','authen'=>'a90fc09aac9bc7af435f3d0de231421e6dcee8c6b1e24b23172e43376c11a991','dept_id'=>'1','user_login'=>'1'] ], ]; $res = multiple_threads_post($nodes); var_dump($res); ``` ### 并发 get 请求 ``` /** * @param $nodes * [ [ 'url'=>'http://192.168.0.120:8010/api/user/get_info', 'data'=>['uid'=>1,'user_login'=>'admin'] ], [ 'url'=>'http://127.0.0.1:8010/api/user/get_info', 'data'=>['uid'=>1,'user_login'=>'1'] ], ]; * @return array * [ 'http://192.168.0.120:8010/api/user/get_info'=>'{"status":"1","data":{"user_id":"1","user_login":"admin","user_name":"系统管理员"}', 'http://127.0.0.1:8010/api/user/get_info'=>'{"status":"1","data":{"user_id":"1","user_login":"1","user_name":"1"}', ] */ function multiple_threads_get($nodes){ $urls=[]; foreach($nodes as $url){ if(is_array($url['data'])){ $urls[] = $url['url'].'?'.http_build_query($url['data']); }else{ $urls[] = $url['url']; } } $mh = curl_multi_init(); $curl_array = []; foreach($urls as $i => $url) { $curl_array[$i] = curl_init($url); curl_setopt($curl_array[$i], CURLOPT_RETURNTRANSFER, true); curl_setopt($curl_array[$i], CURLOPT_CONNECTTIMEOUT ,2); curl_multi_add_handle($mh, $curl_array[$i]); } $running = NULL; do { curl_multi_exec($mh,$running); } while($running > 0); $res = []; foreach($urls as $i => $url) { $res[$url] = curl_multi_getcontent($curl_array[$i]); } foreach($urls as $i => $url){ curl_multi_remove_handle($mh, $curl_array[$i]); } curl_multi_close($mh); return $res; } /* 测试 代码 */ $nodes=[ [ 'url'=>'http://192.168.0.120:8010/api/user/get_info', 'data'=>['uid'=>1,'app_id'=>'system','authen'=>'a90fc09aac9bc7af435f3d0de231421e6dcee8c6b1e24b23172e43376c11a991','dept_id'=>'1','user_login'=>'admin'] ], [ 'url'=>'http://127.0.0.1:8010/api/user/get_info', 'data'=>['uid'=>1,'app_id'=>'system','authen'=>'a90fc09aac9bc7af435f3d0de231421e6dcee8c6b1e24b23172e43376c11a991','dept_id'=>'1','user_login'=>'1'] ], ]; $res = multiple_threads_get($nodes); var_dump($res); ```