/** * Format and sign an OAuth / API request * 目前仅支持get和post方法 * * @return array */ function oAuthRequest($url, $method, $parameters, $useType = true, $multi = false) { $request = ns_OAuthRequest::from_consumer_and_token($this->token, $method, $url, $parameters); $method = strtoupper($method); switch ($method) { case 'GET': $this->last_req_url = $request->to_url(); $this->http->setUrl($request->to_url()); break; case 'POST': $this->last_req_url = $request->get_normalized_http_url(); $this->http->setUrl($request->get_normalized_http_url()); $this->http->setData($request->to_postdata($multi)); if ($multi) { $header_array = array(); $header_array2 = array(); if ($multi) { $header_array2 = array("Content-Type: multipart/form-data; boundary=" . $GLOBALS['__CLASS']['ns_OAuthRequest']['__STATIC']['boundary'], "Expect: "); } foreach ($header_array as $k => $v) { array_push($header_array2, $k . ': ' . $v); } if (!defined('CURLOPT_HTTPHEADER')) { define('CURLOPT_HTTPHEADER', 10023); } $config = array(CURLOPT_HTTPHEADER => $header_array2); $this->http->setConfig($config); } break; default: trigger_error('WRONG REQUEST METHOD IN WEIBO CLASS!', E_USER_ERROR); break; } $this->http->setHeader('API-RemoteIP', (string) XWB_plugin::getIP()); $time_start = microtime(); $result = $this->http->request(strtolower($method)); $time_end = microtime(); $time_process = array_sum(explode(" ", $time_end)) - array_sum(explode(" ", $time_start)); if ($useType === false || $useType === true) { $result = xwb_util_json::decode($result, true); } $code = $this->http->getState(); if (200 != $code) { $this->_delBindCheck(isset($result['error']) ? (string) $result['error'] : (string) $result); $this->req_error_count++; } if (defined('XWB_DEV_LOG_ALL_RESPOND') && XWB_DEV_LOG_ALL_RESPOND == true) { $this->logRespond($this->last_req_url, $method, (int) $code, $result, array('param' => $parameters, 'time_process' => $time_process, 'triggered_error' => $this->http->get_triggered_error(), 'base_string' => $request->base_string, 'key_string' => $request->key_string)); } if (200 != $code) { if (0 == $code) { $result = array("error_code" => "50000", "error" => "timeout"); } if ($useType === true) { if (!is_array($result)) { $result = array('error' => (string) $result, 'error_code' => $code); } $this->setError($result); } } return $result; }
/** * pretty much a helper function to set up the request */ function from_consumer_and_token($consumer, $token, $http_method, $http_url, $parameters = NULL) { @$parameters or $parameters = array(); $defaults = array("oauth_version" => $GLOBALS['__CLASS']['ns_OAuthRequest']['__STATIC']['OAuthRequest_version'], "oauth_nonce" => ns_OAuthRequest::generate_nonce(), "oauth_timestamp" => ns_OAuthRequest::generate_timestamp(), "oauth_consumer_key" => $consumer->key); if ($token) { $defaults['oauth_token'] = $token->key; } $parameters = array_merge($defaults, $parameters); return new ns_OAuthRequest($http_method, $http_url, $parameters); }