note 86862 added to function.fsockopen
| From: | Doug at osu1 dot php dot net | Date: | Thu, 06 Nov 2008 23:17:06 +0000 |
| Subject: | note 86862 added to function.fsockopen | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-146261@lists.php.net to get a copy of this message | ||
Here is a slightly revised version of the USPS zip code city finder that includes some error
messages, as well as changed the $_GET['zip5'] variable into an easier to remember
$_GET['zip'] variable:
<?php
if($_GET['zip'])
{
if(!($fp = fsockopen('zip4.usps.com', 80, $errno, $errstr)))
echo 'Could not connect to USPS! Error number: ' . $errno . '(' . $errstr .
')';
else
{
$poststring =
"GET /zip4/zcl_3_results.jsp?zip5=" . $_GET['zip'] . "
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);
$values = array();
foreach($acceptable as $value)
$values[] =
array
(
'city' => $value[1],
'state' => $value[2]
);
if(count($values) == 0)
echo 'Zip could not be found in the database!';
}
}
else
echo 'Please specify a zip!';
?>
----
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/86862
Del: integrated -- https://master.php.net/note/delete/86862/integrated
Del: useless -- https://master.php.net/note/delete/86862/useless
Del: bad code -- https://master.php.net/note/delete/86862/bad+code
Del: spam -- https://master.php.net/note/delete/86862/spam
Del: non-english -- https://master.php.net/note/delete/86862/non-english
Del: in docs -- https://master.php.net/note/delete/86862/in+docs
Del: other reasons-- https://master.php.net/note/delete/86862
Reject -- https://master.php.net/note/reject/86862
Search -- https://master.php.net/manage/user-notes.php