Find Purchase Circles by Zip Code

Combining two different Web Services can create a new feature.

Finding purchase circles is pretty easy, and with a bit of scraping, you can tie them into geographic data. Amazon doesn’t offer the ability to look up purchase circles by U.S. zip code, but using the USPS web site and some simple pattern matching, you can find purchase circles for all cities within a given zip code.

The Code

This PHP script combines two different services with screen scraping. It looks for all of the cities within a particular zip code at the U.S. Postal Service web site. Then it finds matching purchase circles at Amazon and provides links to them. Create zip_circle.php with the following code:

<?php $strZip = $_GET['zipcode']; $zipPage = ""; $indexPage = ""; $cntCity = 0; $myCity = ""; set_time_limit(60); //get a certain number of characters function left($str_in,$num) { return ereg_replace("^(.{1,$num})[ .,].*","\\1", $str_in); } function findCircle($city) { $indexPage = ""; //Find purchase cirlce brose codes $abc = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z"; $a_abc = split(",", $abc); $thisLetter = strtolower(substr($city,0,1)); for($j = 0;$j < count($a_abc)-1; $j++) { if ($thisLetter == $a_abc[$j]) { $thisCode = 226120 + ($j + 1); } } $url = "http://www.amazon.com/exec/obidos/tg/cm/browse-communities/-/" . $thisCode . "/t/"; $contents = fopen($url,"r"); do { $data = fread ($contents, 4096); if (strlen($data) == 0) { break; } $indexPage .= $data; } while(1); ...

Get Amazon Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.