2014-09-03 61 views
-1

我有一个API文档。我在创建签名请求时遇到问题。我有如何创建签名的以下过程。有人可以帮助我从下面的步骤一个例子:使用php生成API调用签名

生成签名创建签名

Create the canonical zed query string that you need later in this procedure: 
    Sort the UTF-8 query string components by parameter name with natural byte ordering. The parameters can come from the GET URI or from the POST body (when Content-Type is application/x-www-form-urlencoded). 
    URL encode the parameter name and values 
    Concatinate name and values to a single string (eg. var1value1var2value2) 
Calculate an RFC 2104-compliant HMAC with the string you just created, your API Access Key as the key, and SHA1 as the hash algorithm. 
Make the resulting value base64 encoded. 
Use the Resulting value as the value of the Signature request parameter 

编辑:

这里是文档的输出样本:

https://domain.com/api.php?action=checkDomain&version=20090622&keyId=123456 & name = glo0000w.com & signature = fvatTFVwRNF1cyH%2Fj%2Flaig8QytY%3D

下面是我试图这样做,但没有奏效

<?php 
$sig = urlencode('actioncheckDomainversion20090622keyId123456nameglo0000w.com'); 
$sig = hash_hmac('sha1', $sig, '123456'); 
$sig = base64_encode($sig); 
?> 

有人可以帮我实现用PHP生成签名的程序?谢谢。

+0

你遗漏了“使结果值base64编码”。 '$ sig = base64_encode($ sig);' – 2014-09-03 20:43:11

+0

您之前询问的问题的副本 – 2014-09-03 20:46:17

+0

@RocketHazmat我添加了它并仍然出现错误:错误的请求签名UPL-TYQTSBHIYGRFHKXEPJNPELGY – Toni 2014-09-03 21:14:52

回答

2

首先,你没有按照你应该按键排序你的参数。

$p = array(
    'action' => 'checkDomain', 
    'version' => '20090622', 
    'keyId' => 123456, 
    'name' => 'glo0000w.com', 
); 

ksort($p); 
$string = ''; 
foreach($p as $oneKey=>$oneValue) 
    $string .= urlencode($oneKey) . urlencode($oneValue); 

您的其他问题在您致电hash_hmac()。默认情况下,它返回一个十六进制字符串,并且在base64编码中没有任何意义。而且,结果输出比示例更长。我很确定这是一个错误。

相反,你要使用产生一个二进制输出可选的第四个参数per the hash_hmac docs和Base64编码值:

$hash = hash_hmac('sha1', $string, '123456', true); 
$sig = base64_encode($hash); 

最后,我怀疑你可能会使用签名错误的快捷键。您使用的值为keyId,即总是accessKey不同。 (除了可能的例子。)