title: How to use PHP curl_multi_init() usage when I need to pass a custom options?
body: <p>I have a custom cURL function that has to download huge number of images from remote server. I was banned a couple of times before when I used file_get_contents(). I found that curl_multi_init() is better option as with 1 connection it can download for example 20 images at once.
  I made a custom functions that uses curl_init() and I am trying to figure out how I can implement curl_multi_init() so in my LOOP where I grab the list of 20 URLs from the database I can call my custom function and at the last loop to use curl_close(). At the current situation my function generates connection for each url in the LOOP. Here is the function:</p>
  <pre><code>function downloadUrlToFile($remoteurl,$newfileName){
  $errors = 0;
      $options = array(
        CURLOPT_FILE    =&gt; fopen('../images/products/'.$newfileName, 'w'),
        CURLOPT_TIMEOUT =&gt;  28800,
        CURLOPT_URL     =&gt; $remoteurl,
        CURLOPT_RETURNTRANSFER =&gt; 1
      );
  
      $ch = curl_init();
      curl_setopt_array($ch, $options);
      $imageString =curl_exec($ch);
      $image = imagecreatefromstring($imageString);
      if($imageString !== false AND !empty($imageString)){
      if ($image !== false){
          $width_orig = imagesx($image);
          if($width_orig &gt; 1000){
          $saveimage = copy_and_resize_remote_image_product($image,$newfileName);
          }else $saveimage = file_put_contents('../images/products/'.$newfileName,$imageString);
          
          }else $errors++;
      }else $errors++;
      curl_close($ch);
      return $errors;
  }
  </code></pre>
  <p>There has to be a way to use curl_multi_init() and my function downloadUrlToFile because:</p>
  <ol>
  <li>I need to change the file name on the fly</li>
  <li>In my function I am also checking several things for the remote image.. In the sample function I check the size only and resize it if neccessary but there is much more things done by this function (I cutted that part for shorter, but I also use the function to pass more variables..)</li>
  </ol>
  <p>How should the code be changed so during the LOOP to connect only once to the remote server?</p>
  <p>Thanks in advance</p>