duan20081202 2015-02-05 14:49
浏览 153
已采纳

curl_multi_exec():不是有效的cURL句柄资源

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

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

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

This is returning

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

This is referring to this line of code: $mrc = curl_multi_exec ( $mh, $active );

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!

  • 写回答

1条回答 默认 最新

  • dongyou5068 2015-02-05 14:56
    关注

    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 );
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序