获取亚马逊产品广告API的签名网址

时间:2012-01-22 12:24:46

标签: php amazon amazon-product-api

我在Windows中的PHP中有这个功能,easyPHP用于签署对亚马逊产品广告API的请求但不起作用,因为输出没有生成正确的签名,我不知道为什么。当我使用generate url发出请求时,Amazon会说SignatureDoesNotMatch

function amazon_get_signed_url($searchTerm) {
    $keyword = $searchTerm; // The keywords you're searching for
    $aws = array();
    $keyword = urlencode($keyword);
    $url = "http://webservices.amazon.com/onca/xml?Service=AWSECommerceService";
    $url .= "&AWSAccessKeyId=".AWS_ACCESS_KEY_ID;
    $url .= "&Operation=ItemSearch";
    $url .= "&SearchIndex=DVD";
    $url .= "&Keywords=".$keyword."";
    $url .= "&ResponseGroup=Small,OfferFull,Images,Reviews,ItemAttributes,SalesRank";
    $url .= "&Timestamp=".gmdate('Y-m-d\TH:i:s\Z');
    $url = str_replace("+",urlencode("+"),$url);
    $url_a = parse_url($url);
    $url_a['query'] = str_replace(',',urlencode(','),$url_a['query']);
    $url_a['query'] = str_replace(';',urlencode(':'),$url_a['query']);
    parse_str($url_a['query'],$params);
    uksort($params, 'strnatcmp');
    $qstr = '';
    foreach ($params as $key => $val) {
    $qstr .= "=".rawurlencode($val);
    }
    $qstr = substr($qstr, 1);
    $qstr = str_replace('%20',urlencode('+'),$qstr);
    $qstr = str_replace(',',urlencode(','),$qstr);
    $qstr = str_replace(';',urlencode(':'),$qstr);
    $sig = "GET\nwebservices.amazon.com\n/onca/xml\n".$qstr;
    $sig = base64_encode(hash_hmac('sha256', $sig, AWS_SECRET_ACCESS_KEY, true));
    $sig = str_replace('+','%2B',$sig);
    $sig = str_replace('=','%3D',$sig);
    $params['Signature'] = $sig;
    $p = array();
    foreach($params as $k=>$v) {
    $p[]=$k."=".$v;
    }
    $qstr = implode("&",$p);
    $rebuilt_url = $url_a['scheme']."://".$url_a['host'].$url_a['path']."?".$qstr;
    $aws['url'] = $url."&Signature=".$sig;
    return $aws['url'];
}

1 个答案:

答案 0 :(得分:0)

我让这个工作,虽然我不得不修改它。 Google Sameer Borate和Ulrich Mierendorff我使用他们的代码。

找到我的代码here

但我的网站主机现在似乎有问题。 : - (