curl_multi_exec(): 不是有效的 cURL 句柄资源 [英] curl_multi_exec(): is not a valid cURL handle resource

查看:22
本文介绍了curl_multi_exec(): 不是有效的 cURL 句柄资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要对 uClassify Sentiment 分类器进行多次 API 调用,以获取许多推文的情绪.由于我有很多推文要索引,因此仅使用 cURL 是不够的(将大约 228 条推文完全索引需要将近 2 分钟).

I need to make multiple API calls to the uClassify Sentiment classifier to get the sentiment for a number of tweets. Since I have a lot of tweets to index, simply using cURL is not enough (it takes nearly 2 minutes to fully index around 228 tweets).

如果没有情感分析,索引几乎是即时的,所以问题肯定是由于大量的 API 调用造成的.

Without sentiment analysis, indexing is almost instantaneous so the problem is definitely due to the high number of API calls.

我已经考虑使用 curl_multi_init.每当进行 API 调用时,都会调用 curl_init(),而不是处理调用,而是将句柄添加到 curl_multi.添加完所有句柄后,我使用 curl_multi_exec() 函数来处理所有句柄.

I have instead considered to use the curl_multi_init. Whenever an API call is made, curl_init() is called and rather than processing the call, the handle is added to curl_multi. Once all the handles are added, I use the curl_multi_exec() function to process all the handles.

这是我的应用程序的简化版本,仅显示情感部分:

Here is a simplified version of my application to only show the sentiment part:

$mh = curl_multi_init ();

foreach ($tweets as $tweet){
    getSentiment ( $tweet, $mh );
}

executeHandles($mh);

function getSentiment($tweet, $mh) {
    $tweet = str_replace ( ' ', '+', $tweet );
    $prefix = 'http://uclassify.com/browse/uClassify/Sentiment/ClassifyText?';
    $key = 'readkey=' . CLASSIFY_KEY . '&';
    $text = 'text=' . $tweet . '&';
    $version = 'version=1.01';
    $url = $prefix . $key . $text . $version;

    // $xml = getXML($url, $mh);
    addHandle ( $url, $mh );
    // $xml = file_get_contents($url, false, $context); ---- TOO SLOWh
    // $mood = parseSentiment($xml);
    // return $mood;
}

function addHandle($url, $mh) {
    $ch = curl_init ();
    $timeout = 5;
    curl_setopt ( $ch, CURLOPT_URL, $url );
    curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );

    curl_multi_add_handle ( $mh, $ch );
    // $xml = curl_exec($ch);
    curl_close ( $ch );
    // return $xml;
}

function executeHandles($mh) {
    if (! empty ( $mh )) {
        $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) {
                usleep ( 100 );
            }
            do {
                $mrc = curl_multi_exec ( $mh, $active );
            } while ( $mrc == CURLM_CALL_MULTI_PERFORM );
        }
    }
}

回来了

curl_multi_exec(): 12 is not a valid cURL handle resource in C:\xampp\htdocs\Twitter\twitteroauth-master\index.php 第 299 行

这是指这行代码:$mrc = curl_multi_exec ( $mh, $active );

现在这只是我第一次使用 cURL,所以我不确定我是否遗漏了一些重要的细节.我不明白为什么会发生这个错误,我没有在 curl_close() 等之后发生任何 curl 语句.

Now this is just my first time using cURL so I am not sure if I am missing some important detail. I cannot understand why this error is happening, I do not have any curl statements that are happening after curl_close() etc.

如有任何帮助,将不胜感激,谢谢!

Any help would be greatly appreciated, thank you!

推荐答案

所以如果您需要这些句柄,为什么要关闭它们?

so if you need those handles, why did you close them?

function addHandle($url, $mh) {
    $ch = curl_init ();
    $timeout = 5;
    curl_setopt ( $ch, CURLOPT_URL, $url );
    curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );

    curl_multi_add_handle ( $mh, $ch );

}

这篇关于curl_multi_exec(): 不是有效的 cURL 句柄资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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