PHP多次返回未知值


PHP multi return unknown value

我想使用curl登录其他网站。我使用卷曲多线程。

代码1:

<?php
$ch1 = curl_init();
$ch2 = curl_init();
curl_setopt($ch1, CURLOPT_URL, $url);
curl_setopt($ch2, CURLOPT_URL, $url);
curl_setopt($ch1, CURLOPT_COOKIE, $post);
curl_setopt($ch2, CURLOPT_COOKIE, $post2)
$mh = curl_multi_init();
curl_multi_add_handle($mh,$ch1);
curl_multi_add_handle($mh,$ch2);
$active = null;
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);
    }
}
?>

代码2:

<?php
$mh = curl_multi_init();
foreach($data as $key => $value)
{   
    $key = curl_init();
    curl_setopt($key, CURLOPT_URL, $value["url"]);
    curl_setopt($key, CURLOPT_COOKIE, $value[$key]);
    curl_multi_add_handle($mh,$key);
}
$active = null;
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);
    }
}
?>

代码1可以返回我想要的结果,但不是很动态。代码2只需返回我即可。有人能向我解释为什么这样吗?

您能提供第二个示例中使用的$data的示例吗。第一个循环对我来说毫无意义:

foreach($data as $key => $value) //assuming $key here is a scalar
{   
    $key = curl_init();   // after this line $key is a resource or false
    curl_setopt($key, CURLOPT_URL, $value["url"]);
    curl_setopt($key, CURLOPT_COOKIE, $value[$key]); // likely $value[$key] is empty, and there is a notice in the log 
    curl_multi_add_handle($mh,$key);
}

除此之外,您可能希望在最后一个循环之后关闭处理程序,并输出一些结果。我看不出在哪一点代码2只是返回任何东西。