curl - Recursive in a while loop in PHP


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.

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.

$sql = mysql_query( 'SELECT * FROM client' );

while( $row = mysql_fetch_array( $sql ) ) {
    $fields = array( 'clientID' => $row['clientID'] );

    $fields_string = '';
    foreach($fields as $key=>$value) { $fields_string .= urlencode($key).'='.urlencode($value).'&'; }

    $result = getHTML( $row['url'], $fields_string ); //  call to recursive function

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

    if( !$result || !stristr( $result, '<span class="title">' ) ) {
        flush( );
        ob_flush( );
        echo $ip . '<br>'; // just for debugging
        getHTML( $url, $fields_string ); // recursion happen here
    else {
        return $result;



as getHTML returns a value, you shouldreturn getHTML(...) so that the terminal result reaches the original caller.

People are also looking for solutions to the problem: php - Drop down list values linking with mySQL values issue


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.