PHP在循环中使用cURL的正确方法?

时间:2012-12-30 16:19:30

标签: php apache loops curl debian

这个问题困扰了我几次,其中一个是在php循环中使用curl的正确方法

$ch = curl_init();
for($i=0; $i<10; $i++){
  // curl options skiped ..
  curl_setopt($ch,CURLOPT_URL,"https://secure.imvu.com/login/login/");
  $response = curl_exec($ch);
   // print $response;
}
curl_close($ch);

for($i=0; $i<10; $i++){
  $ch = curl_init();
  // curl options skiped ..
  curl_setopt($ch,CURLOPT_URL,"https://secure.imvu.com/login/login/");
  $response = curl_exec($ch);
    // print $response;
  curl_close($ch);
}

为什么?

从我的想法来看,第一个摆脱滞后并使用比第二个更低的进程,因为第二个需要打开和关闭每个循环上的curl处理程序,这会产生很多进程,
但我也认为第一个会出现问题,例如.. cURL cookie文件只在curl_close();调用后保存,因此只要它没有在循环中关闭,我将无法删除它< / p>

我需要知道哪一个更合适,为什么,各有利弊。

1 个答案:

答案 0 :(得分:3)

从性能角度来看无关紧要 - 选择能够补充其余脚本所需功能的方法。与发出HTTP请求所花费的时间相比,运行curl_init()所需的时间是一个短暂的。

我测试了这两个小脚本。第一次运行3,293.014ms,第二次运行3,176.957ms。这看起来有点违反直觉,因为第二个脚本会重复比第一个脚本更多的指令。但是当你查看这两者之间的时间差时,每个请求大约相当于6毫秒,这可能会在“舍入错误”范围内下降,整个过程发出20个HTTP请求需要6秒。

如果您想自己测试一下,可以在下面找到该代码。

<?php // RAY_class_Stopwatch.php
error_reporting(E_ALL);


// DEMONSTRATE A SCRIPT TIMER FOR ALL OR PART OF A SCRIPT PHP 5+
// MAN PAGE http://php.net/manual/en/function.microtime.php


class StopWatch
{
    protected $a; // START TIME
    protected $s; // STATUS - IF RUNNING
    protected $z; // STOP TIME

    public function __construct()
    {
        $this->a = array();
        $this->s = array();
        $this->z = array();
    }

    public function __destruct()
    {
        $ret = $this->readout();
        if (!$ret) return FALSE;
        echo
          __CLASS__
        . '::'
        . __FUNCTION__
        . '() '
        ;
        echo "<b>$ret</b>";
        echo PHP_EOL;
    }

    // A METHOD TO REMOVE A TIMER
    public function reset($name='TIMER')
    {
        // RESET ALL TIMERS
        if ($name == 'TIMER')
        {
            $this->__construct();
        }
        else
        {
            unset($this->a[$name]);
            unset($this->s[$name]);
            unset($this->z[$name]);
        }
    }

    // A METHOD TO CAPTURE THE START TIME
    public function start($name='TIMER')
    {
        $this->a[$name] = microtime(TRUE);
        $this->z[$name] = $this->a[$name];
        $this->s[$name] = 'RUNNING';
    }

    // A METHOD TO CAPTURE THE END TIME
    public function stop($name='TIMER')
    {
        $ret = NULL;

        // STOP ALL THE TIMERS
        if ($name == 'TIMER')
        {
            foreach ($this->a as $name => $start_time)
            {
                // IF THIS TIMER IS STILL RUNNING, STOP IT
                if ($this->s[$name])
                {
                    $this->s[$name] = FALSE;
                    $this->z[$name] = microtime(TRUE);
                }
            }
        }

        // STOP ONLY ONE OF THE TIMERS
        else
        {
            if ($this->s[$name])
            {
                $this->s[$name] = FALSE;
                $this->z[$name] = microtime(TRUE);
            }
            else
            {
                $ret .= "ERROR: CALL TO STOP() METHOD FOR '$name' IS NOT RUNNING";
            }
        }

        // RETURN AN ERROR MESSAGE, IF ANY
        return $ret;
    }

    // A METHOD TO READ OUT THE TIMER(S)
    public function readout($name='TIMER', $dec=3, $m=1000, $eol=PHP_EOL)
    {
        $str = NULL;

        // GET READOUTS FOR ALL THE TIMERS
        if ($name == 'TIMER')
        {
            foreach ($this->a as $name => $start_time)
            {
                $str .= $name;

                // IF THIS TIMER IS STILL RUNNING UPDATE THE END TIME
                if ($this->s[$name])
                {
                    $this->z[$name] = microtime(TRUE);
                    $str .= " RUNNING ";
                }
                else
                {
                    $str .= " STOPPED ";
                }

                // RETURN A DISPLAY STRING
                $lapse_time = $this->z[$name] - $start_time;
                $lapse_msec = $lapse_time * $m;
                $lapse_echo = number_format($lapse_msec, $dec);
                $str .= " $lapse_echo";
                $str .= $eol;
            }
            return $str;
        }

        // GET A READOUT FOR ONLY ONE TIMER
        else
        {
            $str .= $name;

            // IF THIS TIME IS STILL RUNNING, UPDATE THE END TIME
            if ($this->s[$name])
            {
                $this->z[$name] = microtime(TRUE);
                $str .= " RUNNING ";
            }
            else
            {
                $str .= " STOPPED ";
            }


            // RETURN A DISPLAY STRING
            $lapse_time = $this->z[$name] - $this->a[$name];
            $lapse_msec = $lapse_time * $m;
            $lapse_echo = number_format($lapse_msec, $dec);
            $str .= " $lapse_echo";
            $str .= $eol;
            return $str;
        }
    }
}



// DEMONSTRATE THE USE -- INSTANTIATE THE STOPWATCH OBJECT
$sw  = new Stopwatch;

// TIME OSA'S SCRIPTS
ob_start();
$sw->start('ENTIRE');
$sw->start('FIRST');
$ch = curl_init();
for($i=0; $i<10; $i++){
  // curl options skiped ..
  curl_setopt($ch,CURLOPT_URL,"https://secure.imvu.com/login/login/");
  $response = curl_exec($ch);
   // print $response;
}
curl_close($ch);
$sw->stop('FIRST');

$sw->start('SECOND');
for($i=0; $i<10; $i++){
  $ch = curl_init();
  // curl options skiped ..
  curl_setopt($ch,CURLOPT_URL,"https://secure.imvu.com/login/login/");
  $response = curl_exec($ch);
    // print $response;
  curl_close($ch);
}
$sw->stop('SECOND');
$sw->stop('ENTIRE');
ob_end_clean();
$sw->readout();

最好的,〜雷