title: PHP set random variable from proxy list and Curl, if fail repeat
body: <p>I am trying to figure out how to randomly select a proxy ip from a list and then performing a curl with it, and if a fail occurs use a new proxy ip. Here is my working code without the randomization:</p>
  
  <pre><code>    $url       = "www.example.com";
      $loginpassw = 'myproxypw';
  
      $proxy_ip = '23.27.37.128';
      $proxy_port = '29842';
  
      $ch        = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_PROXYPORT, $proxy_port);
      curl_setopt($ch, CURLOPT_PROXY, $proxy_ip);
      curl_setopt($ch, CURLOPT_PROXYUSERPWD, $loginpassw);
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
      curl_setopt($ch, CURLOPT_AUTOREFERER, true);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_TIMEOUT, 600);
      $html  = curl_exec($ch);    
  
      if (strpos($html,'To continue, please type the characters below') !== false) {
          echo "now an error has occurred, let's try a new proxy";
      }
  
      curl_close($ch);
  </code></pre>
  
  <p>Ideally the <code>proxy_ip</code> and <code>proxy_port</code> must stay the same in a list of say:</p>
  
  <pre><code>$proxylist = array (
              array("ip" =&gt; "23.27.37.128", "port" =&gt; "29842"),
              array("ip" =&gt; "23.27.37.111", "port" =&gt; "29852"),
              array("ip" =&gt; "23.27.37.112", "port" =&gt; "29742"),
              array("ip" =&gt; "23.27.37.151", "port" =&gt; "29242")
               );
  </code></pre>
  
  <p>I was wondering if I could possibly use shuffle:</p>
  
  <pre><code>shuffle($proxylist);
  
  while($element = array_pop($proxylist)){
    return $element;
  }
  </code></pre>
  
  <p>My second question would be the best way of doing this, my PHP is not perfect so I am wondering rather than rewriting the top curl over and over should I store it inside a function?</p>
  
  <p>Any help appreciated.</p>
  
  <p>Thanks,
  Simon</p>
  
  <h1>Edit:</h1>
  
  <p>The following code seems to be working where I have split my code into two functions:</p>
  
  <pre><code>    function curltime($url, $proxy_ip, $proxy_port, $loginpassw){
              $ch        = curl_init();
              curl_setopt($ch, CURLOPT_URL, $url);
              curl_setopt($ch, CURLOPT_PROXY, $proxy_ip);
              curl_setopt($ch, CURLOPT_PROXYUSERPWD, $loginpassw);
              curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0);
              curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
              curl_setopt($ch, CURLOPT_AUTOREFERER, true);
              curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
              curl_setopt($ch, CURLOPT_TIMEOUT, 600);
              return curl_exec($ch);  
              curl_close($ch);
      }
  
  //now let's do the curl
  
              $url       = "www.example.com";
              $proxylist = array (
              array("proxyip" =&gt; "23.27.37.128", "proxyport" =&gt; "29842"),
              array("proxyip" =&gt; "23.27.37.111", "proxyport" =&gt; "29852"),
              array("proxyip" =&gt; "23.27.37.112", "proxyport" =&gt; "29742"),
              array("proxyip" =&gt; "23.27.37.151", "proxyport" =&gt; "29242")
               );
              foreach ($proxylist[mt_rand(0,count($proxylist)-1)] as $key =&gt; $value) {
                  $$key = $value;
              }
              $html = $this-&gt;curltime($url, $proxyip, $proxyport, 'somepassword');
  
  
              if (strpos($html,'To continue, please type the characters below') !== false) {
                  echo "now we have errors so let's try again" 
              foreach ($proxylist[mt_rand(0,count($proxylist)-1)] as $key =&gt; $value) {
                  $$key = $value;
              }
              $html = $this-&gt;curltime($url, $proxyip, $proxyport, 'somepassword');
              }
              $cache .= $html;
  </code></pre>
  
  <h1>Anyone know of a better way for me to do the looping?</h1>