简体   繁体   中英

Get ASIN from pasted Amazon url

Whether with PHP, Javascript, or using the Amazon Product Advertising API, I don't care... I can use them all. I've already looked around and found a few other questions here that are the same, but they don't have working answers that I can see.

Has anyone managed this? Best case, Amazon has added a function to it's API where you can pass them a URL and they return the ASIN and other product information for the page.

I use the following regex on my php based site to affiliate my money links, it might not be perfect, but works pretty well:

/(?:dp|o|gp|-)\/(B[0-9]{2}[0-9A-Z]{7}|[0-9]{9}(?:X|[0-9]))/

Essentially, it looks for an ASIN or ISBN-like string after one of the following: "dp/", "o/", "gp/", or "-/" to avoid false positives. "-/" is possible with older "obidos" formatted links - they still resolve, even though Amazon no longer generates them. It captures only the ASIN/ISBN, so $matches[0] will always contain the correct string (in php). Not tested on non-US links. I haven't looked into it, the ASIN formats or the url patterns might be different.

The challenge is that Amazon URLs aren't of a single format, there are various kinds, depending on the kind of identifier used and the kind of product.

The identifier or key can be an ASIN, or other kinds of product identifiers, eg

Valid Values: SKU | UPC | EAN | ISBN (US only, when search index is Books). UPC is not valid in the CA locale. (via http://docs.aws.amazon.com/AWSECommerceService/latest/DG/ItemLookup.html )

Amazon URLs are often (but not always) of the form http://www.amazon.com/(title of product)/dp/(identifier)/(more random characters not relevant here).

In addition to /dp/, you may also see /product/, /ASIN/, /e/.

Your best bet is to use a regular expression to extract the (identifier), then call the Amazon item lookup API, with all the various types as IdType, passing the identifier as the ItemID, and see what matches.

Without more detail, or a specific use case, it's impossible to know whether the URL has an ASIN, an ISBN, a SKU, or some other kind of identifier, so you'd have to query against all of them, see what matches, and extract the ASIN from Item Lookup return payload.

Also - Amazon encourages use of shortened permalinks in the form of: http://amzn.to/xxxxxxx

These are simple HTTP 301 redirections that you'd have to follow in order to get the full link containing the ASIN.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM