我们有一个从AWS API网关调用的Lambda函数。我一直试图从我的PHP代码中调用它。我正在使用此文档尝试获取“授权”标题:
http://docs.aws.amazon.com/AmazonS3/latest/dev/RESTAuthentication.html#RESTAuthenticationDebugging
但我必须遗漏一些东西,因为我不断收到错误:
{“message”:“缺少身份验证令牌”}
这是我到目前为止所拥有的:
current_date = date('D, d M Y H:i:s +u');
$tmp1 = explode('/',AWS_URL);
write_log("URL exploded" . json_encode($tmp1));
$tmp2 = explode('.',$tmp1[2]);
write_log("IP exploded: " . json_encode($tmp2));
$url_parts = $tmp2[0] . '/' . $tmp1[3] . '/' . $tmp1[4];
$to_cannonicalize = 'POST\nContent-MD5\nTontent-Type\n' . $current_date . '\n' . $url_parts;
write_log("cannocalize this string: " . $to_cannonicalize);
$signature = base64_encode(hash_hmac('sha256', $to_cannonicalize, AWS_LABMDA_SECRET));
$formatted_date = date('Ymd');
$zdate = date('Ymd') . 'T' . date('His') . 'Z';
$authorization = 'AWS ' . AWS_LABMDA_KEY . ':' . $signature;
// set up the CURL call
$header_array = array (
'Content-type' => 'application/json',
'Authorization' => $authorization,
'X-Amz-Date' => $zdate
);
write_log(json_encode($header_array));
$post_data = array (
'postalcode' => $zip
);
$curl = curl_init( AWS_TAXRATE_URL );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $curl, CURLOPT_POST, 1 );
curl_setopt($curl, CURLOPT_HTTPHEADER, $header_array);
curl_setopt( $curl, CURLOPT_POSTFIELDS, $post_data );
// make call and send results
$curl_response = curl_exec( $curl );
我还尝试将Postman中使用的内容复制为标题(我遇到了不同类型的错误):
$authorization = 'AWS4-HMAC-SHA256 Credential=' . AWS_LABMDA_KEY . '/' . $formatted_date . '/'
. AWS_SQS_REGION . '/execute-api/aws4_request, SignedHeaders=content-length;content-type;host;x-amz-date, '
. 'Signature=' . $signature;
这似乎也没有用。