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

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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵