我有Curl_Multi和重定向的问题。下面的代码很有效,直到我将CURLOPT_FOLLOWLOCATION设置为1.设置CURLOPT_FOLLOWLOCATION,导致无限循环。
/**
* 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;
}
取消注释此行会导致无限循环:
// CURLOPT_FOLLOWLOCATION => 1,
修改
我的php版本:
$ 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