PHP Proper way to use cURL in a loop?

This question confused me several times, which one of those is the proper way to use curl in a php loop

$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);

OR

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);
}

and why ?

from what i am thinking of, first one gets rid of lag and uses lower process than the second one does, because second one needs to open and close the curl handler on each loop which produces a lot of process, but i also think the first one will case problems, as example.. the cURL cookie file is only saved after curl_close(); call, therefore i won't be able to delete it as long as its not closed in the loop

I need to know which one is more proper and why, advantages and disadvantages for each.

Answers


It does not matter from a performance perspective -- choose the method that otherwise complements the functionality required by the rest of your script. The amount of time needed to run curl_init() is a blip compared to the time it takes to make an HTTP request.

I tested both of these little scripts. The first ran for 3,293.014ms and the second ran for 3,176.957ms. This would appear to be a little counterintuitive because the second script repeats more instructions than the first script. But when you look at the amount of time difference between these two, it amounts to about 6ms per request, and that is probably down in the "rounding error" range when the entire process, making 20 HTTP requests, takes 6 seconds.

If you want to test yourself, the code is available below.

<?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();

Best to all, ~Ray


Need Your Help

Back button in php

php button back

Ok, so I was not able to find this after searching through google for a while, I'm trying to have a back button on the error page for php, right now it will say "email is wrong" "first name is wrong"

deleting image from a file in php

php unlink

I am trying to delete a file from folder in php here is my model function

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.