reversEngineer
Intern
Intern
  • UID817
  • Fans0
  • Follows0
  • Posts1
Reads:52092Replies:2

PHP API Struggle

Created#
More Posted time:Aug 29, 2016 19:18 PM
I am in a desperate struggle to get the PHP API to work. Please explain why this code returns an IncompleteSignature error each time. I have tried every possible approach for several days to get this to work and have had no success.

<?php
//DescribeRegions.php

$accessKeyID=//"MY ACCESS KEY ID HERE";
$accessKeySecret=//"MY ACCESS KEY HERE";


date_default_timezone_set('UTC');
$dateString = date("Y-m-d");
$timeString = date("H:i:s");
$timeStamp =$dateString.'T'.$timeString.'Z';

$nonce=generateNonce();

$requestParameterKeys = array("AccessKeyId",
 "Action",
 "Format",
     "RegionId",
 "SignatureMethod",
 "SignatureNonce",
 "SignatureVersion",
 "Timestamp",
 "Version");

$requestParameterValues = array($accessKeyID,
"DescribeRegions",
"JSON",
"region1",
"HMAC-SHA1",
$nonce,
"1.0",
$timeStamp,
"2014-05-26");

$stringToSign = generateStringToSign($requestParameterKeys,$requestParameterValues);

$signature = base64_encode(hash_hmac('sha1',$stringToSign,$accessKeySecret.'&',$raw_output=TRUE));

$destinationURL=("http://ecs.aliyuncs.com/?".listParameters($requestParameterKeys,$requestParameterValues)."&Signature=".urlencode($signature));
header('Location: '.$destinationURL);

function generateStringToSign($keys,$values){
$canonicalizedQueryString="";
for ($i=0;$i<count($keys);$i++){
if($i!=0){
$canonicalizedQueryString=$canonicalizedQueryString.'&';
}
$canonicalizedQueryString=$canonicalizedQueryString.$keys[$i].'='.$values[$i];
}
return('GET'.'&'.urlencode('/').'&'.urlencode($canonicalizedQueryString));
}

function listParameters($keys,$values){
$parameters="";
for($i=0;$i<count($keys);$i++){
if($i!=0){
$parameters=$parameters.'&';
}
$parameters=$parameters.urlencode($keys[$i]).'='.urlencode($values[$i]);
}
return $parameters;
}

function generateNonce($length = 16) {
    return substr(str_shuffle(str_repeat($x='0123456789', ceil($length/strlen($x)) )),1,$length);
}
?>

Sailor
Intern
Intern
  • UID820
  • Fans0
  • Follows0
  • Posts1
1st Reply#
Posted time:Aug 30, 2016 19:11 PM
Refer to the php code in this doc:


https://help.aliyun.com/knowledge_detail/40603.html


Clear the browser cache before open the above url.

fridayyo
Post banned
Post banned
  • UID214
  • Fans5
  • Follows15
  • Posts357
2nd Reply#
Posted time:Aug 30, 2016 19:28 PM
Sorry, the user is banned
Guest