PHP CURL默认支持keepalive连接复用,单个CURL注意要复用handle才可以:
<?php
$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_URL, "https://www.haiyun.me/");
curl_setopt($ch1, CURLOPT_HTTPHEADER, array(
'Connection: Keep-Alive',
'Keep-Alive: 300'
));
curl_setopt($ch1, CURLOPT_FORBID_REUSE, 0);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER,1);
curl_exec($ch1);
curl_setopt($ch1, CURLOPT_URL, "https://www.haiyun.me/");
curl_setopt($ch1, CURLOPT_RETURNTRANSFER,1);
curl_exec($ch1);
curl_close($ch1);
Multi_CURL无需复用handle即默认支持keepalive连接复用,当然也可复用handle,详情见:自用完美php异步并行 multi curl类
$master = curl_multi_init();
$done = curl_multi_info_read($master)
#删除handle
curl_multi_remove_handle($master, $done['handle']);
#复用删除的handle
curl_multi_add_handle($master, $ch);