先来看下同步的代码以及请求时间。

$start_time=date("h:i:sa");for ($i=0; $i <100 ; $i++) { $urls[]="http://www.downxia.com/downinfo/2315".$i.".html";GetTitle(geturl("http://www.downxia.com/downinfo/2315".$i.".html"));}function geturl($url){               $ch = curl_init();        curl_setopt($ch, CURLOPT_URL, $url);        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);                $output = curl_exec($ch);        curl_close($ch);        return $output;}function GetTitle($output){preg_match('/<title>.*<\/title>/i',$output,$matches);var_dump($matches[0]);}$end_time=date("h:i:sa");echo '开始时间是:'.$start_time;echo '结束时间是:'.$end_time;

最下面可以看到时间花了27秒。

接下来看下php curl 异步并发请求http的代码以及花费时间。

$start_time=date("h:i:sa");$urls=[];for ($i=0; $i <100 ; $i++) { $urls[]="http://www.downxia.com/downinfo/2315".$i.".html";}var_dump($urls);// GetTitle('klasjdkla<title>313asds12</title>');rolling_curl($urls,'GetTitle');function GetTitle($output){preg_match('/<title>.*<\/title>/i',$output,$matches);var_dump($matches[0]);}$end_time=date("h:i:sa");echo '开始时间是:'.$start_time;echo '结束时间是:'.$end_time;function rolling_curl($urls, $callback, $custom_options = null){//多个url访问    // make sure the rolling window isn't greater than the # of urls    $rolling_window = 5;    $rolling_window = (sizeof($urls) < $rolling_window) ? sizeof($urls) : $rolling_window;    $master   = curl_multi_init();    $curl_arr = array();    // add additional curl options here    $std_options = array(        CURLOPT_RETURNTRANSFER => true,        CURLOPT_FOLLOWLOCATION => true,        CURLOPT_MAXREDIRS      => 5        );    $options = ($custom_options) ? ($std_options + $custom_options) : $std_options;    // start the first batch of requests    for ($i = 0; $i < $rolling_window; $i++) {        $ch                   = curl_init();        $options[CURLOPT_URL] = $urls[$i];        curl_setopt_array($ch, $options);        curl_multi_add_handle($master, $ch);    }    do {        while (($execrun = curl_multi_exec($master, $running)) == CURLM_CALL_MULTI_PERFORM);        if ($execrun != CURLM_OK) {            break;        }        // a request was just completed -- find out which one        while ($done = curl_multi_info_read($master)) {            $info = curl_getinfo($done['handle']);            if ($info['http_code'] == 200) {                $output = curl_multi_getcontent($done['handle']);                // request successful.  process output using the callback function.                $callback($output);                // start a new request (it's important to do this before removing the old one)                $ch                   = curl_init();                $options[CURLOPT_URL] = $urls[$i++]; // increment i                curl_setopt_array($ch, $options);                curl_multi_add_handle($master, $ch);                // remove the curl handle that just completed                curl_multi_remove_handle($master, $done['handle']);            } else {                // request failed.  add error handling.            }        }    } while ($running);    curl_multi_close($master);    return true;}

才花了3秒?实际上我感觉应该是花了5秒,因为启动比同步要慢,开始的时候卡了2秒。

http请求效率,毋庸置疑是异步远远高于同步。

核心请求代码如下:(这是老外写的,有点小问题,最后的提示undefined offset)

function rolling_curl($urls, $callback, $custom_options = null){//多个url访问    // make sure the rolling window isn't greater than the # of urls    $rolling_window = 5;    $rolling_window = (sizeof($urls) < $rolling_window) ? sizeof($urls) : $rolling_window;    $master   = curl_multi_init();    $curl_arr = array();    // add additional curl options here    $std_options = array(        CURLOPT_RETURNTRANSFER => true,        CURLOPT_FOLLOWLOCATION => true,        CURLOPT_MAXREDIRS      => 5        );    $options = ($custom_options) ? ($std_options + $custom_options) : $std_options;    // start the first batch of requests    for ($i = 0; $i < $rolling_window; $i++) {        $ch                   = curl_init();        $options[CURLOPT_URL] = $urls[$i];        curl_setopt_array($ch, $options);        curl_multi_add_handle($master, $ch);    }    do {        while (($execrun = curl_multi_exec($master, $running)) == CURLM_CALL_MULTI_PERFORM);        if ($execrun != CURLM_OK) {            break;        }        // a request was just completed -- find out which one        while ($done = curl_multi_info_read($master)) {            $info = curl_getinfo($done['handle']);            if ($info['http_code'] == 200) {                $output = curl_multi_getcontent($done['handle']);                // request successful.  process output using the callback function.                $callback($output);                // start a new request (it's important to do this before removing the old one)                $ch                   = curl_init();                $options[CURLOPT_URL] = $urls[$i++]; // increment i                curl_setopt_array($ch, $options);                curl_multi_add_handle($master, $ch);                // remove the curl handle that just completed                curl_multi_remove_handle($master, $done['handle']);            } else {                // request failed.  add error handling.            }        }    } while ($running);    curl_multi_close($master);    return true;}

修改一下。只要在新增url的时候加个判断就好了。// 当$i等于$urls数组大小时不用再增加了。

function rolling_curl($urls, $callback, $custom_options = null){//多个url访问    // make sure the rolling window isn't greater than the # of urls    $rolling_window = 5;    $rolling_window = (sizeof($urls) < $rolling_window) ? sizeof($urls) : $rolling_window;    $master   = curl_multi_init();    $curl_arr = array();    // add additional curl options here    $std_options = array(        CURLOPT_RETURNTRANSFER => true,        CURLOPT_FOLLOWLOCATION => true,        CURLOPT_MAXREDIRS      => 5        );    $options = ($custom_options) ? ($std_options + $custom_options) : $std_options;    // start the first batch of requests    for ($i = 0; $i < $rolling_window; $i++) {        $ch                   = curl_init();        $options[CURLOPT_URL] = $urls[$i];        curl_setopt_array($ch, $options);        curl_multi_add_handle($master, $ch);    }    do {        while (($execrun = curl_multi_exec($master, $running)) == CURLM_CALL_MULTI_PERFORM);        if ($execrun != CURLM_OK) {            break;        }        // a request was just completed -- find out which one        while ($done = curl_multi_info_read($master)) {            $info = curl_getinfo($done['handle']);            if ($info['http_code'] == 200) {                $output = curl_multi_getcontent($done['handle']);                // request successful.  process output using the callback function.                $callback($output);                // start a new request (it's important to do this before removing the old one)                // 当$i等于$urls数组大小时不用再增加了                if($i<sizeof($urls)){                    $ch                   = curl_init();                    $options[CURLOPT_URL] = $urls[$i++]; // increment i                    curl_setopt_array($ch, $options);                    curl_multi_add_handle($master, $ch);                }                // remove the curl handle that just completed                curl_multi_remove_handle($master, $done['handle']);            } else {                // request failed.  add error handling.            }        }    } while ($running);    curl_multi_close($master);    return true;}

更多相关文章

  1. 5种PHP定义数组的方法
  2. PHP常见数组排序方法小汇总
  3. 数组转json后的结果是数组还是对象? (踩坑记录)
  4. php之日期时间操作一箩筐
  5. PHP怎么把JSON转换成数组?
  6. php时间戳转换

随机推荐

  1. 如何在windows中安装Oracle的SQLPlus
  2. MySQL 8.0的十大新特性
  3. SQL:如何从另一个表中删除行会议条件
  4. java往SQL Server中插入数据插不进去
  5. delphi+sql server 数据库死锁问题。高分
  6. PHP OOP - 调用非obj上的成员函数[重复]
  7. 我学了delphi也有几个月了,我是否能参加团
  8. Postgresql数据库安装问题,找不到configur
  9. mysql 导出数据到txt文件
  10. Oracle ------ SQLDeveloper中SQL语句格