Last active
January 4, 2016 17:09
-
-
Save gertig/8651701 to your computer and use it in GitHub Desktop.
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 | |
// Encode a string to URL-safe base64 | |
function encodeBase64UrlSafe($value) | |
{ | |
return str_replace(array('+', '/'), array('-', '_'), | |
base64_encode($value)); | |
} | |
// Decode a string from URL-safe base64 | |
function decodeBase64UrlSafe($value) | |
{ | |
return base64_decode(str_replace(array('-', '_'), array('+', '/'), | |
$value)); | |
} | |
// Sign a URL with a given crypto key | |
// Note that this URL must be properly URL-encoded | |
function signUrl($myUrlToSign, $privateKey) | |
{ // Decode the private key into its binary format | |
$decodedKey = decodeBase64UrlSafe($privateKey); | |
// Create a signature using the private key and the URL-encoded | |
// string using HMAC SHA1. This signature will be binary. | |
$signature = hash_hmac("MD5",$myUrlToSign, $decodedKey, true); | |
$encodedSignature = encodeBase64UrlSafe($signature); | |
return $encodedSignature; | |
} | |
$key = 'Encoding-Key'; | |
$url = 'http://fake-domain.com/GetChapterEventList/?clientId=12345&chapterId=55'; | |
$signature = signUrl($url, $key); | |
$opts = array('http'=>array('header'=>"Authorization: " . $signature)); | |
$context = stream_context_create($opts); | |
$file = file_get_contents($url, false, $context); | |
echo $file | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment