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

发布于 2024-12-28 11:10:20 字数 1825 浏览 2 评论 0原文

我在 Windows 中的 PHP 中使用 easyPHP 来签署对亚马逊产品广告 API 的请求,但不起作用,因为输出没有生成正确的签名,我不知道为什么。当我使用生成网址发出请求时,亚马逊说 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'];
}

I have this function in PHP in Windows with easyPHP to sign a request for Amazon's Product Advertising API but doesn't work, because the output doesn't generate a correct signature and I don't know why. When I make the request with the generate url, Amazon says that the 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'];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瀟灑尐姊 2025-01-04 11:10:20

我让它工作了,尽管我必须修改它。 Google Sameer Borate 和 Ulrich Mierendorff 我使用他们的代码。

我的代码可以在此处找到

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

I got this to work, though I've had to modify it. Google Sameer Borate and Ulrich Mierendorff I use their code.

My code is found here

But there seems to be something wrong with my webhost right now. :-(

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文