2

这个问题让我困惑了好几次,其中之一是在 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();调用后保存,因此只要它没有在循环中关闭,我将无法删除它

我需要知道哪个更合适,为什么,每个优点和缺点。

4

1 回答 1

3

从性能的角度来看,这并不重要——选择能够补充脚本其余部分所需功能的方法。与发出 HTTP 请求所需的时间相比,运行 curl_init() 所需的时间只是短暂的。

我测试了这两个小脚本。第一个运行了 3,293.014 毫秒,第二个运行了 3,176.957 毫秒。这似乎有点违反直觉,因为第二个脚本比第一个脚本重复了更多的指令。但是,当您查看这两者之间的时间差时,每个请求大约需要 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();

最好的,~雷

于 2012-12-30T16:26:43.847 回答