Case Study: Problems encountered with Amazon API, PHP, XML, and Cross Domain Policy
Case Study: Using Amazon’s WSDL
First I downloaded the package from Sephiroth.it that handles all the SOAP, which you can get here: http://www.sephiroth.it/file_detail.php?id=112. You’ll get a few files, the most important ones being the PHP files, since that includes all the SOAP stuff that I vaguely understand, let alone be able to write from scratch. However, this is set up for books and requires some modification to work for an music artist search.
First, in the soapClient.php file, you’ll need to set your $Token with the one you were given from Amazon.com when you signed up for their AWS account. (I guess you don’t need to worry about the ‘Tag ID’) Now, if you open up the AmazonSearch.php, you’ll see the WSDL file we’ll be referencing, which is: http://soap.amazon.com/schemas2/AmazonWebServices.wsdl. Now, make sure that if you’re looking at the WSDL though your browser; that you are looking at this one, because there are a bunch of them. And trust me, if you spend hours looking at the wrong one trying the figure out how the hell it works, you’ll feel like an idiot when you realize that you were looking at a different schema. Trust me.
Now, still in the AmazonSearch.php file, find where there are $Category variables, and change them from ‘book’ to ‘music’. This will make sure we are only getting results from that are music, but I think that it only searches popular music (as opposed to classical, which might be ‘classical’)
Now in the DoKeywordSearch function, there is a variable that calls _DoSearch, and the first parameter is ‘KeywordSearchRequest’ which is find if that’s how you want to perfom the search, but I want to search by the artist’s name. So change it to ‘ArtistSearchRequest’, and right above that for the $Params array, make sure you change the first item to be ‘artist’ (from ‘keyword’). We should be done here in this file. Maybe.
Back in the SoapClient.php file, we can configure it to get certain results. The section that looks like the following is where we get our data:
$r .= “asin$i=$ResultRow[Asin]&url$i=$ResultRow[Url]&title$i=$ResultRow[ProductName]
Basically, where you have this format: var$i=$ResultRow[Var], where as ‘var’ is the variable that is passed into Flash, and ‘Var’ is the attribute name from the WSDL. Accessing the data from Flash is as simple as this, here is an example for getting the album name:
PHP: title$i=$ResultRow[ProductName]
Flash: myVars['title'+a]
With that, you should be able to check the WSDL and grab whatever info you need. Unless, if you might have noticed, an element is of type “typens:ArtistArray”
Above this little section in the PHP, there’s some code that looks like this:
$ResultRow = $Result[$i];
$det = $ResultRow['Details'];
if(is_array($det[Authors])){
$authors = $det['Authors'];
} else {
$ authors = $det['Authors'];
}
So all this does is break apart the array, if there are more than one author, and strings them together. So, logically, if you want to change this to Artists, all you have to do is replace ‘Authors’ with ‘Artists’ right? That’s that the WSDL says anyways. Well, it’s not. Why? I wish I knew, I could have saved a lot of time. What you want to do is not use the Details array for this part. So I just modified mine to look this (not that I changed our variable of $authors to $artist):
$ResultRow = $Result[$i];
$det = $Result[$i];
if(is_array($det['Artists'])){
$artist = implode(”,”,$det['Artists']);
} else {
$artist = $det['Artists'];
}
Random Errors with PHP and mySQL:
So I had an issue with my database, which is locaed at mysql.eric-decker.com, and my project was up on eric-decker.com/projects. On some computers (Macs, besides my own) the site would not work – it would not fine the database. I ran a packet sniffer and the issue was that the php file was running into cross domain policy violations. (Which is weird, since the database is on a subdomain)
Well, that wasn’t the problem. It’s just a weird bug, and it happens randomly and effect the way some browser on Macs handle the XML from the PHP file. So, in the php file, after you create your XML string, you need two extra lines (this goes after your closing xml tag)
echo ‘ ‘;
flush();
Don’t know why or how, but that seems to fix that issue, so be on the lookout for it. You might want to incluse that in all PHP files with XML returns from now on, just to be on the safe site.
Cross Domain Policy with CIAS at RIT:
So I had a lot of trouble with cross domain policy. First I had the site on my own server, and for some reason CIAS would block it from functioning. It worked everywhere on different platforms except in building 7. So I moved the site to CIAS, which then let me do the database calls but not the Amazon calls. So I put the amazon php files on my server, and made a remote call to them from CIAS. However, I had to add a crossdomain.xml file allowing traffic from CIAS. Here’s the trick: Every sub domain is different. So rit.edu is different from cias.rit.edu and different from ecd7720.cias.rit.edu. Simple solution in to add in the file: *.rit.edu so any sub domain can call xml files. So for those of you working off of the CIAS server, keep in mind that even though you might be accessing and making calls to cias.rit.edu/~dce that now all users have subdomains, so really you’re calling dce.cias.rit.edu which can mess up security settings.
3 comments3 Comments so far
Leave a reply
oh dear! you must be bored to death- or a loner….
why the dreary website?
Yeah I agree the design of it is actually kinda dull. The site/blog was for a class where we had to post our progress on our projects.
The post you responded to was after I had finished the project, I wanted to write down some of the problems I encountered working with some of the Amazon technology, as I know there are some people in my class who might use it in the future, and I thought it could be some use to them.
I also figured out that I don’t want to deal with server side technology for a career, I’ll let the IT people play with databases and php.
very interesting.
i’m adding in RSS Reader