繁体   English   中英

php单卷发工作,但多卷曲不起作用?

[英]php single curl works but multi curl doesn't work?

所以我使用放大器,然后切换到z-wamp认为它会解决问题,但事实并非如此。

我在我的localhost(localhost / site1和localhost / site2)中有单独的“站点”,我正在尝试发送多个curl请求,但由于某些奇怪的原因,它没有做任何事情! 它只适用于我对一个站点进行一次卷曲。 这有效:

$ch = curl_init('http://localhost/site1/');
curl_setopt_array($ch, array(
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HEADER => false,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => array('data' => $data)
));
$res = curl_exec($ch);
//yay!

另一方面,这不起作用:

...
//add a bunch of curl sessions
//to $this->sessions
...
$window = 15;
if (count($this->sessions) < $window)
    $window = count($this->sessions);

$mh = curl_multi_init();

$site_map = array();

for ($i = 0; $i < $window; ++$i) {
    curl_multi_add_handle($mh, $this->sessions[$i]);
    $site_map[(string) $this->sessions[$i]] = $i;
}

$data_results = array();
$running = null;

do {
    $execrun = curl_multi_exec($mh, $running);
} while ($execrun === CURLM_CALL_MULTI_PERFORM);

while ($running && $execrun === CURLM_OK) {

    //the loop just keeps going forever from here

    if (curl_multi_select($mh) !== -1) {
        do {
            $execrun = curl_multi_exec($mh, $running);
        } while ($execrun === CURLM_CALL_MULTI_PERFORM);
    }

    if ($execrun !== CURLM_OK)
        break;

    //to here and never enters the loop below

    while ($done = curl_multi_info_read($mh)) {

        $output = curl_multi_getcontent($done['handle']);

        if ($output)
            $data_results[$site_map[(string) $done['handle']]] = $output;
        else
            $data_results[$site_map[(string) $done['handle']]] = null;

        if (isset($this->sessions[$i]) && $i < count($this->sessions)) {
            curl_multi_add_handle($mh, $this->sessions[$i]);
            $site_map[(string) $this->sessions[$i]] = $i;
            ++$i;
        }

        unset($site_map[(string) $done['handle']]);
        curl_multi_remove_handle($mh, $done['handle']);
        curl_close($done['handle']);
    }
}
curl_multi_close($mh);
return $data_results;

所以在上面的多卷曲代码中,它开始运行,将句柄添加到$ mh,一旦执行,它将保持循环并且永远不会进入$ done = curl_multi_info_read($ mh)while循环。 与此同时它仍然运行良好,并且$ run一直等于2。 此外,curl_multi_info_read将返回false。 所以它只是永远循环。

我的curl扩展已启用(显然,如果单个curl工作),以下是来自PHP Info的详细信息:

cURL support    enabled
cURL Information    7.24.0
Age 3
Features
AsynchDNS   Yes
Debug   No
GSS-Negotiate   No
IDN No
IPv6    Yes
Largefile   Yes
NTLM    Yes
SPNEGO  No
SSL Yes
SSPI    No
krb4    No
libz    Yes
CharConv    No
Protocols   dict, file, ftp, ftps, gopher, http, https, imap, imaps, ldap, pop3, pop3s, rtsp, scp, sftp, smtp, smtps, telnet, tftp
Host    i386-pc-win32
SSL Version OpenSSL/1.0.0g
ZLib Version    1.2.5
libSSH Version  libssh2/1.3.0

这个东西世界上到底发生了什么? 它可能是我的PHP配置的东西? Apache配置? 再一次,我正在使用z-wamp。

PHP版本5.3.10 Apache版本2.4.1 Win 7 64位将PHP目录添加到PATH

编辑事实证明,它一定是某种Apache / PHP配置类型问题,我根本无法发现,因为我放弃了z-wamp并安装了wampserver,这次工作正常。

我刚刚回答了另一个关于多个卷曲调用的问题。

这就是我为运行请求所做的一切。

do {
    $status = curl_multi_exec($mh, $running);
} while ($status === CURLM_CALL_MULTI_PERFORM || $running);

然后我通过循环遍历我的卷曲处理程序数组来获取我需要的信息。

$returned = array();
foreach ($requests as $identifier => $request) {
    $returned[$identifier] = curl_multi_getcontent($request);
    curl_multi_remove_handle($mh, $request);
    curl_close($request);
}

上面的方法对我有用,但似乎你只想在curl多处理程序中添加一定数量的会话。 我们可以将上面的do-while循环更改为以下内容:

do {
    $status = curl_multi_exec($mh, $running);
    if ($running < $window) {
        for ($x = 0; $x < $window - $running; $x++) {
            $index = count($site_map) + $x -1;
            curl_multi_add_handle($mh, $this->sessions[$index]);
            $site_map[(string) $this->sessions[$index]] = $index;
        }
    }
} while ($status === CURLM_CALL_MULTI_PERFORM || $running);

之后我们可以修改数据提取并用以下内容替换整个while ($running && $execrun === CURLM_OK){}部分,因为它只会在处理完所有curl调用后运行:

$returned = array();
foreach ($this->sessions as $identifier => $request) {
    $returned[$identifier] = curl_multi_getcontent($request);
    curl_multi_remove_handle($mh, $request);
    curl_close($request);
}

我曾尝试在同一页面上同时运行curl_exec()curl_multi_exec()并且没有得到任何响应,直到我做了: unset($ch); ,单一请求后。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM