search

Home  >  Q&A  >  body text

Curl_multi_init single thread to multi-thread problem

function post($url, $data = '', $head = 'application/x-www-form-urlencoded')
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type:{$head};charset=utf-8;"));
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
    curl_setopt($ch, CURLOPT_HEADER, 0);
    if (!empty($data)) {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}

$url = 'http://localhost/test.php';
$res = [];
//模拟100条数据
for ($i=0; $i < 100; $i++) { 
    $res[$key] = post($url, 'key='.$i);
}
var_dump($res);

Could you please tell me how to change curl_multi_init to multi-threading? My idea is to split the $data array into array_chunk($data, 15, true); and then perform CURL, but I am a newbie and have not understood the online examples. Please help.

Bob pangBob pang977 days ago1037

reply all(0)I'll reply

No reply
  • Cancelreply