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 mmo能不能做客户端怪物
  • ¥15 osm下载到arcgis出错
  • ¥15 Dell g15 每次打开eiq portal后3分钟内自动退出
  • ¥200 使用python编写程序,采用socket方式获取网页实时刷新的数据,能定时print()出来就行。
  • ¥15 matlab如何根据图片中的公式绘制e和v的曲线图
  • ¥15 我想用Python(Django)+Vue搭建一个用户登录界面,但是在运行npm run serve时报错了如何解决?
  • ¥15 QQ邮箱过期怎么恢复?
  • ¥15 登录他人的vue项目显示服务器错误
  • ¥15 (标签-android|关键词-app)
  • ¥15 comsol仿真压阻传感器