예제 #1
0
 public function fetch($protected_resource_url, $extra_parameters = array(), $http_method = OAUTH_HTTP_METHOD_GET, array $http_headers = array(), $oauth_args = array(), $flags = 0)
 {
     $queryStr = parse_url($protected_resource_url, PHP_URL_QUERY);
     parse_str($queryStr, $queryParams);
     $normalizedUrl = preg_replace('/\\?.*/', '', $protected_resource_url);
     $signatureKeys = array('consumer_key' => $this->consumer_key, 'shared_secret' => $this->consumer_secret, 'oauth_token' => $this->token, 'oauth_secret' => $this->token_secret);
     $oauthParams = array('oauth_consumer_key' => $this->consumer_key, 'oauth_signature_method' => $this->signature_method, 'oauth_nonce' => $this->nonce ?: uniqid() . '.' . time(), 'oauth_timestamp' => $this->timestamp ?: time(), 'oauth_version' => $this->oauthVersion);
     if (!empty($this->token)) {
         $oauthParams['oauth_token'] = $this->token;
     }
     $oauthParams = array_merge($oauthParams, $oauth_args);
     $signParams = array_merge($queryParams, is_array($extra_parameters) ? $extra_parameters : array(), $oauthParams);
     $signature = $this->_generateSignature($http_method, $normalizedUrl, $signParams);
     if ($flags & self::FETCH_SIGONLY) {
         return $signature;
     }
     $requestParams = $extra_parameters;
     switch ($this->auth_type) {
         case OAUTH_AUTH_TYPE_URI:
             $requestParams = $oauthParams + $extra_parameters + array('oauth_signature' => $signature);
             break;
         case OAUTH_AUTH_TYPE_AUTHORIZATION:
             $auth = 'OAuth ';
             $oauthParams['oauth_signature'] = $signature;
             if ($this->flags & self::FETCH_SORTAUTH) {
                 ksort($oauthParams);
             }
             foreach ($oauthParams as $key => $value) {
                 $auth .= oauth_urlencode($key) . '="' . oauth_urlencode($value) . '",';
             }
             $http_headers['Authorization'] = rtrim($auth, ',');
             break;
         case OAUTH_AUTH_TYPE_FORM:
             $extra_parameters = http_build_query($oauthParams + array('oauth_signature' => $signature));
             break;
     }
     $url = $protected_resource_url;
     if (!empty($requestParams) && is_array($requestParams) && empty($queryParams)) {
         $url .= '?' . http_build_query($requestParams);
     }
     $curlHeaders = array();
     foreach ($http_headers as $name => $value) {
         $curlHeaders[] = "{$name}: {$value}";
     }
     if (!isset($http_headers['Accept']) && $this->requestEngine != OAUTH_REQENGINE_CURL) {
         $curlHeaders[] = "Accept:";
         // Prevent curl's default 'Accept: */*'
     }
     if ($http_method != 'POST') {
         $curlHeaders[] = "Expect:";
     }
     $curlOptions = array();
     if ($this->requestEngine == OAUTH_REQENGINE_CURL) {
         $curlOptions[CURLOPT_USERAGENT] = OAUTH_USER_AGENT;
     }
     $curlOptions = $curlOptions + array(CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_0, CURLOPT_RETURNTRANSFER => 1, CURLINFO_HEADER_OUT => 1, CURLOPT_HTTPHEADER => $curlHeaders, CURLOPT_CUSTOMREQUEST => $http_method, CURLOPT_HEADERFUNCTION => array($this, '_curlReceiveHeader'), CURLOPT_SSL_VERIFYPEER => $this->sslChecks & OAUTH_SSLCHECK_PEER, CURLOPT_SSL_VERIFYHOST => $this->sslChecks & OAUTH_SSLCHECK_HOST ? 2 : 0);
     if (is_string($extra_parameters)) {
         $curlOptions[CURLOPT_POSTFIELDS] = $extra_parameters;
     } elseif (is_array($extra_parameters) && !empty($extra_parameters)) {
         $curlOptions[CURLOPT_POSTFIELDS] = $this->http_build_query($extra_parameters);
     }
     $this->lastHeader = false;
     list($this->lastResponse, $this->lastResponseInfo) = $this->execCurl($url, $curlOptions);
     $responseCode = $this->lastResponseInfo['http_code'];
     if ($this->debug) {
         $this->debugInfo = array('lastResponse' => $this->lastResponse, 'lastResponseInfo' => $this->lastResponseInfo, 'lastResponseCode' => $responseCode, 'lastHeader' => $this->lastHeader);
         self::$lastDebugInfo = $this->debugInfo;
     }
     if ($responseCode > 300 && $responseCode < 304) {
         $redirectUrl = substr($this->lastResponseInfo['redirect_url'], 0, OAUTH_MAX_HEADER_LEN - 1);
         return $this->fetch($redirectUrl);
     } elseif ($responseCode < 200 || $responseCode > 209) {
         $e = new OAuthException("Invalid auth/bad request (got a {$responseCode}, expected HTTP/1.1 20X or a redirect)", $responseCode);
         $e->lastResponse = $this->lastResponse;
         $e->debugInfo = $this->lastResponseInfo;
         throw $e;
     }
     return true;
 }