Skip to content

Instantly share code, notes, and snippets.

@dervn
Created September 26, 2012 03:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dervn/3785881 to your computer and use it in GitHub Desktop.
Save dervn/3785881 to your computer and use it in GitHub Desktop.
sig签名构造方法
function build_http_query($params) {
if (!$params) return '';
// Urlencode both keys and values
$keys = array_keys($params);
$values = array_values($params);
//$keys = OAuthUtil::urlencode_rfc3986(array_keys($params));
//$values = OAuthUtil::urlencode_rfc3986(array_values($params));
$params = array_combine($keys, $values);
// Parameters are sorted by name, using lexicographical byte value ordering.
// Ref: Spec: 9.1.1 (1)
uksort($params, 'strcmp');
$pairs = array();
foreach ($params as $parameter => $value) {
if (is_array($value)) {
// If two or more parameters share the same name, they are sorted by their value
// Ref: Spec: 9.1.1 (1)
natsort($value);
foreach ($value as $duplicate_value) {
$pairs[] = $parameter . '=' . $duplicate_value;
}
} else {
$pairs[] = $parameter . '=' . $value;
}
}
// For each parameter, the name is separated from the corresponding value by an '=' character (ASCII code 61)
// Each name-value pair is separated by an '&' character (ASCII code 38)
return implode('&', $pairs);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment