Recommendations

I have created a recommedations section at the right. It displays selected CDs and books I like using the Amazon.co.jp database. (They do ship overseas for most printed materials and music CDs, so it’s a pretty good site to get stuff from) It took me damn long to get the script working right.

Stop reading here if you are not interested in technical fluff.

First of all, I have to confess that I was am totally clueless about PHP, so it took me a lot longer than necessary to write this simple script… _|ï¿£|O

Basically Amazon Affliate links allow you to link to individual items and adds thumbnail and item information. I noticed that these links are actually webpages nested in an iframe, which makes the actual URL of the page known. The code given looks something like this:

<iframe src="URL IS HERE" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0">

The URL itself is seriously long and it will break my layout if I typed it here. Clicky for a sample of one (btw, that’s KOTOKO’s new single).

As you can see from the iframe code and the URL, Amazon has already provided the user with some levels of customizability. The bg1=ffffff parameter in the URL allows you to set the background colour, lc1= for the link colour, bc1= for the border colour, etc. The most important parameter is asins=B000ANY1HA, which tells Amazon’s script which item you are trying to display, with “B000ANY1HA” being the ASIN (Amazon Serial Identification Number) of the item, which can be found on the page of all items listed on Amazon.

Well, being able to customize the colour is great, but it’s not enough. The links are all underlined and there’s this grey “プライバシーについて” (Privacy policy) text at the bottom which is pretty distracting. The default table and iframe size also cause a tiny white margin to overflow and makes the iframe scroll slightly when you turn the mousewheel. This annoys me greatly.

Since, unlike with Google Adsense which uses Javascript to cloak everything, I now have the URL of the actual page, I decided to reformat it using PHP to fit my layout. Originally, my intention was to do away with the iframe completely and using include() to insert the content directly. The benefit of this is that the CSS formatting will all be applied automatically as the whole thing because effectively part of this page. But I soon realized that the idea stinked because Amazon codes like drunkens and introducing the HTML codes in directly will make my site to contain invalid XHTML.

So I decided to file_get_contents() the thing into a variable, reformat it however I like, echo the result and finally put that into the iframe instead (making sure that there isn’t any overflow). Normally the script retrieves and formats an indexed page (specified using indexed=) instead of a live feed so as to speed things up. The other display method is to input a specific ASIN using the url= parameter, through which the script can display even non-indexed items by downloading the live feed. It cycles through the indexed links randomly by default if neither parameter is present. Source code is here. It’s hardly optimized in anyway, but it works.

The results for comparison: KOTOKO’s 421 -a will- in three versions.

Pretty good considering I started this thing not knowing any PHP syntax at all… It took me damn long to find out that iconv() lets me convert Shift-JIS (used by 99% of Japanese sites) to UTF-8 (used by my blog).

Thanks to everyone from #php (irc.freenode.net) and #nipponsei (irc.rizon.net) for their help.

EDIT: Seems like my host is acting up again… I swear I’m moving somewhere else once my current term is over.

This entry was posted in Blog, Tech and tagged , , , . Bookmark the permalink.

1 Response to Recommendations

  1. Sarah says:

    Thx for the warning. I do hate technical stuff :3

Leave a Reply

Your email address will not be published. Required fields are marked *