1

I have problem with Curl_Multi and redirects. Code below works well till I set CURLOPT_FOLLOWLOCATION to 1. Setting CURLOPT_FOLLOWLOCATION on, results in an infinite loop.

/**
 * Curl options
 *
 * @var array
 */
private $_curlOptions = array();

/**
 * Curl default options
 *
 * @var array
 */
private $_curlDefaultOptions = array(
    CURLOPT_HEADER => 0,
//  CURLOPT_FOLLOWLOCATION => 1,
    CURLOPT_CONNECTTIMEOUT => 5,
    CURLOPT_RETURNTRANSFER => 1
);

public function multiRequest(array $urls)
{
    //create the multiple cURL handle
    $mch = curl_multi_init();
    $curlHandles = array();
    $curlOptions = array_merge($this->_curlDefaultOptions,$this->_curlOptions);

    foreach ($urls as $url) {
        $ch = curl_init();
        $curlHandles[] = $ch;
        // set URL
        curl_setopt($ch, CURLOPT_URL, $url);
        // set Options
        curl_setopt_array($ch,$curlOptions);

        //add the two handles
        curl_multi_add_handle($mch,$ch);
    }

    $active = 0;
    do {
        $ret = curl_multi_exec($mch, $active);
    } while ($ret == CURLM_CALL_MULTI_PERFORM);

    while ($active && $ret == CURLM_OK) {
        if (curl_multi_select($mch) != -1) {
            do {
                $mrc = curl_multi_exec($mch, $active);
            } while ($mrc == CURLM_CALL_MULTI_PERFORM);
        }
    }

    $data = array();
    //get data and close the handles
    foreach ($curlHandles as $ch) {
        $result = array();
        $result['errors'] = null;
        if (($error = curl_error($ch))) {
            $result['errors'] = $error;
        }
        $result['content'] = curl_multi_getcontent($ch);
        $data[] = $result;
        curl_multi_remove_handle($mch, $ch);
    }
    curl_multi_close($mch);
    return $data;
}

Uncommenting this line results an infinite loop:

//  CURLOPT_FOLLOWLOCATION => 1,

Edit:

My php version:

$ php -v PHP 
5.4.9-4ubuntu2.3 (cli) (built: Sep  4 2013 19:32:25) 
Copyright (c) 1997-2012 The PHP Group Zend Engine v2.4.0, Copyright
(c) 1998-2012 Zend Technologies
     with Xdebug v2.2.1, Copyright (c) 2002-2012, by Derick Rethans

0 Answers0