【PHP】多线程请求 curl_multi_init()
生活随笔
收集整理的這篇文章主要介紹了
【PHP】多线程请求 curl_multi_init()
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
/*** 多線程請求 curl_multi_init()* https://www.phpied.com/simultaneuos-http-requests-in-php-with-curl/* * $data[0]['url']='url';* $data[0]['post']=[];* $data[0]['post']['appid']='YahooDemo';* $data[0]['post']['output']='php';* $data[0]['post']['context']='content';* * $data[1]['url']='url';* $data[1]['post']=[];* $data[1]['post']['appid']='YahooDemo';* $data[1]['post']['output']='php';* $data[1]['post']['context']='content';* */function multiRequest($data, $options = []){// array of curl handles$curly = [];// data to be returned$result = [];// multi handle$mh = curl_multi_init();// loop through $data and create curl handles// then add them to the multi-handleforeach ($data as $id => $d) {$curly[$id] = curl_init();$url = (is_array($d) && !empty($d['url'])) ? $d['url'] : $d;curl_setopt($curly[$id], CURLOPT_URL, $url);curl_setopt($curly[$id], CURLOPT_HEADER, 0);curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1);// post?if (is_array($d)) {if (!empty($d['post'])) {curl_setopt($curly[$id], CURLOPT_POST, 1);curl_setopt($curly[$id], CURLOPT_POSTFIELDS, $d['post']);}}// extra options?if (!empty($options)) {curl_setopt_array($curly[$id], $options);}curl_multi_add_handle($mh, $curly[$id]);}// execute the handles$running = null;do {curl_multi_exec($mh, $running);} while ($running > 0);// get content and remove handlesforeach ($curly as $id => $c) {$result[$id] = curl_multi_getcontent($c);curl_multi_remove_handle($mh, $c);}// all donecurl_multi_close($mh);return $result;}
原文鏈接:https://www.phpied.com/simultaneuos-http-requests-in-php-with-curl/
總結
以上是生活随笔為你收集整理的【PHP】多线程请求 curl_multi_init()的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【SSH】禁用root远程、修改ssh端
- 下一篇: 【PHP】循环 调用第三方API (cu