我只使用curl_multi_exec() 来处理5 个url。
现在我有这个奇怪的问题。当我在 xampp 上运行我的代码时,它运行完美。我可以看到 $running 值初始化为 5,然后不断减少。
。
但是,当我在其他本地主机(在arm架构上)上尝试它时,$running被初始化为0。
所以我的curl_multi_exec()永远不会返回任何响应。
这是代码片段:
do {
curl_multi_exec($master,$running);
echo "<pre>";
var_dump($running );
echo "</pre>";
} while($running > 0);
这是我的整个代码:
$nodes = array( 'https://www.example.com',
'https://www.example2.com',
'https://www.example3.com',
'https://www.example4.com',
'https://www.example5.com'
);
$node_count = count($nodes);
$curl_arr = array();
$master = curl_multi_init();
for($i = 0; $i < $node_count; $i++)
{
$url =$nodes[$i];
$curl_arr[$i] = curl_init($url);
curl_setopt($curl_arr[$i], CURLOPT_RETURNTRANSFER, true);
curl_multi_add_handle($master, $curl_arr[$i]);
}
do {
curl_multi_exec($master,$running);
echo "<pre>";
var_dump($running );
echo "</pre>";
} while($running > 0);
for($i = 0; $i < $node_count; $i++)
{
$results = curl_multi_getcontent($curl_arr[$i]);
var_dump($results);
}
我用谷歌搜索了一些东西,并了解到curl ssl可能是一个问题。因此,我安装了另一个本地主机(在 ARM 上),并启用了 openssl 和curl ssl。
现在我有两个启用了 SSL 的不同本地主机(均适用于 ARM),此代码片段在一个本地主机上工作正常,但在另一个本地主机上不起作用。
不知怎的,我需要“另一个”,因为它有更多的功能。
有人请指导这个 $running 初始化可能存在什么问题吗?
如有任何帮助,我们将不胜感激:)
也尝试过这个,但没有成功
<?php
// echo "<meta http-equiv='refresh' content='3'/>" ;
include_once ("simple_html_dom.php");
libxml_use_internal_errors(true);
function get_string_between($string, $start, $end){
$string = ' ' . $string;
$ini = strpos($string, $start);
if ($ini == 0) return '';
$ini += strlen($start);
$len = strpos($string, $end, $ini) - $ini;
return substr($string, $ini, $len);
}
function multi_thread_curl($url_array, $number_threads) {
$curl_array = array_chunk($url_array, $number_threads, $preserve_keys = true);
//Iterate through each batch of urls.
foreach($curl_array as $threads) {
//Create your cURL resources.
foreach($threads as $key=>$value) {
${'ch' . $key} = curl_init();
curl_setopt(${'ch' . $key}, CURLOPT_URL, $value);
curl_setopt(${'ch' . $key}, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt(${'ch' . $key}, CURLOPT_RETURNTRANSFER, true);
curl_setopt(${'ch' . $key}, CURLOPT_TIMEOUT, 10);
}
//Create the multiple cURL handler.
$mh = curl_multi_init();
//Add the handles.
foreach($threads as $key=>$value) {
curl_multi_add_handle($mh, ${'ch' . $key});
}
$active = null;
//execute the handles.
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
while ($active && $mrc == CURLM_OK) {
echo $active;
if (curl_multi_select($mh) != -1) {
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
}
}
//Get your data and close the handles.
foreach($threads as $key=>$value) {
$results[$key] = curl_multi_getcontent(${'ch' . $key});
curl_multi_remove_handle($mh, ${'ch' . $key});
}
//Close the multi handle exec.
curl_multi_close($mh);
}
return $results;
}
$nodes = array( 'https://www.example1.com',
'https://www.example2.com',
'https://www.example3.com',
'https://www.example4.com',
'https://www.example5.com',
);
$node_count = count($nodes);
echo "results: ";
$number_threads = 5;
$results = multi_thread_curl($nodes, $number_threads);
print_r($results);
echo 'done';
?>
这里的问题是:$active is always 5
。永远循环:(