title: Recursive in a while loop in PHP
body: <p>I have a list of clients from the database and for every client, I need to use curl to grab their data from a website using a random proxy IP from my list. The recursive function tries to grab the html using an ip and if the ip doesn't work/too slow, I want to skip the ip and grab another random ip, until I get the client data from the website and then proceed next to another client and so on.</p>
  
  <p>Currently the problem is that, if an ip doesn't work, the recursive function seems to skip the client record and iterate to the next client record without recursion. Would appreciate anyone who could shed some light here. Thanks in advanced.</p>
  
  <pre><code>$sql = mysql_query( 'SELECT * FROM client' );
  
  while( $row = mysql_fetch_array( $sql ) ) {
      $fields = array( 'clientID' =&gt; $row['clientID'] );
  
      $fields_string = '';
      foreach($fields as $key=&gt;$value) { $fields_string .= urlencode($key).'='.urlencode($value).'&amp;'; }
      rtrim($fields_string,'&amp;');
  
      $result = getHTML( $row['url'], $fields_string ); //  call to recursive function
      var_dump($result);
  }
  
  $proxy = json_decode(file_get_contents('array2.json'), true);
  
  function getHTML( $url, $fields_string ) {
      global $proxy;
  
      $randKey = array_rand( $proxy );
      $ip = $proxy[$randKey];
  
      $ch = curl_init( );
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_PROXY, $ip );
      curl_setopt($ch, CURLOPT_POST, true );
      curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
      curl_setopt($ch, CURLOPT_TIMEOUT, 10 );
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      $result = curl_exec($ch);
      curl_close($ch);
  
      if( !$result || !stristr( $result, '&lt;span class="title"&gt;' ) ) {
          flush( );
          ob_flush( );
          echo $ip . '&lt;br&gt;'; // just for debugging
          getHTML( $url, $fields_string ); // recursion happen here
      }
      else {
          return $result;
      }
  }
  </code></pre>