我想用户curl登录其他网站。我使用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只返回我<<。任何人都可以向我解释为什么这样?
答案 0 :(得分:0)
您能否在第二个示例中提供您正在使用的$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);
}
除此之外,您可能希望在最终循环后关闭处理程序,并输出一些结果。我不知道 Code 2只返回的任何内容。