function tb_twitter_request($oauth, $resource, $params = array()) { if (!isset($oauth['consumer_key']) || !isset($oauth['consumer_secret']) || !isset($oauth['access_token']) || !isset($oauth['access_token_secret'])) { return false; } require_once TEMPLATEPATH . '/oauth.php'; $consumer = new __OAuthConsumer($oauth['consumer_key'], $oauth['consumer_secret']); $token = new __OAuthConsumer($oauth['access_token'], $oauth['access_token_secret']); $request = __OAuthRequest::from_consumer_and_token($consumer, $token, 'GET', sprintf(TB_TWITTER_API_URL, $resource, 'json'), $params); $request->sign_request(new __OAuthSignatureMethod_HMAC_SHA1(), $consumer, $token); $response = wp_remote_get($request->to_url(), array('sslverify' => false)); $body = wp_remote_retrieve_body($response); if (empty($body)) { return false; } if (($response = json_decode($body)) === null) { return false; } if (isset($response->errors)) { return false; } return $response; }
/** * 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 = $parameters ? $parameters : array(); $defaults = array("oauth_version" => __OAuthRequest::$version, "oauth_nonce" => __OAuthRequest::generate_nonce(), "oauth_timestamp" => __OAuthRequest::generate_timestamp(), "oauth_consumer_key" => $consumer->key); if ($token) { $defaults['oauth_token'] = $token->key; } $parameters = array_merge($defaults, $parameters); return new __OAuthRequest($http_method, $http_url, $parameters); }