Created
July 11, 2009 05:41
-
-
Save bellbind/145089 to your computer and use it in GitHub Desktop.
[php][library][amazon]make AWS Signed URL
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 | |
// Create Signed Amazon REST API URL from old unsigned URL | |
// Amazon API requires signature from 2009-08-15. | |
// You can get sign key from management page of your AWS Access Key. | |
// See signature spec: | |
// https://affiliate.amazon.co.jp/gp/associates/help/t126/a13?ie=UTF8&pf_rd_t=501&ref_=amb_link_84046416_5&pf_rd_m=AN1VRQENFRJN5&pf_rd_p=&pf_rd_s=center-1&pf_rd_r=&pf_rd_i=assoc_help_t126_a9 | |
// | |
function amazon_sign_api($key, $url) { | |
$pattern = '/^([^:]+):\/\/([^\/]+)([^\?]+)\?(.*)$/'; | |
preg_match($pattern, $url, $result); | |
$protocol = $result[1]; | |
$host = $result[2]; | |
$path = $result[3]; | |
$params = split("&", $result[4]); | |
$has_timestamp = false; | |
for ($i = 0; $i < count($params); $i += 1) { | |
$kv = split("=", $params[$i]); | |
if (strcmp("Timestamp", $kv[0]) === 0) $has_timestamp = true; | |
$params[$i] = $kv[0]."=".urlencode($kv[1]); | |
} | |
if (!$has_timestamp) { | |
$time = gmdate('Y-m-d\TH:i:s\Z'); | |
array_push($params, "Timestamp=".urlencode($time)); | |
} | |
sort($params); | |
$query = implode("&", $params); | |
$doc = "GET\n".$host."\n".$path."\n".$query; | |
$mac = hash_hmac("sha256", $doc, $key, true); | |
$sign = urlencode(base64_encode($mac)); | |
$url = $protocol."://".$host.$path."?".$query."&Signature=".$sign; | |
return $url; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment