Skip to content

Instantly share code, notes, and snippets.

@miniplay
Last active December 13, 2015 20:48
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 miniplay/4972221 to your computer and use it in GitHub Desktop.
Save miniplay/4972221 to your computer and use it in GitHub Desktop.
Miniplay API: Parameters validation snippet (PHP)
<?php
/* 1. Decode all get parameters */
// Only if your system configuration or language doesn't automatically decodes it
// foreach ($_GET as $key => $value) $_GET[$key] = urldecode($value);
/* 2. Check signature */
if ($_GET['mini_signature'] != signParameters("YOUR_API_KEY",$_GET)) {
die("The 'mp_*' parameters signature is invalid");
} else {
echo "The 'mp_*' parameters signature is valid!";
}
/* 3. Helper function definition, yeah, it will look so good inside a class :) */
/**
* Signs an array of mp_* parameters by concatenating the values to the api key
* @param string $apiKey
* @param array $parameters
* @return string signature
*/
function mpSignParameters($apiKey, array $parameters) {
ksort($parameters); /* Sort array alphabetically by its keys (Although they should be already sorted by key) */
$signatureString = "";
foreach ($parameters as $key=>$value) {
if (substr($key,0,3)==="mp_") $signatureString.=(string)$value;
}
return md5($apiKey.$signatureString);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment