title: SignatureDoesNotMatch Error using Amazon Web Services
body: <p>The code below results in an SignatureDoesNotMatch error and haven't had much luck on their forums or with support - any suggestions as to what is wrong with the below code?</p>
  
  <pre><code>&lt;?php
  error_reporting(E_ALL); 
  ini_set("display_errors", 1); 
  $AWS_ACCESS_KEY_ID = "";
  $AWS_SECRET_ACCESS_KEY = "";
  
  $base_url = "http://ecs.amazonaws.com/onca/xml?";
  $url_params = array('Operation'=&gt;"ItemSearch",'Service'=&gt;"AWSECommerceService",
   'AWSAccessKeyId'=&gt;$AWS_ACCESS_KEY_ID,'AssociateTag'=&gt;"",
   'Version'=&gt;"2011-08-01",'Availability'=&gt;"Available",'Condition'=&gt;"All",
   'ItemPage'=&gt;"1",'ResponseGroup'=&gt;"Images,ItemAttributes,EditorialReview",
   'Keywords'=&gt;"Amazon");
  
  // Add the Timestamp
  $url_params['Timestamp'] = gmdate("Y-m-d\TH:i:s.\\0\\0\\0\\Z", time());
  
  // Sort the URL parameters
  $url_parts = array();
  foreach(array_keys($url_params) as $key)
      $url_parts[] = $key."=".$url_params[$key];
  sort($url_parts);
  
  // Construct the string to sign
  $string_to_sign = "GET\necs.amazonaws.com\n/onca/xml\n".implode("&amp;",$url_parts);
  $string_to_sign = str_replace('+','%20',$string_to_sign);
  $string_to_sign = str_replace(':','%3A',$string_to_sign);
  $string_to_sign = str_replace(';',urlencode(';'),$string_to_sign);
  
  // Sign the request
  $signature = hash_hmac("sha256",$string_to_sign,$AWS_SECRET_ACCESS_KEY,TRUE);
  
  // Base64 encode the signature and make it URL safe
  $signature = base64_encode($signature);
  $signature = str_replace('+','%2B',$signature);
  $signature = str_replace('=','%3D',$signature);
  $signature = str_replace(';',urlencode(';'),$signature);
  $url_string = implode("&amp;",$url_parts);
  $url = $base_url.$url_string."&amp;Signature=".$signature;
  print $url;
  
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL,$url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_TIMEOUT, 15);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  
  $xml_response = curl_exec($ch);
  echo $xml_response; 
      ?&gt;
  </code></pre>