note 86860 deleted from function.fsockopen by danbrown

From: Date: Thu, 06 Nov 2008 23:18:52 +0000
Subject: note 86860 deleted from function.fsockopen by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-146262@lists.php.net to get a copy of this message
Note Submitter: Doug ---- Here is a way to get an array of cities / states for any zip code anywhere in the US. This example uses a $_GET variable to specify which zip code, but you can get it from any source you wish. This will return actual and acceptable zip code cities (according to USPS), but can be easily modified to include just the actual city. <?php if($_GET['zip5']) { $fp = fsockopen('zip4.usps.com', 80, $errno, $errstr); if(!$fp) echo 'Could not connect to USPS! Error number: ' . $errno . '(' . $errstr . ')'; else { $poststring = "GET /zip4/zcl_3_results.jsp?zip5=" . $_GET['zip5'] . " HTTP/1.0\r\n" . "Connection: close\r\n\r\n"; fputs($fp, $poststring); $buffer = ''; while(!feof($fp)) $buffer .= fgets($fp, 128); fclose($fp); preg_match('/Actual City name(.*)/s', $buffer, $match); $temp = explode('Not Acceptable', $match[1]); // Capture city/state combination for all valid cities preg_match_all('/headers="pre">(?:<b>)?([\w|\s]+), (\w+)/', $temp[0], $acceptable, PREG_SET_ORDER); $cities = array(); foreach($acceptable as $value) $values[] = array ( 'city' => $value[1], 'state' => $value[2] ); } } ?>

« previous php.notes (#146262) next »