Postcode to TinyURL

0 && !empty($_POST[postcode])) { // Regular expression to match a UK postcode: 1-2 letters, 1-2 digits, an optional extra letter; // then an optional space, a digit and two more letters. See http://www.upu.int/post_code/en/countries/GBR.pdf // for more info $postcodeRegex = "^[[:alpha:]]{1,2}[[:digit:]]{1,2}[[:alpha:]]?[[:space:]]*[[:digit:]][[:alpha:]]{2}$"; if (!ereg($postcodeRegex, $_POST[postcode])) { echo "Error! You have not supplied a correctly formatted postcode!"; } else { // Build the Multimap URL (we ask for the print version, apart from that all other options are default); $mapURL = "http://uk.multimap.com/map/browse.cgi"; $mapURL .= "?client=print&scale=5000&"; $mapURL .= "pc=".rawurlencode($_POST[postcode]); // Check to see if it produces a valid map by opening the multimap URL $f = fopen($mapURL, "rb"); $mapHTML = fread($f, 1024000); // Multimap's error message if they can't find it is this: $mapErrorMessage = "we do not recognise that as a valid Royal Mail postcode"; // Check for the error if (strpos($mapHTML, $mapErrorMessage) !== false) { echo "Error! The postcode does not exist!"; } else { // If all good then... // Sort out the data to be POSTed to the tinyurl server, so encode the whole Map URL $tinyData = "url=".rawurlencode($mapURL); $contentLength = strlen($tinyData); $tinyServer = "tinyurl.com"; $tinyRequest = "/create.php"; $ReqHeader = "POST $tinyRequest HTTP/1.0\n". "Host: $tinyServer\n". "Content-Type: application/x-www-form-urlencoded\n". "Content-Length: $contentLength\n\n". "$tinyData"; // HTML we get back from the tinyURL server $tinyHTML = ""; // Open the connection to the host $tinySocket = fsockopen($tinyServer, "80"); if ($tinySocket) { // Send the POST data fputs($tinySocket, $ReqHeader); // Read what comes back while (!feof($tinySocket)) { $tinyHTML .= fgets($tinySocket,1024); } fclose($tinySocket); // Extract the tinyURL from the HTML - it's after a tag and consists of http://tinyurl.com/ followed by lots // of letters and digits, and dump the result into an array $regexArray = array(); ereg("()(http://tinyurl.com/[[:alnum:]]+)", $tinyHTML, $regexArray); // Third element of the array corresponds with the second bracketed expression $tinyURL = $regexArray[2]; // If they change the formatting of the page the above will break and we won't get the right result if (empty($tinyURL)) { echo "Error! No URL was sent back from $tinyServer"; } else { // If it is all good, then our tinyURL is made! echo "

TinyURL made!

"; echo "A TinyURL: $tinyURL has been generated for your postcode $_POST[postcode]."; } } else { echo "Error connecting to $tinyServer! Contact webmaster for more information."; } } } } ?>

Enter A Postcode

Type in a full UK postcode (e.g. SW1A 2AA) and get a tinyURL linking to a map of the local area.
 
Postcode:    

Disclaimer: This site is not affiliated, associated, or in any other way connected officially with either Multimap or TinyURL