Commerce
|
|
|
|
<?
function GetPriorityMailPrice($from_zip, $to_zip, $pounds, $ounces, $safety)
{
$q="";
$p="";
//slam input values
$pounds=intval($pounds);
$ounces=intval($ounces);
$url="http://postcalc.usps.gov/speed.asp?OZ=".$from_zip."&DZ=".$to_zip."&P=".$pounds."&O=".$ounces."&Char=0&zipok=Continue&LP=package&OV=off&BL=off&Dir=forward&retspec=no";
//debug
//echo("$url<p>n");
//open HTTP connection to USPS's postal calculation server and send a GET-encoded request
// save the response HTML in $q
// what does the @ do? It supresses passing on any error messages to the browser FOR THE LINE IT OCCURS ON.
@$fp=fopen($url,"r");
if ($fp!=false)
{
//read the HTML page result
while (!feof($fp))
{
$q.=fread($fp,80);
}
fclose($fp);
//look for the occurance of the text right before the Priority Mail price quote
$ss= "<TD HEADERS='ROW2 HEADER3' align=right><font face='Arial'><B>$";
$e = stristr($q,$ss);
$e=substr($e,strlen($ss));
//parse out the price information from the rest of the HTML
for ($a=0;$a<strlen($e);$a++)
{
$c=substr($e,$a,1);
if ($c=="<") $a=strlen($e); else $p.=$c;
}
}
//slam it to a double value; error is returned as a price of 0.00
$p=doubleval($p);
//if there was an error (and the safety is on), we "fudge" the value by using this approximation.
//Many times the postage is less than this amount, but it is never over this amount.
// (it assumes that we are sending the package to a Zone 5 destination everytime)
if (($p==0)&&($safety==true)) $p=intval(($pounds+1)/4.00-0.01)*7.95+7.95;
return $p;
}
?>
|
|
|
Usage Example
|
- Copy function into a file called "primail.h"
- Invoke script with this HTML page:
<html>
<h1>Calculate Priority Mail Price</h1>
<form action=showprice.php>
From Zip: <input type=text name=fz size=5><br>
To Zip: <input type=text name=tz size=5><br>
Weight: <input type=text name=p size=2> pounds <input type=text name=o size=2> ounces.<p>
<input type=submit value="Get Price">
</html>
Cut and past this into file "showprice.php":
<html>
<?php
include("primail.h");
$price=GetPriorityMailPrice($fz,$tz,$p,$o);
echo("The price is: ".sprintf("$ %.2f",$price)."<p>");
?>
<html>
|
|
|
Rate This Script
|
|
|
|