Extracting data from remote HTML files without the use of regular expressions

You may have noticed that esnipe.com has started charging for their service. Fair enough. But I hate the thought of paying for something I can program myself. I think this is called righteous indignation, thus the birth of http://cniper.com

I am a graphics guy by day, and so knew that to create an auction sniping site would be a challenge. The toughest part would be extracting the data about the auction from ebay's pages. A little source code viewing quickly revealed ebays bidding system protocols. They are very simple.

To place a bid on ebay you need to send the obvious variables, like username, password, maximum bid, quantity and auction item number. But there is another variable called "key". It is sent as a hidden tag from the login screen after you have declared your maximum bid on actual item viewing page.

This hidden "key" value is a string of characters like "$1$27216251$KIZTwRTWLf5xlqtnrp0Oh1" The only consistent thing I could find was the presence of "$" at common locations. Even after reading Dario Gomes' excellent introduction to regular expressions, I was against a wall. What would happen if a "$" appeared ahead of the hidden tag in the page and tricked my code into capturing something like "$Rare$Fossilized13thcenturyRockfishSkeleton".

Here's the critical line from a recent auction:

			<input type=hidden name=item value=1238546294><input type=hidden name=key value=$1$23187461$8pOqejD/opnGPcVLA5c11/><input type=hidden name=maxbid value=325.00>

Now I am sure some real code warrior could hack out a fail-safe regular expression to extract this hidden key in 12 seconds or less. But he wasn't here. I was. There had to be a better way.

Enter the php explode() function. With simple 4 lines of code I have the hidden keys being written to the database for cniper.com flawlessly (so far).

			$filekey=fopen("http://cgi.ebay.com/aw-cgi/eBayISAPI.dll?MfcISAPICommand=MakeBid&item=$number&maxbid=$max","r");
$auctionkey=fread($filekey,12000);
$key=explode("key value=",$auctionkey);
$clean_key=explode(">";,$key[1]);

Lines 1 and 2 request the bid confirmation screen from ebay with an http fread() command. The page is assigned to a variable.

In line 3, I explode that variable with the separator "key value=". This leaves $key[0] with everything before the tag, and $key[1] with everything following HTML text "key value="

We can forget about $key[0], it contains the rock fish skeleton. In line 4 we apply the explode() command one more time to $key[1] but this time using the ">" that closes the hidden HTML tag. Voila! we are now left with $clean_key[1] being the buried jem needed to make the cniper.com site work it's magic.

To send bids, we construct the appropriate HTTP call with the variables retrieved from ebay's servers and wham! bids are placed while you sleep. PHP rocks!