将XSLT样式表应用于Amazon Product Advertising API XML输出

I am trying to apply my XSLT stylesheet to Amazon API xml output and display a movie title on the page

This is my XSLT file (amazon.xsl):

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:aws="http://webservices.amazon.com/AWSECommerceService/2011-08-01">
<xsl:output method="xml" version="1.0" omit-xml-declaration="yes" indent="yes" media-type="text/html"/>

<xsl:template match="/">
    <xml>
        <root>
            <xsl:apply-templates select="aws:ItemLookupResponse/aws:Items/aws:Item/aws:ItemAttributes/aws:Title" />
        </root>
        </xml>
</xsl:template>

<xsl:template match="aws:Title">
    <xsl:text>Movie title</xsl:text>
    <xsl:value-of select="."/>
</xsl:template>

</xsl:stylesheet>

This is Amazon API XML output:

<ItemLookupResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2011-08-01">
   <OperationRequest>
       <HTTPHeaders>
          <Header Name="UserAgent" Value="Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22"/>
       </HTTPHeaders>
       <RequestId>a1138e89-4335-4650-80f2-641e3c58b623</RequestId>
       <Arguments>
    <Argument Name="Operation" Value="ItemLookup"/>
    <Argument Name="Service" Value="AWSECommerceService"/>
    <Argument Name="Signature" Value="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"/>
    <Argument Name="AssociateTag" Value="xxxxxxxxxxxxxx"/>
    <Argument Name="Version" Value="2011-08-01"/>
    <Argument Name="ItemId" Value="B004LWZWGK"/>
    <Argument Name="AWSAccessKeyId" Value="xxxxxxxxxxxxxxxxxxxx"/>
    <Argument Name="Timestamp" Value="2013-03-21T13:56:55.000Z"/>
    <Argument Name="ResponseGroup" Value="Small"/>
       </Arguments>
       <RequestProcessingTime>0.0189320000000000</RequestProcessingTime>
       </OperationRequest>
    <Items>
      <Item>
         <ItemAttributes>
           <Title>
            The Dark Knight Rises (Blu-ray/DVD Combo+UltraViolet Digital Copy)
           </Title>
         </ItemAttributes>
      </Item>
    </Items>
</ItemLookupResponse>

I have found and used the following PHP code to search for the movie title and display it on the page. However, it only displays it in plain text. I would like to apply an XSLT style to it but I don't know how to properly include it in my php code.

If XSLT was used correctly, it would display "Movie title" right above the actual title.

amazon.php

include('aws_signed_request.php');

$public_key = 'XXXXXXXXXXXXXXXXXXXX';
$private_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$associate_tag = 'xxxxxxxxxxxxxxxxxxxxxxxxx';

// generate signed URL
$request = aws_signed_request('co.uk', array(
        'Operation' => 'ItemLookup',
        'ItemId' => 'B004LWZWGK',
        'ResponseGroup' => 'Small'), $public_key, $private_key, $associate_tag);

// do request (you could also use curl etc.)
$response = @file_get_contents($request);
if ($response === FALSE) {
    echo "Request failed.
";
} else {

    // parse XML
    $xsl = new DOMDocument;
    $xsl->load('amazon.xsl');

    $proc = new XSLTProcessor();
    $proc->importStyleSheet($xsl);

    $doc = DOMDocument::loadXML($response);
    $transform = $proc->transformToXML($doc);
    $pxml = simplexml_load_string($transform);

    if ($pxml === FALSE) {
        echo "Response could not be parsed.
";
    } else {
        if (isset($pxml->Items->Item->ItemAttributes->Title)) {
            echo $pxml->Items->Item->ItemAttributes->Title, "
";
        }
    }

}

*aws_signed_request.php*

function aws_signed_request($region, $params, $public_key, $private_key, $associate_tag=NULL, $version='2011-08-01')
{    
    /*
    Parameters:
        $region - the Amazon(r) region (ca,com,co.uk,de,fr,co.jp)
        $params - an array of parameters, eg. array("Operation"=>"ItemLookup",
                        "ItemId"=>"B000X9FLKM", "ResponseGroup"=>"Small")
        $public_key - your "Access Key ID"
        $private_key - your "Secret Access Key"
        $version (optional)
    */

    // some paramters
    $method = 'GET';
    $host = 'webservices.amazon.'.$region;
    $uri = '/onca/xml';

    // additional parameters
    $params['Service'] = 'AWSECommerceService';
    $params['AWSAccessKeyId'] = $public_key;
    // GMT timestamp
    $params['Timestamp'] = gmdate('Y-m-d\TH:i:s\Z');
    // API version
    $params['Version'] = $version;
    if ($associate_tag !== NULL) {
        $params['AssociateTag'] = $associate_tag;
    }

    // sort the parameters
    ksort($params);

    // create the canonicalized query
    $canonicalized_query = array();
    foreach ($params as $param=>$value)
    {
        $param = str_replace('%7E', '~', rawurlencode($param));
        $value = str_replace('%7E', '~', rawurlencode($value));
        $canonicalized_query[] = $param.'='.$value;
    }
    $canonicalized_query = implode('&', $canonicalized_query);

    // create the string to sign
    $string_to_sign = $method."
".$host."
".$uri."
".$canonicalized_query;

    // calculate HMAC with SHA256 and base64-encoding
    $signature = base64_encode(hash_hmac('sha256', $string_to_sign, $private_key, TRUE));

    // encode the signature for the request
    $signature = str_replace('%7E', '~', rawurlencode($signature));

    // create request
    $request = 'http://'.$host.$uri.'?'.$canonicalized_query.'&Signature='.$signature;

    return $request;
}

I know it's a lot of code, but everything in PHP works perfectly fine and I do get a movie title displayed on the page. I just want to apply my XSLT stylesheet to it as well. I am very new to PHP and cannot figure out how to do it properly.

查看全部
dsfdf854456
dsfdf854456
2013/03/21 14:34
  • amazon
  • xml
  • php
  • xslt
  • api
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

2个回复