Tags

 /**
     * Retrieve book cover from Amazon
     *
     * @param: string $isbn: isbn10 or isbn13 code that is used as identifier
     *
     * @return: URL to the Amazon cover image
     */
    public function getBookCover($isbn)
    {
        // init
        $img = '';
        $params = array();
        $host = 'ecs.amazonaws.com';
        $uri = '/onca/xml';
        $type = 'ISBN';
        $access_key = sfConfig::get('app_amazon_access_key');
        $secret_key = sfConfig::get('app_amazon_secret_key');

        // prepend 987 for isbn10
        // check <a href="http://docs.amazonwebservices.com/AWSEcommerceService/2007-01-17/">http://docs.amazonwebservices.com/AWSEcommerceService/2007-01-17/</a> for info
        if (strlen($isbn) == 10)
        {
            //$isbn = '987' . $isbn;
            $type = 'ASIN';
        }
        else
        {
            $params['SearchIndex'] = 'Books';
        }

        // collect parameters
        $params['Service'] = 'AWSECommerceService';
        $params['AWSAccessKeyId'] = sfConfig::get('app_amazon_access_key');
        $params['Operation'] = 'ItemLookup';
        $params['IdType'] = $type;
        $params['ResponseGroup'] = 'Images';
        $params['ItemId'] = $isbn;
        $params['Timestamp'] = gmdate("Y-m-dTH:i:sZ");
        $params['Version'] = '2009-03-31';

        // sort the parameters
        ksort($params);

        // create the canonicalized query
        $canonicalized_query = array();
        foreach ($params as $param =&gt; $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 = "GETn" . $host . "n" . $uri . "n" . $canonicalized_query;

        // calculate HMAC with SHA256 and base64-encoding
        $signature = base64_encode(hash_hmac("sha256", $string_to_sign, $secret_key, True));

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

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

        // disable error display, just in case
        libxml_use_internal_errors(true);
        libxml_clear_errors();

        $doc = new DomDocument;
        $doc->load($amazon_url);

        // retrieve medium image from amazon result
        $tmp = $doc->getElementsByTagName('MediumImage');

        if ($tmp->length > 0)
        {
            $img = $doc->getElementsByTagName('MediumImage')->item(0)->firstChild->textContent;
        }

        return $img;
    }
Advertisements