Last active
December 13, 2015 21:29
-
-
Save erikaheidi/4977971 to your computer and use it in GitHub Desktop.
facebook signed request functions
This file contains hidden or 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
function parse_signed_request($signed_request, $secret) { | |
list($encoded_sig, $payload) = explode('.', $signed_request, 2); | |
// decode the data | |
$sig = base64_url_decode($encoded_sig); | |
$data = json_decode(base64_url_decode($payload), true); | |
if (strtoupper($data['algorithm']) !== 'HMAC-SHA256') { | |
error_log('Unknown algorithm. Expected HMAC-SHA256'); | |
return null; | |
} | |
// check sig | |
$expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true); | |
if ($sig !== $expected_sig) { | |
error_log('Bad Signed JSON signature!'); | |
return null; | |
} | |
return $data; | |
} | |
function base64_url_decode($input) { | |
return base64_decode(strtr($input, '-_', '+/')); | |
} | |
/* working code using the functions bellow */ | |
if(!empty($_REQUEST["signed_request"])) { | |
$data = parse_signed_request($_REQUEST["signed_request"], "YOUR_APP_SECRET"); | |
print_r($data); | |
if (!empty($data["page"]["liked"])) { | |
/* do whatever you want to do here for users who liked your page, like showing special content */ | |
echo "Thanks for liking our fan page!"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment