PHP哈希sha256在服务器上无法正常工作


PHP hash sha256 not working fine on server

我正在研究web服务,API需要一个名为$签名的变量,该变量等于:

// Signature is generated by SHA256 (Api-Key + Shared Secret + Timestamp (in seconds))
$signature = hash("sha256", $apiKey.$sharedSecret.time());

代码的其余部分如下,它使用PECL 2.5.3:

$client = new http'Client;
$request = new http'Client'Request;
$request->setRequestUrl('https://api.test.hotelbeds.com/hotel-api/1.0/status');
$request->setRequestMethod('GET');
$request->setHeaders(array(
'cache-control' => 'no-cache',
'x-signature' => $signature,
'api-key' => '******************************',
'accept' => 'application/json'
));

$client->enqueue($request)->send();
$response = $client->getResponse();
echo $response->getBody();

问题是,如果我在本地服务器上生成并var_dump $signature值,然后像这样粘贴:

'x-signature' => 'b67bfd70f9ba8564eddfde0aa02dffb8aa6e24f937886ae80f77efcee139e5e5',

然后在项目服务器上运行脚本,它会给我一个正确的响应,但如果我只输入:

'x-signature' => $signature,。。。

它给了我一个"未授权的错误"。我不知道出了什么问题,我已经在项目服务器上打印了时间,彼此之间的差异就像10个数字,即使我把它们放得相等,问题仍然存在。

来自本地服务器的PHP版本5.5.12,来自项目服务器的版本是5.4.16

所以,它可能是时间函数,我不认为是,或者哈希函数与PHP版本不同,但这会很奇怪。

请帮忙。

我通过更改服务器上的时区并更正那里的时间来解决这个问题。