代码之家  ›  专栏  ›  技术社区  ›  st4ck0v3rfl0w

使用PHP的Amazon产品API

  •  1
  • st4ck0v3rfl0w  · 技术社区  · 14 年前

    内部服务器错误 服务器 遇到内部错误或 完成您的请求。请联系 服务器管理员, awsadmin@amazon.com 告诉他们 错误发生的时间,以及 导致了错误。更多 有关此错误的信息可能是 在服务器错误日志中可用。

    $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;
    

    编辑

    上面的代码现在可以工作了…我在基本URL后面缺少了一个“?”

    2 回复  |  直到 10 年前
        1
  •  5
  •   st4ck0v3rfl0w    14 年前
    $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\u响应!

    希望这能帮助别人:)

        2
  •  -1
  •   Undo ptrk    8 年前

    亚马逊产品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);
    }
    ?>