PX : code

UPS Class (NON-US) by Alan Knowles
Download this code


<?
/*
    UPS Shipping calculator for NON US shipments.
    The class is compatible with the original UPS class
    

    Sample usage: 
    $rate = new omphp_UPS; 
    $rate->origin(\"08033\", \"US\"); // Use ISO country codes! 
    $rate->dest(\"90210\", \"US\");      // Use ISO country codes! 
    $rate->container(\"CP\");    // See the container() function for codes 
    $rate->weight(\"2\"); 
    $rate->rescom(\"RES\");    // See the rescom() function for codes 
    $quote = $rate->getQuote(); 
    echo $quote; 

*/

Class omphp_Http_post {
    var $REMOTE_PAGE;
    var $REMOTE_HOST;
    var $REMOTE_HOST_IP;
    var $REMOTE_HOST_PORT;
    var $GET_VARS;
    // array of get vars
    
    function get_data() {
        if($this->GET_VARS) {
            while(list($k,$v)=each($this->GET_VARS))
                $this->REQUEST.=\"$k=\".urlencode($v).\"&\";
            $this->REQUEST=substr($this->REQUEST,0,-1);
        }
        $this->msg=\"POST $this->REMOTE_PAGE HTTP/1.1\\r\\n\";
        if($this->REFERER)
            $this->msg.=\"Referer: \".$this->REFERER.\"\\r\\n\";
        $this->msg.=\"Host: $this->REMOTE_HOST\\r\\n\";
        $this->msg.=\"Content-type: application/x-www-form-urlencoded\\r\\n\";
        $this->msg.=\"Content-Length: \".strlen($this->REQUEST).\"\\r\\n\\r\\n\";
        $this->msg.=$this->REQUEST;

        $f=fsockopen($this->REMOTE_HOST_IP,$this->REMOTE_HOST_PORT);
        fputs($f,$this->msg);
        socket_set_timeout($f,5);
        $retval=\"\";
        $eof=0;
        $res=\"X\";
        while(!$eof
            && $res) {
            $res=fread($f,2000);
            $retval.=$res;
            list($this->timed_out,$this->blocked,
                $eof,
                $unread_bytes)=socket_get_status($f);
            if($this->timed_out)
                return \"\";
            if($this->blocked)
                return \"\";
        }
        fclose($f);
        return $retval;
    }
}







Class omphp_UPS extends omphp_Http_post {
    var $GET_VARS=array(\"iso_language\"=>\"en\",
        \"iso_country\"=>\"US\",
        \"origcountry\"=>\"HK\",
        \"origpostal\"=>\"\",
        \"destcountry\"=>\"BM\",
        \"destpostal\"=>\"\",
        \"destcity\"=>\"\",
        \"residential\"=>\"NO\",
        \"packages\"=>1,
        \"rate_chart\"=>\"RTP\",
        \"container\"=>21,
        \"currencycode\"=>\"HKD\",
        \"weight1\"=>1,
        \"weight_std1\"=>\"KGS\",
        \"length1\"=>\"\",
        \"width1\"=>\"\",
        \"height1\"=>\"\",
        \"ratesummarypackages.x\"=>73,
        \"ratesummarypackages.y\"=>15);
    var $REMOTE_PAGE=\"/servlet/QCCServlet\";
    var $REMOTE_HOST=\"wwwapps.ups.com\";
    var $REFERER=\"http://wwwapps.ups.com/servlet/QCCServlet\";
    var $REMOTE_HOST_IP=\"153.2.224.55\";
    var $REMOTE_HOST_PORT=80;
    
    function upsProduct($prod) {
        // not supported = Delivery type!
        return;
        // not supported =
    }
    
    function origin($postal,$country) {
        $this->GETVARS[origpostal]=$postal;
        $this->GETVARS[origcountry]=$country;
    }
    
    function dest($postal,$country) {
        $this->GETVARS[destpostal]=$postal;
        $this->GETVARS[destcountry]=$country;
    }
    
    function rate($foo) {
        // not supported = pickup type
    }
    
    function container($foo=\"21\") {
        switch($foo) {
            case \"CP\":
                // Customer Packaging
                $this->GETVARS[container]=\"00\";
                break;
            case \"ULE\":
                // UPS Letter Envelope
                $this->GETVARS[container]=\"01\";
                break;
            case \"UT\":
                // UPS Tube
                $this->GETVARS[container]=\"03\";
                break;
            case \"UEB\":
                // UPS Express Box
                $this->GETVARS[container]=\"21\";
                break;
            case \"UW25\":
                // UPS Worldwide 25 kilo
                $this->GETVARS[container]=\"24\";
                break;
            case \"UW10\":
                // UPS Worldwide 10 kilo
                $this->GETVARS[container]=\"25\";
                break;
            default:
                $this->GETVARS[container]=$foo;
        }
    }
    
    function weight($foo) {
        $this->GETVARS[\"weight1\"]=$foo;
    }
    
    function rescom($foo) {
        switch($foo) {
            case \"RES\":
                // Residential Address
                $this->GETVARS[residential]=\"YES\";
                break;
            case \"COM\":
                // Commercial Address
                $this->GETVARS[residential]=\"NO\";
                break;
        }
    }
    
    function getQuote() {
        $html=$this->get_data();
        $a=explode(\"Total Rate\",$html);
        $b=explode(\"</TR>\",$a[1]);
        $c=$b[0];
        $c=ereg_replace(\"<[^>]+>\",\"\",
            $c);
        $c=eregi_replace(\"[^0-9.]+\",\"\",
            $c);
        return ceil($c);
    }
}


?>

Comments or questions?
PX is running PHP 5.2.17
Thanks to Miranda Productions for hosting and bandwidth.
Use of any code from PX is at your own risk.