curl_multi_exec(): is not a valid cURL handle resource

江枫思渺然 提交于 2020-01-13 16:29:21

问题


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:


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

}


来源:https://stackoverflow.com/questions/28346961/curl-multi-exec-is-not-a-valid-curl-handle-resource

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!