curl_multi_exec显示不同的运行 [英] curl_multi_exec shows different runs
问题描述
我仅对5个网址使用curl_multi_exec()。
现在我有这个奇怪的问题。当我在xampp上运行代码时,它可以完美运行。我可以看到$ running值已初始化为5,然后不断下降。
。
但是,当我在其他本地主机(在arm体系结构上)上尝试过时,$ running初始化为0。
,因此我的curl_multi_exec()永远不会返回任何响应。
I am using curl_multi_exec() for just 5 url. Now i have this strange issue. When i run my code on xampp , it works perfect. i can see $running value initialized with 5 and then keeps decreasing. . But, when i tried it on other localhost(on arm architecture), $running gets initialized with 0. so my curl_multi_exec() never returns any response.
这是代码段:
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);
}
我用Google搜索了几样东西,然后知道curl ssl可能是个问题。因此,我在启用了openssl和curl ssl的情况下安装了另一个localhost(在ARM上)。
现在,我有两个启用了SSL的不同本地主机(都适用于ARM),此代码段在一个本地主机上正常工作,而在另一个本地主机上不工作。
I googled a few things and got to know curl ssl might be an issue. So, i installed another localhost(on ARM) with openssl and curl ssl enabled. Now i have two different localhost(both for ARM) with SSL enabled, this snippet works fine on one localhost and doesn't work on the other one.
并且以某种方式我需要另一个,因为它具有更多功能。
And somehow i need that "other one" because it has lot more features.
请指导此$ running初始化可能是什么问题?
Someone please guide what might be the issue with this $running initialization?
感谢任何帮助:)
也尝试过此方法,但没有成功
Tried this also, but no success
<?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始终为5
。 Forever Loop:(
Issue Here is : $active is always 5
. Forever Loop :(
推荐答案
这里是一个多线程卷曲函数,我使用PHP.net的示例将它们组合在一起。已经使用此功能获取了大量的URL。它能够真正加快速度。我在此方面取得了巨大的成功。
Here is a multi-thread-curl function that I put together using examples from PHP.net. I have used this function to get large amounts of URL's. It is capable of really speeding things up. I have had great success with it.
您甚至可以对其进行扩展,
You could even expand on it and add a parameter for your curl options.
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) {
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);
//Limits to one group of threads.
//break;
}
return $results;
}
$urls = array(
'https://en.wikipedia.org/wiki/Wiki'
);
$results = multi_thread_curl($urls, 1);
print_r($results);
这篇关于curl_multi_exec显示不同的运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!