note 86860 added to function.fsockopen

From: Date: Thu, 06 Nov 2008 22:49:53 +0000
Subject: note 86860 added to function.fsockopen
Groups: php.notes 
Request: Send a blank email to php-notes+get-146259@lists.php.net to get a copy of this message
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] ); } } ?> ---- Server IP: 209.41.74.194 Probable Submitter: 67.214.241.189 ---- Manual Page -- http://www.php.net/manual/en/function.fsockopen.php Edit -- https://master.php.net/note/edit/86860 Del: integrated -- https://master.php.net/note/delete/86860/integrated Del: useless -- https://master.php.net/note/delete/86860/useless Del: bad code -- https://master.php.net/note/delete/86860/bad+code Del: spam -- https://master.php.net/note/delete/86860/spam Del: non-english -- https://master.php.net/note/delete/86860/non-english Del: in docs -- https://master.php.net/note/delete/86860/in+docs Del: other reasons-- https://master.php.net/note/delete/86860 Reject -- https://master.php.net/note/reject/86860 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#146259) next »