curl - Recursive in a while loop in PHP

266

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).'&'; }
    rtrim($fields_string,'&');

    $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, '<span class="title">' ) ) {
        flush( );
        ob_flush( );
        echo $ip . '<br>'; // just for debugging
        getHTML( $url, $fields_string ); // recursion happen here
    }
    else {
        return $result;
    }
}
475

Answer

Solution:

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

Source

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.