public function testOauthUrlencode_TwitterExamples() { self::assertEquals('Ladies%20%2B%20Gentlemen', oauth_urlencode('Ladies + Gentlemen')); self::assertEquals('An%20encoded%20string%21', oauth_urlencode('An encoded string!')); self::assertEquals('Dogs%2C%20Cats%20%26%20Mice', oauth_urlencode('Dogs, Cats & Mice')); self::assertEquals('%E2%98%83', oauth_urlencode('☃')); }
function oauth_get_sbs($http_method, $uri, $request_parameters = null) { $request_parameters = $request_parameters === null ? array() : $request_parameters; if (!is_array($request_parameters)) { trigger_error('oauth_get_sbs() expects parameter 3 to be array, ' . gettype($request_parameters) . ' given', E_USER_WARNING); return null; } list($uriBase) = explode('?', strtolower($uri), 2); parse_str(parse_url($uri, PHP_URL_QUERY), $query_params); $params = $query_params + $request_parameters; $params = array_diff_key($params, array('oauth_signature' => 1)); $normalizedParams = array(); foreach ($params as $key => $value) { $normalizedParams[oauth_urlencode($key)] = oauth_urlencode($value); } uksort($normalizedParams, 'strnatcmp'); $paramParts = array(); foreach ($normalizedParams as $key => $value) { $paramParts[] = $key . '=' . $value; } $param_str = implode('&', $paramParts); return $http_method . '&' . oauth_urlencode($uriBase) . '&' . oauth_urlencode($param_str); }
public function sign($type = 'oauth', $key = '', $secret1 = '', $secret2 = '', $method = '') { if (empty($method)) { $method = $this->get('method'); if ($method == 'GET') { $qkey = 'query'; } else { $qkey = 'postdata'; } } //$qkey = 'query'; switch ($type) { case 'oauth': $queryvars = $this->get('queryvars'); $postvars = $this->get('postdata'); if (!isset($queryvars['oauth_nonce']) && !isset($postvars['oauth_nonce'])) { $this->add($qkey, 'oauth_nonce', uniqid()); } if (!isset($queryvars['oauth_timestamp']) && !isset($postvars['oauth_timestamp'])) { $this->add($qkey, 'oauth_timestamp', time()); } if (!isset($queryvars['oauth_token']) && !isset($postvars['oauth_token'])) { $this->add($qkey, 'oauth_token', ''); } if (!isset($queryvars['oauth_consumer_key']) && !isset($postvars['oauth_consumer_key'])) { $this->add($qkey, 'oauth_consumer_key', oauth_urlencode($key)); } if (!isset($queryvars['oauth_signature_method']) && !isset($postvars['oauth_signature_method'])) { $this->add($qkey, 'oauth_signature_method', 'HMAC-SHA1'); } if (!isset($queryvars['oauth_version']) && !isset($postvars['oauth_version'])) { $this->add($qkey, 'oauth_version', '1.0'); } if (isset($queryvars['oauth_signature']) || isset($postvars['oauth_signature'])) { return false; } $sbs = $this->get('sbs'); $secret = !empty($secret1) ? oauth_urlencode($secret1) : ''; $token_secret = !empty($secret2) ? oauth_urlencode($secret2) : ''; $secret = $secret . '&' . $token_secret; $signature = base64_encode(hash_hmac('sha1', $sbs, $secret, true)); $this->add($qkey, 'oauth_signature', $signature); break; default: return false; } }
<?php /* best viewed through an atom viewer, such as http://inforss.mozdev.org/ */ require "config.inc.php"; $o = new OAuth(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_AUTHORIZATION); try { $access_token_info = unserialize(file_get_contents(OAUTH_TMP_DIR . "/access_token_resp")); $o->setToken($access_token_info["oauth_token"], $access_token_info["oauth_token_secret"]); $feeds_url = "http://api.netflix.com/users/" . oauth_urlencode($access_token_info["user_id"]) . "/feeds"; $o->fetch($feeds_url); $feeds = $o->getLastResponse(); /* we need to pick the rental history feed (returned rentals) */ $feeds_xml = new SimpleXMLElement($feeds); /* if you want to access other feeds, change the following rel attribute */ $feed_rel = "http://schemas.netflix.com/feed.rental_history.returned"; $returned_feed = current($feeds_xml->xpath("/resource/link[@rel=\"{$feed_rel}\"]"))->attributes(); /* don't sign the feed requests */ $curl = curl_init($returned_feed["href"]); curl_exec($curl); } catch (OAuthException $E) { echo "Exception caught!\n"; echo "Response: " . $E->lastResponse . "\n"; var_dump($E); }
<?php echo oauth_urlencode('http://www.example.com'), "\n"; echo oauth_urlencode('http://www.example.com/~user'), "\n";
/** * Proper encoding for PHP version <= 5.3 */ public function http_build_query($params, $separator = '&') { $query = ''; $prevSeparator = ''; foreach ($params as $key => $value) { $query .= $prevSeparator . oauth_urlencode($key) . '=' . oauth_urlencode($value); $prevSeparator = $separator; } return $query; }