php单卷曲工程,但多卷曲不工作? [英] php single curl works but multi curl doesn't work?

查看:144
本文介绍了php单卷曲工程,但多卷曲不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我使用安培,然后切换到z-wamp认为它会解决这个问题,但它没有。

So I was using ampps and then switched to z-wamp thinking it would solve the issue, but it didn't.

我有单独的网站我的localhost(localhost / site1& localhost / site2),我试图发送多卷曲请求,但由于一些奇怪的原因,它不做任何事情!它只有当我做一个单一的卷曲到一个网站。这工作原理:

I have separate "sites" in my localhost (localhost/site1 & localhost/site2) that I'm trying to send multi curl requests to, but for some odd reason, it's not doing anything! It only works when I do one single curl to one site. This works:

$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!

另一方面,这不起作用:

In the other hand, this doesn't work:

...
//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;

因此,在上面的多重curl代码中,它开始,将句柄添加到$ mh,一旦它执行,它将保持循环,永远不会进入$ done = curl_multi_info_read($ mh)while循环。同时它仍然运行良好,并且$ running等于2的整个时间。此外,curl_multi_info_read将返回false。

So in the multi curl code above, it starts going, adds the handles to the $mh, and once it executes, it will keep looping and never go into the $done = curl_multi_info_read($mh) while loop. Meanwhile it is still running fine and also $running equals 2 the whole time. Also, curl_multi_info_read will return false. So it just keeps looping forever.

我的curl扩展是启用的(显然,如果单个curl工作),这里是从PHP信息的细节:

My curl extension is enabled (obviously, if single curl works) and here are the details of it from 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。

What in the world is going on with this thing? Could it be something with my PHP config? Apache config? Once again, I'm using z-wamp.

PHP版本5.3.10
Apache版本2.4.1
Win 7 64位
向PATH添加了PHP目录

PHP version 5.3.10 Apache version 2.4.1 Win 7 64-bit Added the PHP dir to PATH

编辑
原来它必须是某种类型的Apache / PHP配置类型问题,我不能发现,因为我没有z-wamp和安装wampserver和它工作这一次。

Edit It turns out that it must be some kind of Apache/PHP config type issue that I can't spot at all because I did away with z-wamp and installed wampserver and it worked this time.

推荐答案

我刚才回答了另一个问题,我发现有多个curl调用。

I just answered another question that I found about multiple curl calls.

这是我运行请求所做的。

This is all I did to run the requests.

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

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

Then I fetched the info I needed by looping over my array of curl handlers.

$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循环改为:

The above approach worked for me, however it seems like you want to add only a certain amount of sessions to the curl multi-handler. We could probably change the do-while loop above to the following:

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);

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

After that we could modify the data fetch and replace your whole while ($running && $execrun === CURLM_OK){} section with the following since it will only run once all your curl calls have been processed:

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

这篇关于php单卷曲工程,但多卷曲不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆