/** * Format and sign an WPOAuth / API request */ function WPOAuthRequest($url, $args = array(), $method = NULL) { //Handle media requests using tmhOAuth library. if ($method == 'MEDIA') { return $this->handleMediaRequest($url, $args); } if (empty($method)) { $method = empty($args) ? "GET" : "POST"; } $req = WPOAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $args); $req->sign_request($this->sha1_method, $this->consumer, $this->token); $response = false; $url = null; switch ($method) { case 'GET': $url = $req->to_url(); $response = wp_remote_get($url); break; case 'POST': $url = $req->get_normalized_http_url(); $args = wp_parse_args($req->to_postdata()); $response = wp_remote_post($url, array('body' => $args, 'timeout' => 30)); break; } if (is_wp_error($response)) { return false; } $this->http_code = $response['response']['code']; $this->last_api_call = $url; $this->format = 'json'; $this->http_header = $response['headers']; return $response['body']; }
/** * pretty much a helper function to set up the request */ public static function from_consumer_and_token($consumer, $token, $http_method, $http_url, $parameters = null) { @$parameters or $parameters = array(); $defaults = array("oauth_version" => WPOAuthRequest::$version, "oauth_nonce" => WPOAuthRequest::generate_nonce(), "oauth_timestamp" => WPOAuthRequest::generate_timestamp(), "oauth_consumer_key" => $consumer->key); if ($token) { $defaults['oauth_token'] = $token->key; } $parameters = array_merge($defaults, $parameters); return new WPOAuthRequest($http_method, $http_url, $parameters); }