Jump to content

Telling Differece Between A Real Number Or An Error String.


websmoken

Recommended Posts

I'm working on a UPS rates program for my shopping cart.  I come back with an actual shipping amount like 23.55 or an error.  I need to set a variable like $_SESSION["UPS_Error"] if I do.  The errors I've encountered are:

 

error (This is what I get if UPS were busy or offline)

Missing ServiceLevelCode

Missing ShipperPostalCode

Missing ConsigneePostalCode

The maximum per package weight for the selected service from the selected country is 150 pounds.

 


<?php/*-----This script sets up every thing then calls the ups.php script below ---------------*/

    require("ups.php");
    $rate = new Ups;
    $rate->upsProduct("GND"); // See upsProduct() function for codes
    $rate->origin("37217", "US"); // Use ISO country codes!
    $rate->dest("68505", "US"); // Use ISO country codes!
    $rate->rate("RDP"); // See the rate() function for codes
    $rate->container("CP"); // See the container() function for codes
    $rate->weight("55");
    $rate->rescom("RES"); // See the rescom() function for codes
    $quote = $rate->getQuote();
			echo $quote;
?>

<?php/* ------------------ Actual ups calling script - ups.php ---------------------*/


  class Ups{

    function upsProduct($prod){
       /*

     1DM == Next Day Air Early AM
     1DA == Next Day Air
     1DP == Next Day Air Saver
     2DM == 2nd Day Air Early AM
     2DA == 2nd Day Air
     3DS == 3 Day Select
     GND == Ground
     STD == Canada Standard
     XPR == Worldwide Express
     XDM == Worldwide Express Plus
     XPD == Worldwide Expedited
       
    */
      $this->upsProductCode = $prod;
    }
     
    function origin($postal, $country){
      $this->originPostalCode = $postal;
      $this->originCountryCode = $country;
    }

    function dest($postal, $country){
      $this->destPostalCode = $postal;
          $this->destCountryCode = $country;
    }

    function rate($foo){
      switch($foo){
        case "RDP":
          $this->rateCode = "Regular+Daily+Pickup";
          break;
        case "OCA":
          $this->rateCode = "On+Call+Air";
          break;
        case "OTP":
          $this->rateCode = "One+Time+Pickup";
          break;
        case "LC":
          $this->rateCode = "Letter+Center";
          break;
        case "CC":
          $this->rateCode = "Customer+Counter";
          break;
      }
    }

    function container($foo){
          switch($foo){
        case "CP": // Customer Packaging
          $this->containerCode = "00";
          break;
               case "ULE": // UPS Letter Envelope
          $this->containerCode = "01";
          break;
        case "UT": // UPS Tube
          $this->containerCode = "03";
          break;
        case "UEB": // UPS Express Box
          $this->containerCode = "21";
          break;
        case "UW25": // UPS Worldwide 25 kilo
          $this->containerCode = "24";
          break;
        case "UW10": // UPS Worldwide 10 kilo
          $this->containerCode = "25";
          break;
      }
    }
     
    function weight($foo){
      $this->packageWeight = $foo;
    }

    function rescom($foo){
          switch($foo){

        case "RES": // Residential Address
          $this->resComCode = "1";
          break;
        case "COM": // Commercial Address
          $this->resComCode = "2";
          break;
          }
    }

    function getQuote(){
          $upsAction = "3"; // You want 3. Don't change unless you are sure.
      $url = join("&",
               array("http://www.ups.com/using/services/rave/qcostcgi.cgi?accept_UPS_license_agreement=yes",
                     "10_action=$upsAction",
                     "13_product=$this->upsProductCode",
                     "14_origCountry=$this->originCountryCode",
                     "15_origPostal=$this->originPostalCode",
                     "19_destPostal=$this->destPostalCode",
                     "22_destCountry=$this->destCountryCode",
                     "23_weight=$this->packageWeight",
                     "47_rateChart=$this->rateCode",
                     "48_container=$this->containerCode",
                     "49_residential=$this->resComCode"
           )
                );
      $fp = fopen($url, "r");
      while(!feof($fp)){
        $result = fgets($fp, 500);
        $result = explode("%", $result);
        $errcode = substr($result[0], -1);
        switch($errcode){
          case 3:
            $returnval = $result[8];
                break;
          case 4:
            $returnval = $result[8];
            break;
          case 5:
            $returnval = $result[1];
            break;
          case 6:
            $returnval = $result[1];
            break;
        }
      }
      fclose($fp);
          if(! $returnval) { $returnval = "error"; }
					return $returnval;
    }
  }
?>

 

I've tried the "gettype() " and tried "function check_int" It was probably something I did wrong but couldn't get thoses to work.  :(  Maybe one of ya'll expert coders could lead me in the right direction.

Thanks,

          Dave 

[move]...  _ _ _  ...[/move]

You could replace this...

 

 

    $quote = $rate->getQuote();
			echo $quote;
?>

 

With this...

 

	$quote = $rate->getQuote();

if ( abs ( $quote ) == 0 )
{
	// not a valid dollar amount, so we have an error
	$_SESSION['UPS_Error'] = $quote;
}
else
{
	// got a valid return dollar amount, so show it!
	echo $quote;
}
?>

 

printf

printf

Thanks Bubba that done it.  I'll remember you in my will.  If I ever get outta da POHouse, I'll hire an Attorney and write 1. ;D

;)

Thanks again and Thanks PHP Freaks for this great Forum

 

  Dave

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.