Amazon Product Advertising API - SOAP + PHP + New Requirements

The most popular thing I've ever written was the SOAP+PHP+Authentication article for Amazon Web Services.

Sad, but true.

As of November 1, 2011, Amazon changed the minimum requirements for its soap services, and in doing so, broke my example. I only know this because of a few comments on my blog post alerting me to the issue, and asking if I had a fix. 

Now, I do.

First, they changed the structure of the request a little bit. You now have a nested parameter object, and the AssociateTag element is required.

Additionally, the Signature, Timestamp, and AWS Access Key Id is now required at the SOAP header level.



function find_match ($search,$section="All",$locale='US'){

        $id='<Your Amazon Access Key ID>';
        $key='<Your Amazon Secret Access Key>';
        $wsdl = "http://ecs.amazonaws.com/AWSECommerceService/2010-11-01/$locale/AWSECommerceService.wsdl";
        $associates_tag = '<Your Amazon Associates Tag>';
 
        //  Create the DigSig for the AWS Call.
        $timeStamp = gmdate("Y-m-d\TH:i:s\Z");
        $string = 'ItemSearch'.$timeStamp;
        $signature = base64_encode(hash_hmac("sha256", $string, $key, True));

        $client = new SoapClient($wsdl);

        $headers = array(
                        new SoapHeader(
                                'http://security.amazonaws.com/doc/2007-01-01/',
                                'AWSAccessKeyId',
                                $id
                                ),
                        new SoapHeader(
                                'http://security.amazonaws.com/doc/2007-01-01/',
                                'Timestamp',
                                $timeStamp
                                ),
                        new SoapHeader(
                                'http://security.amazonaws.com/doc/2007-01-01/',
                                'Signature',
                                $signature
                                )
                        );

        //create the nested request object.

        $params["AssociateTag"] = $associates_tag;
        $params["AWSAccessKeyId"] = $id;

        $params['Request'] = array (
                        "Service"=>"AWSECommerceService",
                        "Operation"=>"ItemSearch",
                        "Keywords"=>$search,
                        "SearchIndex"=>"All",
                        "ResponseGroup"=>"Large",
                        );

        //print_r($params);
        $client->__setSoapHeaders($headers);
        $response = $client->__soapCall('ItemSearch',array($params));





        //If its an array, we have multiple items. If its an object, its a single object.
        //Create a slimmed down array from the reponse w/ the stuff I want in it.

        if (is_array($response->Items->Item) ) {
                foreach ($response->Items->Item as $item) {
                        $toReturn[] =   array(
                                        "Image"=>$item->MediumImage->URL,
                                        "Height"=>$item->MediumImage->Height,
                                        "Width"=>$item->MediumImage->Width,
                                        "Detail"=>$item->DetailPageURL,
                                        "Title"=>$item->ItemAttributes->Title,
                                        "Rating"=>$item->CustomerReviews->AverageRating,
                                        "Reviews"=>$item->CustomerReviews->TotalReviews,
                                        "Price"=>$item->OfferSummary->LowestNewPrice->FormattedPrice
                                        );
                }
                return ($toReturn);
        }elseif (is_object($response->Items->Item) ) {
                $item=$response->Items->Item;
                $toReturn[] =   array(
                                "Image"=>$item->MediumImage->URL,
                                "Height"=>$item->MediumImage->Height,
                                "Width"=>$item->MediumImage->Width,
                                "Detail"=>$item->DetailPageURL,
                                "Title"=>$item->ItemAttributes->Title,
                                "Rating"=>$item->CustomerReviews->AverageRating,
                                "Reviews"=>$item->CustomerReviews->TotalReviews,
                                "Price"=>$item->OfferSummary->LowestNewPrice->FormattedPrice
                                );
                return($toReturn);
        }else {
                return(false);
        }
}

?>
 
Posted by Dustin Hawkins at 11:10

Trackbacks
Trackback specific URI for this entry

No Trackbacks

Comments
Display comments as (Linear | Threaded)

No comments

The author does not allow comments to this entry