2010-07-13 185 views
0

我正在嘗試將亞馬遜產品API集成到我的網站中,並且遇到了幾篇幫助我構建URL的文章。唯一的問題是當我執行下面的代碼時,我得到以下錯誤。難道我做錯了什麼?亞馬遜與PHP的產品API

內部服務器錯誤服務器 遇到一個內部錯誤或 配置錯誤,無法 完成您的請求。請聯繫 服務器管理員, [email protected]並告知他們 發生錯誤的時間,並且 您可能已經完成的任何事情可能會導致該錯誤 。更多 有關此錯誤的信息可能在服務器錯誤日誌中可用 。

$AWS_ACCESS_KEY_ID = "[myaccesskeyhere]"; 
$AWS_SECRET_ACCESS_KEY = "[mysecretkeyhere]"; 

$base_url = "http://ecs.amazonaws.com/onca/xml?"; 
$url_params = array('Operation'=>"ItemSearch",'Service'=>"AWSECommerceService", 
'AWSAccessKeyId'=>$AWS_ACCESS_KEY_ID,'AssociateTag'=>"yourtag-10", 
'Version'=>"2006-09-11",'Availability'=>"Available",'Condition'=>"All", 
'ItemPage'=>"1",'ResponseGroup'=>"Images,ItemAttributes,EditorialReview", 
'Keywords'=>"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("&",$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); 

$url_string = implode("&",$url_parts); 
$url = $base_url.$url_string."&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; 

編輯

上面的代碼現在工作...我是缺少一個 「?」 BASE URL後

回答

4
$AWS_ACCESS_KEY_ID = "[myaccesskeyhere]"; 
$AWS_SECRET_ACCESS_KEY = "[mysecretkeyhere]"; 

$base_url = "http://ecs.amazonaws.com/onca/xml?"; 
$url_params = array('Operation'=>"ItemSearch",'Service'=>"AWSECommerceService", 
'AWSAccessKeyId'=>$AWS_ACCESS_KEY_ID,'AssociateTag'=>"yourtag-10", 
'Version'=>"2006-09-11",'Availability'=>"Available",'Condition'=>"All", 
'ItemPage'=>"1",'ResponseGroup'=>"Images,ItemAttributes,EditorialReview", 
'Keywords'=>"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("&",$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); 

$url_string = implode("&",$url_parts); 
$url = $base_url.$url_string."&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; 

對於任何想知道的人。然後,您可以使用simplexml導航xml_response!

希望這有助於有人出來:)

+0

我與這個測試,但它說 我們出的要求籤名不匹配您提供的簽名。檢查您的AWS祕密訪問密鑰和簽名方法。詳細信息請參閱服務文檔。 雖然我對這個網址的關鍵測試工作正常。 http://associates-amazon.s3.amazonaws.com/signed-requests/helper/index.html 請幫忙嗎? – Daroath 2017-07-18 04:29:50

-1

亞馬遜產品API使用貝婁代碼:

<?php 
//Enter your IDs 
define("Access_Key_ID", "[Your Access Key ID]"); 
define("Associate_tag", "[Your Associate Tag ID]"); 

//Set up the operation in the request 
function ItemSearch($SearchIndex, $Keywords){ 

//Set the values for some of the parameters 
$Operation = "ItemSearch"; 
$Version = "2013-08-01"; 
$ResponseGroup = "ItemAttributes,Offers"; 
//User interface provides values 
//for $SearchIndex and $Keywords 

//Define the request 
$request= 
    "http://webservices.amazon.com/onca/xml" 
    . "?Service=AWSECommerceService" 
    . "&AssociateTag=" . Associate_tag 
    . "&AWSAccessKeyId=" . Access_Key_ID 
    . "&Operation=" . $Operation 
    . "&Version=" . $Version 
    . "&SearchIndex=" . $SearchIndex 
    . "&Keywords=" . $Keywords 
    . "&Signature=" . [Request Signature] 
    . "&ResponseGroup=" . $ResponseGroup; 

//Catch the response in the $response object 
$response = file_get_contents($request); 
$parsed_xml = simplexml_load_string($response); 
printSearchResults($parsed_xml, $SearchIndex); 
} 
?>