Last active
December 13, 2015 20:48
-
-
Save miniplay/4972221 to your computer and use it in GitHub Desktop.
Miniplay API: Parameters validation snippet (PHP)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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