php curl multi init HTTP 401 Unauthorized

时间:2012-01-14 18:21:40

标签: php authentication curl

我使用了curl单一init来发出HTTP Get,一切正常。 现在我尝试使用多初始化(因为我需要获取多个URL),并且在对Get的响应中收到带有“此请求需要HTTP身份验证”的401消息。 两种情况下使用相同的Curl选项。 这是多init的代码,下面是单init函数。

protected function _multiQueryRunkeeper($uri, $subscribersInfo,$acceptHeader) {       
    $curlOptions = array(       
            CURLOPT_URL => 'https://api.runkeeper.com' . $uri,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_TIMEOUT => 8,
            CURLOPT_HTTPAUTH => CURLAUTH_ANY,
            CURLOPT_HTTPGET => true
    );
    $curl_array = array();
    $mh = curl_multi_init();
    foreach ($subscribersInfo as $i => $subscriber) {
        $curl_array[$i] = curl_init();
        curl_setopt_array($curl_array[$i],$curlOptions);
        curl_setopt($curl_array[$i], CURLOPT_HEADER,
            array('Authorization: Bearer '.$subscriber['token'],
            'Accept: application/vnd.com.runkeeper.' . $acceptHeader));
        curl_multi_add_handle($mh,$curl_array[$i]);
    }
    $running = NULL;
    do {
        usleep(10000);
        curl_multi_exec($mh,$running);
    } while($running > 0);
    $subscribersWorkoutFeed = array();
    foreach($subscribersInfo as $i => $subscriber)
    {
            $subscribersWorkoutFeed[$i] = curl_multi_getcontent($curl_array[$i]);
            curl_multi_remove_handle($mh, $curl_array[$i]);
    }

    curl_multi_close($mh);
    return $subscribersWorkoutFeed;                   
}



protected function _singleQueryRunkeeper($uri, $subscriberToken,$acceptHeader) {
    try{
        // get fitness user's fitness activities from Runkeeper
        $this->_curl = isset($this->_curl)? $this->_curl : curl_init();
        $curlOptions = array(
            CURLOPT_URL => 'https://api.runkeeper.com' . $uri,
            CURLOPT_HTTPHEADER => array('Authorization: Bearer '.$subscriberToken,
                                        'Accept: application/vnd.com.runkeeper.' . $acceptHeader),
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_TIMEOUT => 8,
            CURLOPT_HTTPGET => true
        );
        curl_setopt_array($this->_curl,$curlOptions);
        $response = curl_exec($this->_curl);
        if($response == false) {
            if (Zend_Registry::isRegistered('logger')) {
                $logger = Zend_Registry::get('logger');
                $logger->log('Curl error on _singleQueryRunkeeper: '
                        . curl_error($this->_curl), Zend_Log::INFO);
            }
            return null;
        }
        $data = Zend_Json::decode($response);
        return($data);
    } catch(Exception $e){
        if (Zend_Registry::isRegistered('logger')) {
            $logger = Zend_Registry::get('logger');
            $logger->log('exception occured on getUsersLatestWorkoutsFromRK. Curl error'
                    . curl_error($this->_curl), Zend_Log::INFO);
        }
    }

}

0 个答案:

没有答案