first_page the funky knowledge base
personal notes from way, _way_ back and maybe today

PHP Code: Call to Amazon.com Web Service with fopen(); phpGetAmazon()

function phpGetAmazon($ASIN){

$amazonAffiliateTag = '';
$amazonURI = '';
$filePointer = false;
$HTML = '';
$index = '';
$key = '';
$stream = '';
$streamline = '';
//This URI seems to work for books as well:
$uri = "http://www.amazon.com/o/ASIN/"
    . "{$ASIN}/ref=nosim/{$amazonAffiliateTag}";
$values = '';

/*
    Amazon.com refers to this search as
    a "light" ASIN and ISBN search.
    Up to 30 ASIN's can be used
    in the light search but this function
    currently returns only one product per call.
*/

//Protocol, domain, path and start of query string:
$amazonURI = 'http://xml.amazon.com/onca/xml3?';

/*
    Associate ID (replaces the
    "webservices-20" tag in examples) and Developer ID:
*/
$amazonURI .= "t={$amazonAffiliateTag}&dev-t=999";

//ASIN specifier:
$amazonURI .= "&AsinSearch=" . $ASIN;

//Specify light search type and XML format:
$amazonURI .= '&type=lite&f=xml';

//Call web service.
$filePointer = @fopen($amazonURI,'r'); 
if($filePointer){
    //xmldom stuff:
    //return phpGetAmazonXML($stream);

    while($streamline = fgets($filePointer,4096)) $stream .= $streamline;
    fclose($filePointer);

    $xml_parser = xml_parser_create(); 
    xml_parser_set_option($xml_parser,XML_OPTION_CASE_FOLDING,0);
    xml_parse_into_struct($xml_parser,$stream,$values,$index); 
    xml_parser_free($xml_parser); 

    foreach ($index as $key=>$val) {
        switch ($key) {
            case 'ImageUrlMedium':
                $imageURL = $values[$val[0]]['value'];
            break;

            case 'OurPrice':
                $ourPrice = $values[$val[0]]['value'];
            break;

            case 'ProductName':
                $productName = $values[$val[0]]['value'];
            break;
        }
    }
    //Add name of product:
    $HTML = "<br><small><a class=\\"amazonLink\\" "
        . "href=\\"{$uri}\\" target=\\"_blank\\">{$productName}</a></small>";
    //Add image:
    $HTML = "<a class=\\"amazonLink\\" href=\\"{$uri}\\" "
        . "target=\\"_blank\\"><img class=\\"main\\" "
        . "alt=\\"Support this web site!\\" border=\\"8\\" "
        . "src=\\"{$imageURL}\\"></a>\\n" . $HTML;

    return $HTML;

} else return 'Web Service call failed.';

}

mod date: 2005-04-28T02:59:37.000Z